Chrome Broadcast for Content Scripts

Is there a way to pass a chrome message to all chrome extension content scripts?

chrome.runtime.sendMessage () accepts tabId as input. However, if there are several tabs launched with the contents of the script, how can I translate all the tabs?

+4
source share
1 answer

I assume that you were referring chrome.tabs.sendMessageto the tab identifier argument.

There are 2 in the Chrome API sendMessage:

, . , :

!

chrome.tabs.query({}, function(tabs) {
  tabs.forEach(function(tab) {
    chrome.tabs.sendMessage(tab.id, message);
  });
});

, :

// Even accepts arrays of patterns!
chrome.tabs.query({url: "*://*.example.com/*"}, function(tabs) {
  tabs.forEach(function(tab) {
    chrome.tabs.sendMessage(tab.id, message);
  });
});

/

, . , .

, . , , , .

+4

All Articles