質問

I have a background.js with following snippet:

chrome.browserAction.onClicked.addListener(toggleStatus);

Now inside toggleStatus I'd like to send a message to my content script, that something has happened. Is that possible?

Or am I completely missing the point, is this the right way to go?

Behind this is that I'd like to activate/deactive my extension, without the need of reloading the page.

役に立ちましたか?

解決

After some more trial & error, I finally found a solution which satisfied me enough.

First add an event listener to the content script:

var extensionPort = chrome.extension.connect();

extensionPort.onMessage(function(msg) {
  // handle the received message
});

To send messages to every connected port of the extension add this to the background.js:

chrome.extension.onConnect.addListener(function(port) {
  port.postMessage({ someMessage: 'take me to the foobar' });
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top