Tabs do not have the ability to communicate with each other directly. To transfer data, you must use the background script as a message broker.
Expansion example:
manifest.json
{ "name":"Test", "manifest_version": 2, "description":"", "version":"1.0", "content_scripts": [ { "matches":[ "<all_urls>" ], "js": ["content-script.js"] } ], "background": { "scripts":["background.js"] }, "permissions": [ "tabs", "*://*/*" ] }
background.js
// ΠΡΠΎΡΠ»ΡΡΠΈΠ²Π°Π½ΠΈΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ ΠΎΡ Π²ΡΠ΅Ρ
Π²ΠΊΠ»Π°Π΄ΠΎΠΊ chrome.extension.onMessage.addListener(function(msg) { /* * msg = {domain, text} */ chrome.tabs.query({ url: msg.domain // ΠΌΠ°ΡΠΊΠ° Π΄ΠΎΠΌΠ΅Π½ΠΎΠ³ΠΎ ΠΈΠΌΠ΅Π½ΠΈ Π΄Π»Ρ Π²ΡΠ±ΠΎΡΠ° Π²ΠΊΠ»Π°Π΄ΠΊΠΈ }, function(result) { if(result.length) { var tab = result[0]; // ΠΏΠ΅ΡΠ²Π°Ρ Π½Π°ΠΉΠ΄Π΅Π½Π½Π°Ρ Π²ΠΊΠ»Π°Π΄ΠΊ chrome.tabs.sendMessage(tab.id, msg.text); } }); });
Here you can use one content script for all tabs.
In the example below, a tab on the domain yandex.ru sends a message to the tab with the domain google.com.
Please note that the URL mask is used to indicate how to get it. The chrome.tabs.query method supports this kind of URL-pattern .
content-script.js
if(document.location.host.includes('yandex')) { chrome.runtime.sendMessage({ domain: '*://*.google.com/*', text: 'Hello from yandex' }); } // ΠΠΊΠ»Π°Π΄ΠΊΠ° Ρ google.com ΠΏΡΠΈΠΌΠ΅Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ chrome.runtime.onMessage.addListener(function(message) { console.log(message); });