Chrome Extension message passing : verify the sender
I'm writing a chrome extension in which content-script sends a message to background script.
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
When background.js receives this message it get's the sender information which has
{ ID, tab, url }
I want to verify that this message was sent by content script that belong to my extension and nothing else.
How do I verify the sender ID, how can a background.js get the extension ID.
You don't have to.
There are two distinct events relating to messages:
chrome.runtime.onMessage
- it is only for messages sent by your own extension. sender
is used to determine context, ie tab ID.
chrome.runtime.onMessageExternal
- it is only for messages sent by other sources, be it another extension or a webpage. There, sender
will contain either an extension ID or the URL of the page in question.
Note that you can explicitly restrict the possible senders of external messages in the manifest, in the externally_connectable
key. The default policy is to disallow web pages and allow all extensions.
上一篇: Chrome扩展程序消息作为对象传递
下一篇: Chrome扩展程序消息传递:验证发件人