Просмотр исходного кода

fix: fix get the active tab from extension dashborad window

While user want to get active tab from extension dashboard window (such as trigger CSS selector),
the extension dashboard window is always the only focused one, so that original code could not get the active tab correctly.

But via the [`windows.getLastFocused()`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/getLastFocused) api
with query the "normal" type window will exactly get the correctly active tab.
zthxxx 1 год назад
Родитель
Сommit
62489659d1
2 измененных файлов с 8 добавлено и 17 удалено
  1. 1 0
      src/manifest.chrome.json
  2. 7 17
      src/utils/helper.js

+ 1 - 0
src/manifest.chrome.json

@@ -55,6 +55,7 @@
   ],
   "permissions": [
     "tabs",
+    "activeTab",
     "proxy",
     "alarms",
     "storage",

+ 7 - 17
src/utils/helper.js

@@ -2,29 +2,19 @@ import browser from 'webextension-polyfill';
 
 export async function getActiveTab() {
   try {
-    let windowId = null;
     const tabsQuery = {
       active: true,
       url: '*://*/*',
     };
-    const extURL = browser.runtime.getURL('');
-    const windows = await browser.windows.getAll({ populate: true });
-    for (const browserWindow of windows) {
-      const [tab] = browserWindow.tabs;
-      const isDashboard =
-        browserWindow.tabs.length === 1 && tab.url?.includes(extURL);
-
-      if (isDashboard) {
-        await browser.windows.update(browserWindow.id, {
-          focused: false,
-        });
-      } else if (browserWindow.focused) {
-        windowId = browserWindow.id;
-      }
-    }
+
+    const window = await browser.windows.getLastFocused({
+      populate: true,
+      windowTypes: ['normal'],
+    });
+    const windowId = window.id;
 
     if (windowId) tabsQuery.windowId = windowId;
-    else if (windows.length > 2) tabsQuery.lastFocusedWindow = true;
+    else tabsQuery.lastFocusedWindow = true;
 
     const [tab] = await browser.tabs.query(tabsQuery);