目前,我的后台脚本中有这个:
function disable(tabs) {
for (var i = 0; i < tabs.length; ++i) {
browser.tabs.sendMessage(tabs[i].id, {
command: "Disable"
});
}
}
else if (e.target.classList.contains("disable")) {
browser.tabs.query({})
.then(disable)
}
我的内容脚本中有这个:
(function () {
if (window.hasRun) {
return;
}
window.hasRun = true;
function disableHelper() {
clearInterval(Interval1)
console.log("Helper has been disabled")
}
browser.runtime.onMessage.addListener((message) => {
if (message.command === "Blah") {
var Interval1 = setInterval(changeToHelper, 15000);
Interval1;
} else if (message.command === "Disable") {
disableHelper();
}
});
})();
我的权限设置为仅在具有指定 URL 的网站上运行此命令,并且当选项卡处于焦点/活动状态时它可以工作。
但是,如果我在不属于权限范围的另一个选项卡上的弹出窗口中单击“禁用”,则它将无法在所需选项卡上正确禁用。
有没有办法向所有选项卡“发送消息”,无论它们是否处于焦点/活动状态?
我知道这有点晚了,但你开始吧。 您可以通过调用
向所有选项卡发送消息//remove currentWindow:true if you want to add all the tabs across all windows
const tabs = await chrome.tabs.query({
currentWindow: true
});
for (const tab of tabs) {
if (!tab.id) continue;
chrome.tabs.sendMessage(tab.id,{type:"your message"})
}