简体   繁体   English

firefox扩展中的跨页面通信

[英]Cross-Page communication in firefox extension

I have two tabs that my extension uses and I wanted to pass events back and forth between them. 我的扩展程序使用了两个选项卡,我希望在它们之间来回传递事件。 I've already developed a Google Chrome extension that does this via the background page api, but there doesn't seem to be an equivalent in firefox. 我已经开发了一个谷歌浏览器扩展程序,通过后台页面api执行此操作,但在Firefox中似乎没有相同的功能。

I thought message-manager in the firefox extension docs would do the trick, but the documentation on the object is quite poor. 我认为firefox扩展文档中的消息管理器可以解决问题,但是关于对象的文档很差。

I'd be just as happy with using one of the tabs to control the other if I can't directly import the ideas of a background page from google chrome api. 如果我不能直接从google chrome api导入背景页面的想法,我会很高兴使用其中一个选项卡来控制另一个。

Any help/guidance would be great. 任何帮助/指导都会很棒。

To do this, you need to set up an event pipe in main.js that accepts events from one page's content script and pipes it to the other page's content script: 为此,您需要在main.js中设置一个事件管道,它接受来自一个页面内容脚本的事件并将其传递给另一个页面的内容脚本:

https://addons.mozilla.org/en-US/developers/docs/sdk/1.7/dev-guide/guides/content-scripts/index.html https://addons.mozilla.org/en-US/developers/docs/sdk/1.7/dev-guide/guides/content-scripts/index.html

I'm not completely sure what your use case is so it's hard to recommend more specific techniques. 我不完全确定你的用例是什么,因此很难推荐更具体的技术。 What you would do is: 你要做的是:

  • ensure that a content script is attached to each tab 确保将内容脚本附加到每个选项卡
  • when a given event that happens in a tab, emit that event to main.js using self.port.emit 当在选项卡中发生的给定事件时,使用self.port.emit将该事件发送到main.js
  • when that event reaches main.js, emit it to the worker for the other tab. 当该事件到达main.js时,将其发送给另一个选项卡的worker。

One thing that isn't clear to me is whether these 2 tabs will be the only tabs open with that specific url? 我不清楚的一件事是这两个标签是否是唯一用特定网址打开的标签?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM