Chrome extension: Send message from background script to *all* tabs

后端 未结 1 1497
青春惊慌失措
青春惊慌失措 2020-12-09 15:52

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

Something like the follow

相关标签:
1条回答
  • 2020-12-09 16:21

    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题