1

又是我 :-|

我无法让事件正常工作,我可以将其与 worker.port.on(...) 一起使用。

查看我的代码:

主.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");
                                }
                            );
            }
        );
};

注入.js:

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

帮助会很好。谢谢!

4

1 回答 1

1

以下调用:

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

应该:

worker.port.emit("testcontent", "bladibla");
于 2013-11-08T21:53:40.147 回答