浏览器.tabs.sendmessage是否有办法?
目前,我在背景脚本中有此内容:
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的站点上运行此功能,并且当选项卡处于焦点/活动状态时,它可以正常运行。
但是,如果我在另一个选项卡上单击弹出窗口中的“禁用”,这不是权限的一部分,则不会在所需的选项卡上正确禁用。
有没有办法将所有选项卡“发送”到所有选项卡中,无论它们是否处于焦点/活动状态?
Currently, I have this in my background script:
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)
}
I have this in my content script:
(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();
}
});
})();
I have permissions set to only run this on sites with specified URLs and it works when the tab is in focus/active.
However, if I click "Disable" within my popup on another tab that is not part of the permissions it won't Disable properly on the desired tab.
Is there a way to 'sendMessage' to all tabs regardless if they're in focus/active?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这有点晚了,但是你去了。
您可以通过致电将消息发送到所有选项卡
I know this is a bit late, but here you go.
You can send a message to all tabs by calling