()
| 14 | * and handle this case in callers explicitly |
| 15 | */ |
| 16 | export async function getActiveTab(): Promise<chrome.tabs.Tab | null> { |
| 17 | let log: string | null = null; |
| 18 | let tab = (await queryTabs({ |
| 19 | active: true, |
| 20 | lastFocusedWindow: true, |
| 21 | // Explicitly exclude Dark Reader's Dev Tools and other special windows from the query |
| 22 | windowType: 'normal', |
| 23 | }))[0]; |
| 24 | if (!tab) { |
| 25 | tab = (await queryTabs({ |
| 26 | active: true, |
| 27 | lastFocusedWindow: true, |
| 28 | windowType: 'app', |
| 29 | }))[0]; |
| 30 | } |
| 31 | if (!tab) { |
| 32 | if (__DEBUG__ || __TEST__) { |
| 33 | log = 'method 1'; |
| 34 | } |
| 35 | // When Dark Reader's DevTools are open, last focused window might be the DevTools window |
| 36 | // so we lift this restriction and try again (with the best guess) |
| 37 | tab = (await queryTabs({ |
| 38 | active: true, |
| 39 | windowType: 'normal', |
| 40 | }))[0]; |
| 41 | } |
| 42 | if (!tab) { |
| 43 | if (__DEBUG__ || __TEST__) { |
| 44 | log = 'method 2'; |
| 45 | } |
| 46 | tab = (await queryTabs({ |
| 47 | active: true, |
| 48 | windowType: 'app', |
| 49 | }))[0]; |
| 50 | } |
| 51 | if (!tab && isFirefox && isMobile) { |
| 52 | tab = (await queryTabs({ |
| 53 | active: true, |
| 54 | }))[0]; |
| 55 | } |
| 56 | if (log) { |
| 57 | console.warn(`TabManager.getActiveTab() could not reliably find the active tab, picking the best guess ${log}`, tab); |
| 58 | } |
| 59 | // In rare cases tab can be null, despite what TypeScript says |
| 60 | return tab || null; |
| 61 | } |
| 62 | |
| 63 | export async function getActiveTabURL(): Promise<string | null> { |
| 64 | const tab = await getActiveTab(); |
no test coverage detected