面板& PageMod内容脚本消息在Firefox扩展中传递 [英] Panel & PageMod Content Script message passing in a Firefox extension

查看:181
本文介绍了面板& PageMod内容脚本消息在Firefox扩展中传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firefox插件SDK将Firefox扩展程序移植到Firefox。
$ b 扩展程序由一个面板连接到一个工具栏按钮(相当于Chrome的popup.html +浏览器操作)和PageMod内容脚本。



当面板打开时,需要向当前标签的内容脚本发送消息,以接收包含该页面某些信息的对象。我遇到的部分是如何实际执行消息传递。有人能帮助我指出正确的方向吗?我无法找到许多资源来帮助Chrome扩展程序开发人员学习Firefox插件开发。



以下问题在Chrome环境中演示了这一概念。我只需要帮助将其移植到Firefox。

SDK,因为您不在那里与标签进行通信 - 您可以与您创建的工作人员进行交流。系统不会跟踪工人,你必须自己做。这样的东西应该工作(未经测试的代码):

pre $ 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);
});
}
});

这确保 workers 变量包含在职员工名单( Worker object document )。所以当你需要发送一个消息给分配给特定选项卡的工作者时,你可以这样做:

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

当然,您只能从扩展本身,而不是从加载到面板的内容脚本或类似的东西。如果您在内容脚本中,您首先必须向扩展程序发送消息,然后应该将消息转发给标签中的工作人员。


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

解决方案

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.

这篇关于面板&amp; PageMod内容脚本消息在Firefox扩展中传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆