瀏覽代碼

fix: element selector doesn't work in multiple windows

Ahmad Kholid 3 年之前
父節點
當前提交
5e4161f3e2

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "automa",
-  "version": "0.3.0",
+  "version": "0.3.1",
   "description": "An extension for automating your browser by connecting blocks",
   "license": "MIT",
   "repository": {

+ 23 - 9
src/background/blocks-handler.js

@@ -137,6 +137,9 @@ export function newTab(block) {
     browser.tabs
       .create(block.data)
       .then((tab) => {
+        this.tabId = tab.id;
+        this.windowId = tab.windowId;
+
         this._listener({
           name: 'tab-updated',
           id: tab.id,
@@ -149,7 +152,6 @@ export function newTab(block) {
                 file: './contentScript.bundle.js',
               });
 
-              this.tabId = tabId;
               this._connectTab(tabId);
 
               deleteListener();
@@ -160,6 +162,7 @@ export function newTab(block) {
               });
             } catch (error) {
               console.error(error);
+              reject(error);
             }
           },
         });
@@ -186,13 +189,17 @@ export async function activeTab(block) {
       return data;
     }
 
-    const [tab] = await browser.tabs.query({ active: true });
+    const [tab] = await browser.tabs.query({
+      active: true,
+      currentWindow: true,
+    });
 
     await browser.tabs.executeScript(tab.id, {
       file: './contentScript.bundle.js',
     });
 
     this.tabId = tab.id;
+    this.windowId = tab.windowId;
     this._connectTab(tab.id);
 
     return data;
@@ -200,6 +207,7 @@ export async function activeTab(block) {
     console.error(error);
     return {
       data: '',
+      message: error.message || error,
       nextBlockId,
     };
   }
@@ -238,16 +246,22 @@ export async function takeScreenshot(block) {
         throw new Error(errorMessage('no-tab', block));
       }
 
-      const [tab] = await browser.tabs.query({ active: true });
+      const [tab] = await browser.tabs.query({
+        active: true,
+        currentWindow: true,
+      });
 
+      await browser.windows.update(this.windowId, { focused: true });
       await browser.tabs.update(this.tabId, { active: true });
 
-      setTimeout(() => {
-        browser.tabs.captureVisibleTab(options).then((uri) => {
-          browser.tabs.update(tab.id, { active: true });
-          saveImage(uri);
-        });
-      }, 500);
+      await new Promise((resolve) => setTimeout(resolve, 500));
+
+      const uri = await browser.tabs.captureVisibleTab(options);
+
+      await browser.windows.update(tab.windowId, { focused: true });
+      await browser.tabs.update(tab.id, { active: true });
+
+      saveImage(uri);
     } else {
       const uri = await browser.tabs.captureVisibleTab(options);
 

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

@@ -87,6 +87,7 @@ class WorkflowEngine {
     this.isDestroyed = false;
     this.currentBlock = null;
     this.workflowTimeout = null;
+    this.windowId = null;
 
     this.tabMessageListeners = {};
     this.tabUpdatedListeners = {};
@@ -293,7 +294,7 @@ class WorkflowEngine {
             message: error.message,
             name: tasks[block.name].name,
           });
-          console.dir(error);
+
           if (
             this.workflow.settings.onError === 'keep-running' &&
             error.nextBlockId

+ 1 - 1
src/components/block/BlockExportData.vue

@@ -21,7 +21,7 @@
     </div>
     <input
       v-model="block.data.name"
-      class="w-full bg-input rounded-lg transition mb-2 py-2 px-4 block"
+      class="bg-input rounded-lg transition w-40 mb-2 py-2 px-4 block"
       placeholder="File name"
     />
     <ui-select v-model="block.data.type" class="w-40" placeholder="Export as">

+ 1 - 1
src/popup/pages/Home.vue

@@ -115,7 +115,7 @@ function openDashboard(url) {
     });
 }
 async function selectElement() {
-  const [tab] = await browser.tabs.query({ active: true });
+  const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
 
   try {
     await browser.tabs.sendMessage(tab.id, {