如何在Firefox扩展中实现消息传递? [英] How to implement message passing in Firefox extension?
问题描述
我有一个覆盖 overlay.xul
覆盖 browser.xul
的文件。我希望以类似的方式实现消息传递,就像在Chrome扩展中实现的一样。
chrome.manifest -
content helloworld content /
is-
overlay chrome://browser/content/browser.xul chrome://helloworld/content/overlay.xul
overlay chrome:// navigator /content/navigator.xul chrome://helloworld/content/overlay.xul
skin helloworld classic / 1.0 skin /
style chrome:// global / content / customizeToolbar如何注册content_script.js
在我的情况下是overlay.js
?
覆盖.xul -
< script type =application / x-javascriptsrc =chrome:// helloworld / content / jquery.js/>
< script type =application / x-javascriptsrc =chrome://helloworld/content/overlay.js/>
< script type =application / x-javascriptsrc =chrome://helloworld/content/background.js/>
现在在我的
overlay.js
m使用 - $ / $>
document.documentElement.addEventListener('click',function(e){
messageManager.sendAsyncMessage('MyMessenger.MyMessage',{});
},true);
background.js
addMessageListener(MyMessenger.MyMessage,function(obj){
Firebug.Console.log obj.name);
},true);
- 消息传递的正确语法是什么?
- 如何配置内容脚本和浏览器脚本之间的连接? 如果你感兴趣的只是注入一个内容脚本并与之通信,那么使用附加SDK ,特别是
page-mod
package 。它允许轻松地注入内容脚本,并提供一种沟通的方式(请参阅我提到的文档中的与内容脚本进行通信部分)。
关于
messageManager
,它是针对多进程环境的,但是它也可以在当前单进程的Firefox中运行。上面的代码的主要问题是:addMessageListener
不是全局函数,您应该调用messageManager.addMessageListener()
。但是使用messageManager
在加载到同一个命名空间的脚本之间传递消息,并且可以直接调用对方,这是一个矫枉过正的行为。
要与当前选项卡中的内容脚本进行通信,叠加层中的脚本将执行:
gBrowser.selectedBrowser。 messageManager.sendAsyncMessage('MyMessenger.MyMessage',{});
内容脚本确实有
addMessageListener
作为一个全局函数,所以这应该工作:
$ $ p $ addMessageListener(MyMessenger.MyMessage,function(obj){
console.log(obj.name);
});
I have a file which overwrites
overlay.xul
that overwritesbrowser.xul
. I want to implement message passing in a similar way as implemented in chrome extensions.chrome.manifest-
content helloworld content/ overlay chrome://browser/content/browser.xul chrome://helloworld/content/overlay.xul overlay chrome://navigator/content/navigator.xul chrome://helloworld/content/overlay.xul skin helloworld classic/1.0 skin/ style chrome://global/content/customizeToolbar.xul chrome://helloworld/content/overlay.css
How to I register
content_script.js
which in my case isoverlay.js
?Overlay.xul -
<script type="application/x-javascript" src="chrome://helloworld/content/jquery.js" /> <script type="application/x-javascript" src="chrome://helloworld/content/overlay.js" /> <script type="application/x-javascript" src="chrome://helloworld/content/background.js" />
Now inside my
overlay.js
I'm using -document.documentElement.addEventListener('click', function(e) { messageManager.sendAsyncMessage('MyMessenger.MyMessage', {}); }, true);
And the
background.js
is-addMessageListener("MyMessenger.MyMessage", function(obj) { Firebug.Console.log(obj.name); }, true);
- What is the correct syntax for message passing?
- How do I configure the connection between content script and browser script?
解决方案If all you are interested in is really injecting a content script and communicating with it then it should be easier to use the Add-on SDK, particularly the
page-mod
package. It allows injecting content scripts easily and provides a way to communicate (see "Communicating With Content Scripts" section in the docs I mentioned).As to
messageManager
, it is meant for a multi-process environment but it will work in the current single-process Firefox as well. The main problem with your code above is:addMessageListener
isn't a global function, you should callmessageManager.addMessageListener()
. But usingmessageManager
to pass messages between scripts that are loaded into the same namespace and could call each other directly is an overkill anyway.To communicate with a content script in the current tab the script in the overlay would do:
gBrowser.selectedBrowser.messageManager.sendAsyncMessage('MyMessenger.MyMessage', {});
And the content script would indeed have
addMessageListener
as a global function so this should work:addMessageListener("MyMessenger.MyMessage", function(obj) { console.log(obj.name); });
这篇关于如何在Firefox扩展中实现消息传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!