2013-03-08 39 views
1

我在Firefox附加组件开发中遇到了麻烦。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

我遇到了同样的问题。然后,我意识到自己正在加载的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); 
    } 
}); 
相关问题