Firefox附加组件 - 如何在附加面板上的页面上获取发布消息

时间:2013-03-08 04:07:51

标签: javascript html5 firefox firefox-addon

我在firefox附加组件开发中遇到了麻烦。

[症状]: 无法从firefox附加栏的面板页面接收消息,该消息是从panel.html上的iframe发送的。

这是我的代码:

文件[popup.html]中的

/ * * /    //在正文中我添加了一个iframe元素。

   iframe src="http://localhost/hello.html"
文件[popup.js]中的

/ * * /    //我添加了一个监听器

   window.addEventListener("message", 
    function(event) {
        console.log("popupJS Receive Event from WebPage(" + event.origin);      
        console.log(event);
        //alert(event);
    });
远程页面中的

/ * hello.html * /    //我点击发送消息 强调文字

   window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" }, "http://dicrectpass.com");

=====>>    但是,我仍然无法从iframe收到消息!    为什么?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。然后我意识到我在我加载的HTML文档中包含了我的Javascript而不是通过Panel构造函数。一旦我将其切换出来,它就可以正常工作。

Panel reference

var pnl = panel.Panel({
    width: 300,
    height: 300,
    contentURL: self.data.url('popup.html'),
    contentScriptFile: [
        self.data.url('jquery.js'),
        self.data.url('popup.js')
    ],
    onMessage: function(message) {
        console.log(message);
    }
});
相关问题