2016-09-20 71 views
0

我是新來的社區和開發插件。從addon main.js到頁面工作者

我試圖從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

你的問題是什麼? – Basilevs

回答

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}); 
}); 
      } 
});