I'm working on porting a Chrome extension to Firefox using the Firefox Add-on SDK.
The extension consists of a panel hooked up to a toolbar button (equivalent to Chrome's popup.html + browser action) and a PageMod content script.
When the panel opens, it needs to send a message to the current tab's content script to receive an object containing some information from that page. The part I'm having trouble with is how to actually do the message passing. Can someone help point me in the right direction? I can't seem to find many resources to help Chrome extension developers learn Firefox addon development.
The following question demonstrates this concept in the Chrome environment. I just need help porting it to Firefox.
Chrome Extension - Message Passing from Popup to Content Script
Word forms: plural panels. 1. countable noun [with singular or plural verb] A panel is a small group of people who are chosen to do something, for example to discuss something in public or to make a decision.
: a member of a discussion or advisory panel or of a radio or television panel.
Noun. pannel (plural pannels) Obsolete form of panel.
An electrical panel is basically a service box that connects a main power line to a home, and distributes electrical currents to the various circuits within the home. Circuit breakers and fuses ensures there are no over-currents during the distribution of power to different circuits.
It's somewhat more complicated with the Add-on SDK because you don't communicate with tabs there - you communicate with workers that you created. And the system won't keep track of the workers, you have to do it yourself. Something like this should work (untested code):
var workers = [];
var pageMod = require("page-mod");
pageMod.PageMod({
include: ...,
contentScriptFile: ...,
onAttach: function(worker)
{
workers.push(worker);
worker.on("detach", function()
{
var index = workers.indexOf(worker);
if (index >= 0)
workers.splice(index, 1);
});
}
});
This makes sure that the workers
variable contains the list of active workers (Worker
object documentation). So when you need to send a message to the worker assigned to a particular tab you do this:
var tabs = require('tabs');
for (var i = 0; i < workers.length; i++)
if (workers[i].tab == tabs.activeTab)
worker.postMessage(...);
Of course you can do this only from the extension itself, not from the content script loaded into a panel or something like that. If you are in a content script you first have to send a message to the extension and it should then forward the message to the worker in the tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With