1

I currently am making a Firefox extension with Firefox add on sdk and I tried to communicate between content script (page mod) and popup (panel) to no avail.

I am using toolbar button by Erik Vold. Here is my code:

var tbb = require('toolbarbutton').ToolbarButton({
    id: 'from-us_button',
    label: 'from-us',
    image: data.url('img/on.png'),
    panel: panel

});

var pageMod = require('page-mod').PageMod({
  include: "*",
  contentScriptFile: [
    data.url('recuperation.js')
    ],
  contentScriptWhen : "end",
  attachTo: ["existing", "top"]

});


var panel = require('panel').Panel({
    width: 200,
    height: 500,
    contentURL: data.url('popup.html')

});

My popup.html contains:

<script type="text/javascript" src="popup.js"></script>

I would like to pass a variable from recuperation.js to popup.js, how could I do that?

4

2 回答 2

2

在您的 popup.js 中,您应该有一个“插件”全局对象,它允许您将消息发送回 main.js。这记录在这里:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/panel.html#Styling%20Trusted%20Panel%20Content

于 2013-06-04T19:38:34.277 回答
1

您需要将popup.js附加到您的面板:

var panel = require('panel').Panel({
    ...
    contentURL: data.url('popup.html'),
    contentScriptFile:  require("self").data.url("popup.js")  //or data.url() if you already have it set
});

通过标签加载的脚本<script>缺少使用self.port.

然后将监听器添加到您的 page-modworker并将消息中继到您的面板,如下所示:

var panel = ...
...
require('page-mod').PageMod({
  include: "*",
  contentScriptFile: [
    data.url('recuperation.js')
  ],
  contentScriptWhen : "end",
  attachTo: ["existing", "top"],
  onAttach: function(worker){
    worker.port.on("recuperation-to-panel",function(msg){
        panel.port.emit("recuperation-to-panel",msg);
    });    
  }
});

然后从recuperation.js

self.port.emit("recuperation-to-panel",msg);

* 编辑:应该是worker.port.on; 不是pageMod.port.on。对不起。

于 2013-06-04T19:50:33.117 回答