简体   繁体   English

如何知道哪个内容脚本与 Firefox 附加组件中的后台通信?

[英]How to know which content script communicates with the background in a Firefox add-on?

According to this simple example : how to know which content script (which tab) is actually sending a message to the background (line 10)?根据这个简单的例子:如何知道哪个内容脚本(哪个选项卡)实际上正在向后台发送消息(第 10 行)? For example with the tab id.例如使用标签 ID。

In the example for background-script.js, the portFromCS has a sender property: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/Port在 background-script.js 的例子中, portFromCS有一个 sender 属性: https : //developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/Port

That sender is an object which contains tab and in particular tab.id https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/MessageSender该发件人是一个包含tab的对象,特别是tab.id https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/MessageSender

/* background-script.js */
browser.runtime.onConnect.addListener(port => {
    port.onMessage.addListener((msg) => {
        console.log("bg received", msg, "from tab", port.sender.tab.id);
    });
});

You can also do "one-off" or connection-less messages with browser.runtime.onMessage您还可以使用browser.runtime.onMessage执行“一次性”或无连接消息

the handler has signature (msg, sender, reply) , where sender is the same MessageSender object as above.处理程序具有签名(msg, sender, reply) ,其中 sender 是与上面相同的 MessageSender 对象。

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

相关问题 从后台/内容脚本访问Firefox附加访问弹出脚本? - Firefox add-on access popup script from background/content script? Firefox附加组件:(本地应用程序+内容脚本+后台脚本)消息传递 - Firefox add-on: (Native app + Content Script + Background script) messaging 如何在 Firefox 控制台中访问附加内容脚本? - How to access add-on content script in Firefox console? Firefox附加内容脚本中的Crypto getRandomValue - Crypto getRandomValue in Firefox add-on content script 如何知道Firefox Add-on SDK中其他加载项的安装 - How to know the installation of other add-on in Firefox Add-on SDK Firefox附加组件:如何判断窗口是否在后台 - Firefox add-on: how to tell if a window is in the background 如何在Firefox附加组件上允许跨域内容脚本而不触发混合内容阻止错误 - How to allow cross domain content script on Firefox add-on without firing mixed content blocking errors Firefox附加组件声明功能和在内容脚本中使用 - Firefox add-on declaring functions and use in content script Firefox附加组件的内容脚本不会写入IndexedDB - Content script from Firefox add-on doesn't write to IndexedDB Firefox Add-On SDK中内容脚本中的相对图像URL - Relative image url in content script in Firefox Add-On SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM