Panel Content & PageMod Script Passed in Firefox Extension

I am working on porting the Chrome extension to Firefox using the Firefox Add-on SDK.

The extension consists of a panel connected to the button (equivalent to the Chrome popup.html + Chrome browser action) and the contents of the PageMod script.

When the panel opens, it needs to send a message to the current contents of the script tab in order to get an object containing some information from this page. I'm having problems with how to actually send a message. Can anyone help point me in the right direction? I can't seem to find many resources to help Chrome extension developers learn about the development of Firefox add-ons.

The following question demonstrates this concept in a Chrome environment. I just need help porting it to Firefox.
Chrome extension - transferring messages from a popup to Script content

+5
source share
1 answer

This is somewhat more complicated with the Add-on SDK, because you don’t communicate with tabs there - you communicate with the employees you create. And the system will not track workers, you have to do it yourself. Something like this should work (unverified 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 ensures that the variable workerscontains a list of active workers ( Workerobject documentation ). Therefore, when you need to send a message to an employee assigned to a specific tab, do the following:

var tabs = require('tabs');
for (var i = 0; i < workers.length; i++)
  if (workers[i].tab == tabs.activeTab)
    worker.postMessage(...);

, , script, , - . script, , .

+9

All Articles