0

我有background.js以下片段:

chrome.browserAction.onClicked.addListener(toggleStatus);

现在在里面toggleStatus我想向我的内容脚本发送一条消息,表明发生了一些事情。那可能吗?

还是我完全错过了重点,这是正确的方法吗?

这背后是我想激活/停用我的扩展程序,而不需要重新加载页面。

4

1 回答 1

0

经过更多的尝试和错误,我终于找到了一个让我足够满意的解决方案。

首先在内容脚本中添加一个事件监听器:

var extensionPort = chrome.extension.connect();

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

要将消息发送到扩展的每个连接端口,请将其添加到background.js

chrome.extension.onConnect.addListener(function(port) {
  port.postMessage({ someMessage: 'take me to the foobar' });
});
于 2012-09-18T17:59:35.823 回答