firefox附加sdk worker.port事件不起作用

时间:2013-11-08 14:25:04

标签: javascript firefox firefox-addon firefox-addon-sdk

又是我了: - |

我无法使事件正常工作,我可以使用worker.port.on(...)。

请参阅我的代码:

main.js:

// import the modules we need
var data = require('self').data;
var {Cc, Ci} = require('chrome');
var mediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
var self = require('self');
var panel = require("sdk/panel");
var ss = require("sdk/simple-storage");
var tabs = require('sdk/tabs');

// exports.main is called when extension is installed or re-enabled
exports.main = function(options, callbacks)
{
    tabs.on("ready",
            function (activeTab)
            {
                var worker = activeTab.attach({contentScriptFile: [self.data.url("inject.js")]});
                worker.port.on("testmain",
                                function(data)
                                {
                                    self.port.emit("testcontent", "bladibla");
                                }
                            );
            }
        );
};

inject.js:

self.port.on("testcontent",
function (data)
{
window.alert(data);
});
self.port.emit("testmain");

帮助会很好。谢谢!

1 个答案:

答案 0 :(得分:1)

以下电话:

self.port.emit("testcontent", "bladibla");

应该是:

worker.port.emit("testcontent", "bladibla");