Browse Source

feat: run JS before page load option

Ahmad Kholid 2 years ago
parent
commit
4b8ee59166

+ 1 - 1
src/background/workflowEngine/blocksHandler/handlerCreateElement.js

@@ -27,7 +27,7 @@ async function handleCreateElement(block, { refData }) {
     payload.refData = { ...refData, secrets: {} };
     payload.refData = { ...refData, secrets: {} };
   }
   }
 
 
-  await this._sendMessageToTab(payload);
+  await this._sendMessageToTab(payload, {}, data.runBeforeLoad ?? false);
 
 
   return {
   return {
     data: '',
     data: '',

+ 5 - 1
src/background/workflowEngine/blocksHandler/handlerJavascriptCode.js

@@ -24,7 +24,11 @@ export async function javascriptCode({ outputs, data, ...block }, { refData }) {
   if (!data.code.includes('automaNextBlock'))
   if (!data.code.includes('automaNextBlock'))
     payload.data.code += `\nautomaNextBlock()`;
     payload.data.code += `\nautomaNextBlock()`;
 
 
-  const result = await this._sendMessageToTab(payload);
+  const result = await this._sendMessageToTab(
+    payload,
+    {},
+    data.runBeforeLoad ?? false
+  );
   if (result) {
   if (result) {
     if (result.columns.data?.$error) {
     if (result.columns.data?.$error) {
       throw new Error(result.columns.data.message);
       throw new Error(result.columns.data.message);

+ 1 - 1
src/background/workflowEngine/injectContentScript.js

@@ -30,7 +30,7 @@ export default function (tabId, frameId = 0) {
 
 
         await browser.tabs.executeScript(tabId, {
         await browser.tabs.executeScript(tabId, {
           allFrames: true,
           allFrames: true,
-          runAt: 'document_end',
+          runAt: 'document_start',
           file: './contentScript.bundle.js',
           file: './contentScript.bundle.js',
         });
         });
         const isScriptExists = await contentScriptExist(tabId, currentFrameId);
         const isScriptExists = await contentScriptExist(tabId, currentFrameId);

+ 13 - 7
src/background/workflowEngine/worker.js

@@ -299,7 +299,7 @@ class Worker {
     };
     };
   }
   }
 
 
-  async _sendMessageToTab(payload, options = {}) {
+  async _sendMessageToTab(payload, options = {}, runBeforeLoad = false) {
     try {
     try {
       if (!this.activeTab.id) {
       if (!this.activeTab.id) {
         const error = new Error('no-tab');
         const error = new Error('no-tab');
@@ -308,10 +308,12 @@ class Worker {
         throw error;
         throw error;
       }
       }
 
 
-      await waitTabLoaded({
-        tabId: this.activeTab.id,
-        ms: this.settings?.tabLoadTimeout ?? 30000,
-      });
+      if (!runBeforeLoad) {
+        await waitTabLoaded({
+          tabId: this.activeTab.id,
+          ms: this.settings?.tabLoadTimeout ?? 30000,
+        });
+      }
 
 
       const { executedBlockOnWeb, debugMode } = this.settings;
       const { executedBlockOnWeb, debugMode } = this.settings;
       const messagePayload = {
       const messagePayload = {
@@ -333,7 +335,7 @@ class Worker {
       return data;
       return data;
     } catch (error) {
     } catch (error) {
       console.error(error);
       console.error(error);
-      const noConnection = error.message?.startsWith(
+      const noConnection = error.message?.includes(
         'Could not establish connection'
         'Could not establish connection'
       );
       );
       const channelClosed = error.message?.includes('message channel closed');
       const channelClosed = error.message?.includes('message channel closed');
@@ -345,7 +347,11 @@ class Worker {
         );
         );
 
 
         if (isScriptInjected) {
         if (isScriptInjected) {
-          const result = await this._sendMessageToTab(payload, options);
+          const result = await this._sendMessageToTab(
+            payload,
+            options,
+            runBeforeLoad
+          );
           return result;
           return result;
         }
         }
         error.message = 'Could not establish connection to the active tab';
         error.message = 'Could not establish connection to the active tab';

+ 7 - 0
src/components/newtab/workflow/edit/EditCreateElement.vue

@@ -14,6 +14,13 @@
         {{ $t(`workflow.blocks.create-element.insertEl.items.${item}`) }}
         {{ $t(`workflow.blocks.create-element.insertEl.items.${item}`) }}
       </option>
       </option>
     </ui-select>
     </ui-select>
+    <ui-checkbox
+      :model-value="data.runBeforeLoad"
+      class="mt-2"
+      @change="updateData({ runBeforeLoad: $event })"
+    >
+      Run before page loaded
+    </ui-checkbox>
     <ui-button
     <ui-button
       variant="accent"
       variant="accent"
       class="w-full mt-4"
       class="w-full mt-4"

+ 7 - 0
src/components/newtab/workflow/edit/EditJavascriptCode.vue

@@ -32,6 +32,13 @@
     >
     >
       {{ t('workflow.blocks.javascript-code.everyNewTab') }}
       {{ t('workflow.blocks.javascript-code.everyNewTab') }}
     </ui-checkbox>
     </ui-checkbox>
+    <ui-checkbox
+      :model-value="data.runBeforeLoad"
+      class="mt-2"
+      @change="updateData({ runBeforeLoad: $event })"
+    >
+      Run before page loaded
+    </ui-checkbox>
     <ui-modal v-model="state.showCodeModal" content-class="max-w-3xl">
     <ui-modal v-model="state.showCodeModal" content-class="max-w-3xl">
       <template #header>
       <template #header>
         <ui-tabs v-model="state.activeTab" class="border-none">
         <ui-tabs v-model="state.activeTab" class="border-none">

+ 2 - 0
src/utils/shared.js

@@ -530,6 +530,7 @@ export const tasks = {
       code: 'console.log("Hello world!");\nautomaNextBlock()',
       code: 'console.log("Hello world!");\nautomaNextBlock()',
       preloadScripts: [],
       preloadScripts: [],
       everyNewTab: false,
       everyNewTab: false,
+      runBeforeLoad: false,
     },
     },
   },
   },
   'trigger-event': {
   'trigger-event': {
@@ -1208,6 +1209,7 @@ export const tasks = {
       preloadScripts: [],
       preloadScripts: [],
       findBy: 'cssSelector',
       findBy: 'cssSelector',
       insertAt: 'after',
       insertAt: 'after',
+      runBeforeLoad: false,
       waitForSelector: false,
       waitForSelector: false,
       waitSelectorTimeout: 5000,
       waitSelectorTimeout: 5000,
       selector: 'body',
       selector: 'body',