Envíe datos desde el script de contenido a main.js en la extensión SDK de Firefox con puertos
Frecuentes
Visto 332 equipos
0
I'm developing simple FF SDK Add-on, my current goal is to pass hooked link from webpage to Add-on's code and open Add-on's tab.
Manual https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/using_port tells me that using self.port.emit()& self.port.on() is an appropriate way for this. self.port.emit() called in my content script embedded into webpage by PageMod but in main.js I've stumbled with "self.port is undefined" in the window where cfx run is running. Below is snipped:
var pageMod = require("sdk/page-mod");
var self = require("sdk/self");
pageMod.PageMod({
include: "*", // <- this tell firefox to attach the following
// content script to all web pages
contentScriptFile: self.data.url("js/script.js")
});
self.port.on("gotLink", function(myAddonMessagePayload) {
var tabs = require("sdk/tabs");
tabs.open({
url: self.data.url("page.html"),
});
});
probably self.port is not that self that got from var self = require("sdk/self")? then, how do I get right 'self' to listen recieve data from content script?
2 Respuestas
0
self.port.on
works in the content script, where as you'll need a worker object (see using port y worker object) to send a message to the content script. So in main.js: worker.port.on
y worker.port.emit
and in the content script self.port.on
y self.port.emit
.
respondido 19 nov., 14:20
0
1st, You are right about: "self.port is not that self that got from var self = require("sdk/self")". Refer to:https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/self.
Note that the self module is completely different from the global self object accessible to content scripts, which is used by a content script to communicate with the add-on code.
2nd, "addon script" can communicate with " content script" like this:
var worker = require("sdk/tabs").activeTab.attach({
contentScript: "self.port.emit('onTextSelected', window.getSelection().toString());"
});
worker.port.on("onTextSelected", function (text){
console.log("onTextSelected: "+ text);
selectionText = text;
});
You can do the same way in the "PageMode".
Respondido el 09 de enero de 16 a las 12:01
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas firefox sdk port or haz tu propia pregunta.