[英]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.