从addon main.js传递给page-worker

时间:2016-09-20 16:56:44

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

我是社区的新手并且正在开发插件。

我正在尝试从1个页面工作者处获取一些数据并将其发送到主插件然后包含更多数据,然后将其发送给其他页面工作者。

我能够完成第一部分,即来自页面工作者的postMessage,并在主插件处接收。

var self = require("sdk/self");
var pageWorker = require("sdk/page-worker");
var getdata = pageWorker.Page({
    contentScript: "self.postMessage(document.body.innerHTML);",
    contentURL: "http://itildemo.servicedeskplus.com/sdpapi/request?INPUT_DATA={%22operation%22:{%22details%22:{%22status%22:%22open%22,%22from%22:0,%22limit%22:500,%22filterby%22:%22Unassigned_System%22}}}&OPERATION_NAME=GET_REQUESTS&TECHNICIAN_KEY=D357605B-E4B5-4892-A7C2-62CA556CB5A8&format=json" (http://itildemo.servicedeskplus.com/sdpapi/request?INPUT_DATA={%22operation%22:{%22details%22:{%22status%22:%22open%22,%22from%22:0,%22limit%22:500,%22filterby%22:%22Unassigned_System%22}}}&OPERATION_NAME=GET_REQUESTS&TECHNICIAN_KEY=D357605B-E4B5-4892-A7C2-62CA556CB5A8&format=json%27) ,
    contentScriptWhen: "ready"
});
getdata.on("message", function(e) {
    console.log(e);
});

现在可以将postMessage从这里发送到其他页面工作者 像:

getdata.on("message",function(e){
     insertdata.postMessage(e);
});
var insertdata = pageWorker.Page({
     onMessage: function(e){
     console.log(e);
     }
}); 

1 个答案:

答案 0 :(得分:0)

是的,很可能从main.js上的一个工作人员收到消息并将其发送给其他工作人员。这里我们有两个工人。现在,只要从pagemod工作人员收到消息,就会将其发送给其他工作人员。

var wsWorker = require('sdk/page-worker').Page({
    contentURL: "websocket.html",
    contentScriptFile : ["websocket.js"]
});


var pageMod = require("sdk/page-mod").PageMod({
  include: ['*'],
  contentStyleFile: [self.data.url('fillStyle.css')],
  contentScriptFile: ["content.js"],
  contentScriptWhen: "start",
  attachTo: ["top", "frame", "existing"],
  onError : function(error) {
      console.log('ERROR Ocurred :- ',error);
  },
  onAttach: function(worker) {
                 worker.port.on("worker_msg",function(msg_of_other_worker){
  //send this message to another worker.
   wsWorker.port.emit("some_action",{tabUrl : msg_of_other_worker});
});
            }
});