Chrome 扩展程序:从后台脚本向*所有* 标签发送消息 [英] Chrome extension: Send message from background script to *all* tabs

查看:21
本文介绍了Chrome 扩展程序:从后台脚本向*所有* 标签发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让后台脚本通知所有当前打开的标签(即它们的内容脚本)发生了事件.

Is there a way to have the background script inform all currently open tabs (i.e. their content scripts) that an event took place.

基本上类似于以下内容

chrome.tabs.sendMessage("*", {foo: "bar"})

我怀疑我可以在后台脚本上维护一个打开的选项卡列表,如果可能的话,并使用它.但是有没有更简单的方法?

I suspect I could maintain a list of open tabs on the background script, if that's possible, and use that. But is there a simpler way?

推荐答案

不支持通配符.访问所有标签的唯一方法是查询 all现有标签,并使用 chrome.tabs 发送消息.发送消息.

The wildcard is not supported. The only way to reach all tabs is to query all existing tabs, and dispatch the message using chrome.tabs.sendMessage.

chrome.tabs.query({}, function(tabs) {
    var message = {foo: bar};
    for (var i=0; i<tabs.length; ++i) {
        chrome.tabs.sendMessage(tabs[i].id, message);
    }
});

这篇关于Chrome 扩展程序:从后台脚本向*所有* 标签发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆