]> git.r.bdr.sh - rbdr/junction/blobdiff - extension/junction.js
Inject script only once
[rbdr/junction] / extension / junction.js
index 3e8b4d9e4996b50c738d476df389573ad0e4d09d..776096927473e635738e8577f8988675b7e9645e 100644 (file)
@@ -1,5 +1,6 @@
 const internals = {
   promisesSupported: !!window.browser,
 const internals = {
   promisesSupported: !!window.browser,
+  injectedScript: false,
   port: null,
   currentUrl: null,
 
   port: null,
   currentUrl: null,
 
@@ -20,14 +21,11 @@ const internals = {
       return internals.hangUp();
     }
 
       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,
       action: "joinAudioCall",
       data: {
         currentUrl: internals.currentUrl,
@@ -38,6 +36,13 @@ const internals = {
     internals.setIcon("hangUp");
   },
 
     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" }, () => {});
   onMessage(message) {
     if (message.action === "error") {
       internals.getRoot().browserAction.setBadgeText({ text: "x" }, () => {});
@@ -52,22 +57,24 @@ const internals = {
     internals.getRoot().browserAction.enable();
   },
 
     internals.getRoot().browserAction.enable();
   },
 
-  async joinAudioCall() {
+  async initializeContentScript() {
     internals.getRoot().browserAction.disable();
     const activeTabs = await internals.getActiveTabs();
 
     internals.currentUrl = activeTabs[0].url;
     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();
 
     if (execution && !execution[0]) {
       internals.onDisconnect();