简体   繁体   中英

communicate from addon main.js to page-worker

I'm new to the community and to developing addons.

I'm trying to fetch some data from 1 page-worker and sending it to the main addon then include some more data to it and then send it to some other page-worker.

I'm able to do the first part ie postMessage from page-worker and receive at main addon.

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

Now is it possible to postMessage from here to other page-worker like:

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

Yes it is very much possible to recieve message from one worker on main.js and send it to other worker. Here We have two workers.Now as soon as message recieved from pagemod worker it is sent to other worker.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM