从firefox附加内容脚本到周围页面的消息

时间:2013-02-11 02:15:30

标签: firefox firefox-addon

我正在将Chrome扩展程序移植到Firefox,它使用内容脚本通过postMessage调用周围页面中的方法。该页面包含一个像

这样的事件监听器
window.addEventListener('message', function(event) {
   console.log(event)
}, false);

我正在尝试使用调用从firefox附加sdk内容脚本发送消息:

self.postMessage({toPage: true, type: "initAck"}, "*");

BTW 我尝试直接使用windows.postMessage,但我得到一个例外,说我应该使用self.postMessage

我在添加main.js上收听此消息并通过worker.postMessage方法发布,但消息永远不会到达页面的事件监听器:

pageMod.PageMod({
   include: "*",
   contentScriptWhen: "start",
   contentScriptFile: [self.data.url("jquery.min.js"),
                       self.data.url("myscript.js")],
   onAttach: function(worker) {
      worker.on("message", function(addonMessage) {
           worker.postMessage(addonMessage, "*");
      });
   }
});

一般情况下,firefox上下文脚本是否有办法访问周围的页面?

1 个答案:

答案 0 :(得分:1)

不确定我是否收到了您的问题,但在documentation中描述了如何从内容脚本向页面脚本发送消息:

document.defaultView.postMessage("Message from content script", "http://my-domain.org/");

希望它有所帮助!