const clients = []; self.onconnect = function(e) { console.log(1) const port = e.ports[0]; clients.push(port) port.onmessage = function(e) { const message = e.data; clients.forEach(client => client.postMessage(message)) } port.isMaster = function() { clients.forEach(client => client.postMessage({tabId: port.tabId})) } port.setMaster = function(tabId) { port.tabId = tabId; } }