我已经浏览了 MDN 上的 webextension 文档。我确实知道消息传递 Api 用于从 content_script
到 后台脚本
进行通信。但是,我想从弹出脚本到后台脚本进行通信
。
我的用例是:
How to communicate to
background script
from a script associated topopup.html
page.
比方说,manifest.json
是
{
"description": "Demonstrating toolbar buttons",
"manifest_version": 2,
"name": "button-demo",
"version": "1.0",
"permissions" : ["activeTab","currentWindow"],//
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"browser_style": true,
"default_popup": "popup.html",
"default_icon": {
"16": "icons/page-16.png",
"32": "icons/page-32.png"
}
}
}
background.js
//background script is listening to
browser.runtime.onMessage.addListener((sentMesssage) =>
{
console.log('got the message: ',sentMessage.actualMessage);
});
popup.html 是
<html>
<body>
<script src = 'popup.js'></script>
</body>
<html>
popup.js
My question stands here. Which method to use from the below options:
browser.runtime.sendMessage({"actualMessage":"some message"});
或
var tabQ = browser.tabs.query({
currentWindow: true,
active: true
});
tabQ.then( (tabs)=>{
browser.tabs.sentMessage(tab[0].id , {'actualMessage' : "some message"});
});
最佳答案
My question stands here. Which method to use from the below options:
您使用browser.runtime.sendMessage({"actualMessage":"some message"});
关于javascript - firefox webextension 从弹出脚本到后台脚本的消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018895/