浏览器.tabs.sendmessage是否有办法?

发布于 2025-01-26 01:08:34 字数 1054 浏览 2 评论 0原文

目前,我在背景脚本中有此内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2025-02-02 01:08:34

我知道这有点晚了,但是你去了。
您可以通过致电将消息发送到所有选项卡

//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"})
  }

I know this is a bit late, but here you go.
You can send a message to all tabs by calling

//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"})
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文