From: Ruben Beltran del Rio Date: Wed, 13 Sep 2023 21:33:22 +0000 (+0200) Subject: Inject script only once X-Git-Url: https://git.r.bdr.sh/rbdr/junction/commitdiff_plain/5685e249653cae8c2ba97c0c59c45e3221d8ffa2?ds=inline Inject script only once --- diff --git a/extension/content_script.js b/extension/content_script.js index c344bd9..9d46669 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -26,6 +26,11 @@ const internals = { console.error(error.stack || error); }, + connect() { + internals.port = chrome.runtime.connect({ name: "content" }); + internals.port.onMessage.addListener(internals.onMessage); + }, + async joinAudioCall({ currentUrl, tada }) { try { const mediaStream = await startMedia(); @@ -84,8 +89,10 @@ const internals = { }, }; -internals.port = chrome.runtime.connect({ name: "content" }); -internals.port.onMessage.addListener(internals.onMessage); +internals.connect(); +chrome.runtime.onConnect.addListener(() => { + internals.connect(); +}); console.debug("Content Script Loaded"); diff --git a/extension/junction.js b/extension/junction.js index 3e8b4d9..7760969 100644 --- a/extension/junction.js +++ b/extension/junction.js @@ -1,5 +1,6 @@ const internals = { promisesSupported: !!window.browser, + injectedScript: false, port: null, currentUrl: null, @@ -20,14 +21,11 @@ const internals = { return internals.hangUp(); } - return internals.joinAudioCall(); + return internals.initializeContentScript(); }, - onConnect(port) { - internals.port = port; - port.onDisconnect.addListener(internals.onDisconnect); - port.onMessage.addListener(internals.onMessage); - port.postMessage({ + joinAudioCall() { + internals.port.postMessage({ action: "joinAudioCall", data: { currentUrl: internals.currentUrl, @@ -38,6 +36,13 @@ const internals = { internals.setIcon("hangUp"); }, + onConnect(port) { + internals.port = port; + port.onDisconnect.addListener(internals.onDisconnect); + port.onMessage.addListener(internals.onMessage); + internals.joinAudioCall(); + }, + onMessage(message) { if (message.action === "error") { internals.getRoot().browserAction.setBadgeText({ text: "x" }, () => {}); @@ -52,22 +57,24 @@ const internals = { internals.getRoot().browserAction.enable(); }, - async joinAudioCall() { + async initializeContentScript() { internals.getRoot().browserAction.disable(); const activeTabs = await internals.getActiveTabs(); internals.currentUrl = activeTabs[0].url; - const execution = await internals.getRoot().tabs.executeScript( - activeTabs[0].id, - { - file: "/build/content_script.js", - }, - () => { - if (internals.getRoot().runtime.lastError) { - internals.onDisconnect(); - } - }, - ); + if (!internals.injectedScript) { + const execution = await internals.getRoot().tabs.executeScript( + activeTabs[0].id, + { + file: "/build/content_script.js", + }, + () => { + internals.injectedScript = true; + }, + ); + } else { + internals.getRoot().tabs.connect(activeTabs[0].id) + } if (execution && !execution[0]) { internals.onDisconnect();