user3003149
user3003149

Reputation:

firefox extension call to addon function from content script

i want to run addon function[main.js] from content script .i read firefox docs but it's not working for me.this is the official docs about communication between 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 listen.js content script

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

actually i expect console.log("working"); this output .but it's not working .can some one help me i'm really confused here..i actually want to call main.js function from listen.js .

Upvotes: 4

Views: 952

Answers (1)

nmaier
nmaier

Reputation: 33162

main.js doesn't have a self.port, content-scripts do. In main.js you instead need to use port with whatever initiated the content-script. E.g. the PageMod documentation has more.

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

Upvotes: 2

Related Questions