Open the page with main.js and pass data to it
I am trying to create a very simple test addon that does one thing: open a html page (located in the data folder) from main.js and then pass it some generated JSON that it can display. I figured out a very complicated way to do this using messaging and cloning unsafeWindow.options, but there should be an easier way?
ps. I'm happy to use the "addon-page" module if that's the right way to do it ...
source to share
A simple template for opening a tab, then when the tab is ready, attach the content scripts, pass some parameters, and set up the message handlers:
In the addon module:
var resourceURL=require("sdk/self").data.url;
require("sdk/tabs").open({
url:resourceURL("index.html"),
onReady:function(tab){
var worker=tab.attach({
contentScriptFile:["support.js","content.js"].map(resourceURL),
contentScriptOptions:{}, //parameters passed to content script
});
worker.port.on("ready",function(msgIn){
worker.port.emit("acknowledge",msgOut);
});
}
});
And in the content of the script; To send the "ready" message and receive the "confirm" message:
self.port.on("acknowledge",function(msgIn){});
self.port.emit("ready",msgOut);
There may be changes in the handling of parameters url
and / or contentScriptFile
so that relative paths are allowed without the need to require ("sdk / self"). data.url. Not sure if this is just speculation / suggestion or if it is currently implemented; Not verified.
Also I think this is addon-page
deprecated and not working since the upgrade to the australis UI (i.e. the navigation bar is no longer hidden for special URIs like about: addons).
source to share
You can use the port to send data in a different way, for example:
main.js
// Create a menu panel
var menu = panels.Panel({
contentURL: data.url("menu.html"),
contentScriptFile: data.url("menu.js"),
});
// Your data
var data = {animal: 'panda'};
menu.port.emit('data', JSON.stringify(data));
menu.js
self.port.on('data', function onReceiveData(data) {
console.log(data);
});
source to share