Browse Source

fix: selector not working on multiple windows

Ahmad Kholid 2 years ago
parent
commit
5a516e7c1b
2 changed files with 9 additions and 6 deletions
  1. 1 1
      src/components/newtab/app/AppSidebar.vue
  2. 8 5
      src/newtab/utils/elementSelector.js

+ 1 - 1
src/components/newtab/app/AppSidebar.vue

@@ -208,7 +208,7 @@ async function injectElementSelector() {
       return;
     }
 
-    await initElementSelector(tab);
+    await initElementSelector();
   } catch (error) {
     console.error(error);
   }

+ 8 - 5
src/newtab/utils/elementSelector.js

@@ -4,9 +4,16 @@ import { isXPath, sleep } from '@/utils/helper';
 const isMV2 = browser.runtime.getManifest().manifest_version === 2;
 
 async function getActiveTab() {
+  const currentWindow = await browser.windows.getCurrent();
+  if (currentWindow)
+    await browser.windows.update(currentWindow.id, { focused: false });
+
+  await sleep(200);
+
   const [tab] = await browser.tabs.query({
     active: true,
     url: '*://*/*',
+    lastFocusedWindow: true,
   });
   if (!tab) throw new Error('No active tab');
 
@@ -26,11 +33,7 @@ export async function initElementSelector(tab = null) {
   let activeTab = tab;
 
   if (!tab) {
-    const [queryTab] = await browser.tabs.query({
-      active: true,
-      url: '*://*/*',
-    });
-    activeTab = queryTab;
+    activeTab = await getActiveTab();
   }
 
   const result = await browser.tabs.sendMessage(activeTab.id, {