简体   繁体   中英

firefox jpm - emit to tab from outside the tab-call

In this documentation there is an example of how to emit data/events to a tab. Here is my edited version:

success-window.js

// "self" is a global object in content scripts
// Listen for a "drawBorder"
self.port.on("drawBorder", function(color) {
  document.body.style.border = "5px solid " + color;
});

In index.js

var self = require("sdk/self");
var tabs = require("sdk/tabs");

var success_tab;
var myTab = tabs.open({
    url: data.url("success-window.html"),
    onReady: function onReady(tab) {
        success_tab = tab.attach({
            contentScriptFile: [data.url('angular/angular.min.js'),
                data.url('angular-bootstrap/ui-bootstrap-tpls.min.js'),
                data.url('success-window.js')]
        });
    }
});

How can i use success_tab.port.emit outside the button-definition? Like in another function within index.js eg console.log

console.log('mytab:', myTab); // mytab: undefined
console.log('success_tab:', success_tab); // success_tab: undefined
success_tab.port.emit("drawBorder", "red"); // JPM [error]   Message: TypeError: success_tab is undefined

Try this. Don't forget to adjust include to match whatever your success html page is named like:

var self = require("sdk/self");
var tabs = require("sdk/tabs");
var mods = require("sdk/page-mod");

var successWorker;

mods.PageMod({
    include: "*", //adjust this to match ONLY your success page
    contentScriptWhen:'start',
    contentScriptFile: [data.url('angular/angular.min.js'),
                        data.url('angular-bootstrap/ui-bootstrap-tpls.min.js'),
                        data.url('success-window.js')],
    onAttach: function(worker){

        successWorker= worker;

        worker.on("message", function(aData){
            //messages from success-window.js
        });

        worker.on("detach", function(){
            //prevent worker from being used after it's been disposed
            successWorker= undefined;
        });
    }
});

function postToSuccessWindow(message){
    if(successWorker){
        successWorker.postMessage(message);
    }
}

Also, a call to tabs.open doesn't return a tab. To reference that tab you have to do it like this:

var successTab;

tabs.open({
   url: "success-window.html"
   onOpen: function(tab){
    successTab = tab;
   }
});

Did you try setting it to a global like this:

var self = require("sdk/self");
var tabs = require("sdk/tabs");

var worker;
var button = require("sdk/ui/button/action").ActionButton({
  id: "style-tab",
  label: "Style Tab",
  icon: "./icon-16.png",
  onClick: function() {
    worker = tabs.activeTab.attach({
      contentScriptFile: self.data.url("my-script.js")
    });
    worker.port.emit("drawBorder", "red");
  }
});

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