不能在firefox附加组件上使用“port.emit”

时间:2015-10-26 02:55:06

标签: firefox firefox-addon port emit

我在main.js代码上使用port.emit时遇到了一些问题。

我已经尝试了很多不同的方法来做到这一点,但所有这些都没有成功。 这是我的主要代码:

    // main.js
var tabs = require("sdk/tabs"); //
//var data = require("sdk/self").data; //acrescenta a pasta data
var { ToggleButton } = require("sdk/ui/button/toggle");
var dadosCopiados=[];
var worker1; 
var worker2;
var self = require("sdk/self");


var button = ToggleButton({
  id: "copiadorDeDadosDeAcesso",
  label: "Copiador de Dados de Acesso",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
  onChange: function(state) {
      if(state.checked==true){
           worker1 = tabs.activeTab.attach({
                contentScriptFile: self.data.url("copiadorDeDados.js")
            });

        worker1.port.on("dadosDeAcesso", function processaDadosDeAcesso(dados) {
        console.log(dados[0]);
        console.log(dados[1]);
        //console.log(worker);
    });

      }
      else{

        worker2 = tabs.activeTab.attach({
           contentScriptFile: self.data.url("coladorDeDados.js")
        });

        worker2.port.emit("teste","my message");

       }
    }


});

port.on命令正在运行。 当它通过“port.emit”时,我在命令提示符中收到此错误:

console.error:
JPM [error]   Message: TypeError: this.sandbox is undefined
  Stack:
    receive@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/
modules/commonjs/sdk/content/worker-child.js:75:7
emitOnObject@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre
/modules/commonjs/sdk/event/core.js:112:9
emit@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules
/commonjs/sdk/event/core.js:89:38
processMessageReceived@resource://gre/modules/commonjs/toolkit/loader.js -> reso
urce://gre/modules/commonjs/sdk/remote/child.js:54:38
openModalWindow@resource://gre/components/nsPrompter.js:370:5
ModalPrompter.prototype.openPrompt@resource://gre/components/nsPrompter.js:553:9

ModalPrompter.prototype.alert@resource://gre/components/nsPrompter.js:605:9
@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/com
monjs/sdk/loader/sandbox.js -> resource://myaddon/data/coladorDeDados.js:2:1
load@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules
/commonjs/sdk/loader/sandbox.js:66:12
importScripts@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gr
e/modules/commonjs/sdk/content/sandbox.js:313:9
WorkerSandbox@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gr
e/modules/commonjs/sdk/content/sandbox.js:275:1
constructor@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/
modules/commonjs/sdk/core/heritage.js:146:23
initialize@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/m
odules/commonjs/sdk/content/worker-child.js:52:20
constructor@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/
modules/commonjs/sdk/core/heritage.js:146:23
@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/com
monjs/sdk/content/worker-child.js:140:16
emitOnObject@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre
/modules/commonjs/sdk/event/core.js:112:9
emit@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules
/commonjs/sdk/event/core.js:89:38
processMessageReceived@resource://gre/modules/commonjs/toolkit/loader.js -> reso
urce://gre/modules/commonjs/sdk/remote/child.js:54:38

我是 初学者 ,这是我的第一个插件,我知道这应该是一个简单的问题,但我无法解决这个问题。 此外,我的语言没有文件(葡萄牙语)。

1 个答案:

答案 0 :(得分:0)

问题出在contentScriptFile中的alert。注释掉警报和问题将会消失。

在你自己的堆栈跟踪中,你可以看到它在这里提到:

ModalPrompter.prototype.alert@resource://gre/components/nsPrompter.js:605:9
@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/com
monjs/sdk/loader/sandbox.js -> resource://myaddon/data/coladorDeDados.js:2:1

请注意,我也是firefox-addon开发的初学者。如果我的回答有问题,请发表评论并让我解决。