Firefox extension addon function call from script content

I want to run the addon [main.js] function from the contents of the script.i read firefox docs, but it does not work for me. These are white papers between the scripts https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/using_port

this is my main.js code

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

var pageMod = require("sdk/page-mod");
pageMod.PageMod({
  include: "http://mydomain/x.html",
  contentScriptFile: data.url("listen.js")
});

self.port.on("myAddonMessage", function(myAddonMessagePayload) {
  console.log("working");
});

this is my content listen.js script

var myContentScriptMessagePayload="hi"; 
self.port.emit("myContentScriptMessage", myContentScriptMessagePayload);

In fact, I expect console.log ("working"); this way out. but it does not work. Can someone help me, I'm really confused here. I really want to call the main.js function from listen.js.

+4
source share
1 answer

main.js self.port, . main.js script. . PageMod documentation .

var data = require("sdk/self").data;

var pageMod = require("sdk/page-mod");
pageMod.PageMod({
  include: "http://mydomain/x.html",
  contentScriptFile: data.url("listen.js"),
  onAttach: function(worker) {
    worker.port.on("myAddonMessage", function(myAddonMessagePayload) {
      console.log("working");
    });
  }
});
+2

Source: https://habr.com/ru/post/1547233/


All Articles