【问题标题】:Firefox Add-on - how to get post message on the page in add-on panel.htmlFirefox 附加组件 - 如何在附加组件 panel.html 中的页面上获取帖子消息
【发布时间】:2013-02-23 13:21:36
【问题描述】:

我在开发 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 的消息! 为什么?

【问题讨论】:

    标签: javascript html firefox firefox-addon


    【解决方案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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多