最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Panel & PageMod Content Script message passing in a Firefox extension - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited May 23, 2017 at 10:27 CommunityBot 11 silver badge asked Mar 5, 2012 at 18:28 MikeASchneiderMikeASchneider 1051 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

It's somewhat more plicated with the Add-on SDK because you don't municate with tabs there - you municate 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.

发布评论

评论列表(0)

  1. 暂无评论