我试图运行tabs.sendMessage()MDN 页面上给出的示例代码。所以我的代码是:
清单.json:
{
"manifest_version": 2,
"name": "test1",
"version": "1.0",
"description": "test",
"icons": {
"48": "icons/Ruler48.png"
},
"permissions": [
"notifications",
"tabs",
"activeTab"
],
"browser_action": {
"default_icon": "icons/Ruler48.png",
"default_title": "test"
},
"content_scripts": [{
"matches": ["*://*/"],
"js": ["content-script.js"]
}],
"background": {
"scripts": ["bgS.js"]
}
}
Run Code Online (Sandbox Code Playgroud)
bgS.js:
function onError(error) {
console.error(`Error: ${error}`);
}
function sendMessageToTabs(tabs) {
for (let tab of tabs) {
console.log(tab.id);
browser.tabs.sendMessage(
tab.id,
{greeting: "Hi from background script"}
).then(response => {
console.log("Message from the content script:");
console.log(response.response); …Run Code Online (Sandbox Code Playgroud)