Browse Source

fix: preload script not working in firefox

Ahmad Kholid 2 years ago
parent
commit
f254592cb0

+ 30 - 2
src/content/blocksHandler/handlerJavascriptCode.js

@@ -1,7 +1,35 @@
 import { jsContentHandler } from '@/newtab/utils/javascriptBlockUtil';
 import { jsContentHandler } from '@/newtab/utils/javascriptBlockUtil';
 
 
-function javascriptCode({ data }) {
-  return jsContentHandler(...data);
+function javascriptCode({ data, isPreloadScripts, frameSelector }) {
+  if (!isPreloadScripts) return jsContentHandler(...data);
+
+  let $documentCtx = document;
+
+  if (frameSelector) {
+    const iframeCtx = document.querySelector(frameSelector)?.contentDocument;
+    if (!iframeCtx) return Promise.resolve(false);
+
+    $documentCtx = iframeCtx;
+  }
+
+  data.scripts.forEach((script) => {
+    const scriptAttr = `block--${script.id}`;
+
+    const isScriptExists = $documentCtx.querySelector(
+      `.automa-custom-js[${scriptAttr}]`
+    );
+
+    if (isScriptExists) return;
+
+    const scriptEl = $documentCtx.createElement('script');
+    scriptEl.textContent = script.data.code;
+    scriptEl.setAttribute(scriptAttr, '');
+    scriptEl.classList.add('automa-custom-js');
+
+    $documentCtx.documentElement.appendChild(scriptEl);
+  });
+
+  return Promise.resolve(true);
 }
 }
 
 
 export default javascriptCode;
 export default javascriptCode;

+ 11 - 6
src/newtab/workflowEngine/blocksHandler/handlerActiveTab.js

@@ -41,17 +41,22 @@ async function activeTab(block) {
     }
     }
 
 
     if (this.preloadScripts.length > 0) {
     if (this.preloadScripts.length > 0) {
-      const preloadScripts = this.preloadScripts.map((script) =>
-        injectPreloadScript({
-          script,
+      if (this.engine.isMV2) {
+        await this._sendMessageToTab({
+          isPreloadScripts: true,
+          label: 'javascript-code',
+          data: { scripts: this.preloadScripts },
+        });
+      } else {
+        await injectPreloadScript({
+          scripts: this.preloadScripts,
           frameSelector: this.frameSelector,
           frameSelector: this.frameSelector,
           target: {
           target: {
             tabId: this.activeTab.id,
             tabId: this.activeTab.id,
             frameIds: [this.activeTab.frameId || 0],
             frameIds: [this.activeTab.frameId || 0],
           },
           },
-        })
-      );
-      await Promise.allSettled(preloadScripts);
+        });
+      }
     }
     }
 
 
     await browser.tabs.update(tab.id, { active: true });
     await browser.tabs.update(tab.id, { active: true });

+ 3 - 1
src/newtab/workflowEngine/blocksHandler/handlerJavascriptCode.js

@@ -54,7 +54,9 @@ export async function javascriptCode({ outputs, data, ...block }, { refData }) {
 
 
   if (data.everyNewTab) {
   if (data.everyNewTab) {
     const isScriptExist = this.preloadScripts.some(({ id }) => id === block.id);
     const isScriptExist = this.preloadScripts.some(({ id }) => id === block.id);
-    if (!isScriptExist) this.preloadScripts.push({ ...block, data });
+
+    if (!isScriptExist)
+      this.preloadScripts.push({ id: block.id, data: cloneDeep(data) });
     if (!this.activeTab.id) return { data: '', nextBlockId };
     if (!this.activeTab.id) return { data: '', nextBlockId };
   } else if (!this.activeTab.id && data.context !== 'background') {
   } else if (!this.activeTab.id && data.context !== 'background') {
     throw new Error('no-tab');
     throw new Error('no-tab');

+ 11 - 6
src/newtab/workflowEngine/blocksHandler/handlerNewTab.js

@@ -88,17 +88,22 @@ async function newTab({ id, data }) {
   }
   }
 
 
   if (this.preloadScripts.length > 0) {
   if (this.preloadScripts.length > 0) {
-    const preloadScripts = this.preloadScripts.map((script) =>
-      injectPreloadScript({
-        script,
+    if (this.engine.isMV2) {
+      await this._sendMessageToTab({
+        isPreloadScripts: true,
+        label: 'javascript-code',
+        data: { scripts: this.preloadScripts },
+      });
+    } else {
+      await injectPreloadScript({
+        scripts: this.preloadScripts,
         frameSelector: this.frameSelector,
         frameSelector: this.frameSelector,
         target: {
         target: {
           tabId: this.activeTab.id,
           tabId: this.activeTab.id,
           frameIds: [this.activeTab.frameId || 0],
           frameIds: [this.activeTab.frameId || 0],
         },
         },
-      })
-    );
-    await Promise.allSettled(preloadScripts);
+      });
+    }
   }
   }
 
 
   if (data.waitTabLoaded) {
   if (data.waitTabLoaded) {

+ 11 - 6
src/newtab/workflowEngine/blocksHandler/handlerSwitchTab.js

@@ -79,17 +79,22 @@ export default async function ({ data, id }) {
   }
   }
 
 
   if (this.preloadScripts.length > 0) {
   if (this.preloadScripts.length > 0) {
-    const preloadScripts = this.preloadScripts.map((script) =>
-      injectPreloadScript({
-        script,
+    if (this.engine.isMV2) {
+      await this._sendMessageToTab({
+        isPreloadScripts: true,
+        label: 'javascript-code',
+        data: { scripts: this.preloadScripts },
+      });
+    } else {
+      await injectPreloadScript({
+        scripts: this.preloadScripts,
         frameSelector: this.frameSelector,
         frameSelector: this.frameSelector,
         target: {
         target: {
           tabId: this.activeTab.id,
           tabId: this.activeTab.id,
           frameIds: [this.activeTab.frameId || 0],
           frameIds: [this.activeTab.frameId || 0],
         },
         },
-      })
-    );
-    await Promise.allSettled(preloadScripts);
+      });
+    }
   }
   }
 
 
   if (activeTab) {
   if (activeTab) {

+ 15 - 13
src/newtab/workflowEngine/helper.js

@@ -177,12 +177,12 @@ function automaRefData(keyword, path = '') {
   `;
   `;
 }
 }
 
 
-export function injectPreloadScript({ target, script, frameSelector }) {
+export function injectPreloadScript({ target, scripts, frameSelector }) {
   return browser.scripting.executeScript({
   return browser.scripting.executeScript({
     target,
     target,
     world: 'MAIN',
     world: 'MAIN',
-    args: [script.id, script.data.code, frameSelector || null],
-    func: (scriptId, code, frame) => {
+    args: [scripts, frameSelector || null],
+    func: (preloadScripts, frame) => {
       let $documentCtx = document;
       let $documentCtx = document;
 
 
       if (frame) {
       if (frame) {
@@ -192,20 +192,22 @@ export function injectPreloadScript({ target, script, frameSelector }) {
         $documentCtx = iframeCtx;
         $documentCtx = iframeCtx;
       }
       }
 
 
-      const scriptAttr = `block--${scriptId}`;
+      preloadScripts.forEach((script) => {
+        const scriptAttr = `block--${script.id}`;
 
 
-      const isScriptExists = $documentCtx.querySelector(
-        `.automa-custom-js[${scriptAttr}]`
-      );
+        const isScriptExists = $documentCtx.querySelector(
+          `.automa-custom-js[${scriptAttr}]`
+        );
 
 
-      if (isScriptExists) return;
+        if (isScriptExists) return;
 
 
-      const scriptEl = $documentCtx.createElement('script');
-      scriptEl.textContent = code;
-      scriptEl.setAttribute(scriptAttr, '');
-      scriptEl.classList.add('automa-custom-js');
+        const scriptEl = $documentCtx.createElement('script');
+        scriptEl.textContent = script.data.code;
+        scriptEl.setAttribute(scriptAttr, '');
+        scriptEl.classList.add('automa-custom-js');
 
 
-      $documentCtx.documentElement.appendChild(scriptEl);
+        $documentCtx.documentElement.appendChild(scriptEl);
+      });
     },
     },
   });
   });
 }
 }