Browse Source

refactor: go forward and back block

Ahmad Kholid 3 years ago
parent
commit
e42e8889fd

+ 14 - 23
src/background/workflow-engine/blocks-handler/handler-forward-page.js

@@ -1,32 +1,23 @@
 import browser from 'webextension-polyfill';
 import { getBlockConnection } from '../helper';
 
-function forwardPage(block) {
-  return new Promise((resolve, reject) => {
-    const nextBlockId = getBlockConnection(block);
+export async function goBack({ outputs }) {
+  const nextBlockId = getBlockConnection({ outputs });
 
-    if (!this.activeTab.id) {
-      const error = new Error('no-tab');
-      error.nextBlockId = nextBlockId;
+  try {
+    if (!this.activeTab.id) throw new Error('no-tab');
 
-      reject(nextBlockId);
+    await browser.tabs.goForward(this.activeTab.id);
 
-      return;
-    }
+    return {
+      data: '',
+      nextBlockId,
+    };
+  } catch (error) {
+    error.nextBlockId = nextBlockId;
 
-    browser.tabs
-      .goForward(this.activeTab.id)
-      .then(() => {
-        resolve({
-          nextBlockId,
-          data: '',
-        });
-      })
-      .catch((error) => {
-        error.nextBlockId = nextBlockId;
-        reject(error);
-      });
-  });
+    throw error;
+  }
 }
 
-export default forwardPage;
+export default goBack;

+ 13 - 22
src/background/workflow-engine/blocks-handler/handler-go-back.js

@@ -1,32 +1,23 @@
 import browser from 'webextension-polyfill';
 import { getBlockConnection } from '../helper';
 
-export function goBack(block) {
-  return new Promise((resolve, reject) => {
-    const nextBlockId = getBlockConnection(block);
+export async function goBack({ outputs }) {
+  const nextBlockId = getBlockConnection({ outputs });
 
-    if (!this.activeTab.id) {
-      const error = new Error('no-tab');
-      error.nextBlockId = nextBlockId;
+  try {
+    if (!this.activeTab.id) throw new Error('no-tab');
 
-      reject(error);
+    await browser.tabs.goBack(this.activeTab.id);
 
-      return;
-    }
+    return {
+      data: '',
+      nextBlockId,
+    };
+  } catch (error) {
+    error.nextBlockId = nextBlockId;
 
-    browser.tabs
-      .goBack(this.activeTab.id)
-      .then(() => {
-        resolve({
-          nextBlockId,
-          data: '',
-        });
-      })
-      .catch((error) => {
-        error.nextBlockId = nextBlockId;
-        reject(error);
-      });
-  });
+    throw error;
+  }
 }
 
 export default goBack;

+ 2 - 24
src/background/workflow-engine/engine.js

@@ -1,7 +1,7 @@
 import browser from 'webextension-polyfill';
 import { nanoid } from 'nanoid';
 import { tasks } from '@/utils/shared';
-import { convertData } from './helper';
+import { convertData, waitTabLoaded } from './helper';
 import { toCamelCase, parseJSON, isObject, objectHasKey } from '@/utils/helper';
 import referenceData from '@/utils/reference-data';
 import executeContentScript from './execute-content-script';
@@ -330,28 +330,6 @@ class WorkflowEngine {
   }
 
   async _sendMessageToTab(payload, options = {}) {
-    const checkActiveTab = () => {
-      return new Promise((resolve, reject) => {
-        const activeTabStatus = () => {
-          browser.tabs
-            .get(this.activeTab.id)
-            .then((tab) => {
-              if (tab.status === 'loading') {
-                setTimeout(() => {
-                  activeTabStatus();
-                }, 500);
-                return;
-              }
-
-              resolve();
-            })
-            .catch(reject);
-        };
-
-        activeTabStatus();
-      });
-    };
-
     try {
       if (!this.activeTab.id) {
         const error = new Error('no-tab');
@@ -360,7 +338,7 @@ class WorkflowEngine {
         throw error;
       }
 
-      await checkActiveTab();
+      await waitTabLoaded(this.activeTab.id);
 
       this.activeTab.frames = await executeContentScript(
         this.activeTab.id,

+ 24 - 0
src/background/workflow-engine/helper.js

@@ -1,3 +1,27 @@
+export function waitTabLoaded(tabId) {
+  return new Promise((resolve, reject) => {
+    const activeTabStatus = () => {
+      chrome.tabs.get(tabId, (tab) => {
+        if (!tab) {
+          reject(new Error('no-tab'));
+          return;
+        }
+
+        if (tab.status === 'loading') {
+          setTimeout(() => {
+            activeTabStatus();
+          }, 500);
+          return;
+        }
+
+        resolve();
+      });
+    };
+
+    activeTabStatus();
+  });
+}
+
 export function convertData(data, type) {
   let result = data;
 

+ 1 - 0
src/content/element-selector/App.vue

@@ -271,6 +271,7 @@ function destroy() {
   rootElement.style.display = 'none';
 
   Object.assign(state, {
+    hide: true,
     activeTab: '',
     elSelector: '',
     isDragging: false,