forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabs.ts
More file actions
59 lines (56 loc) · 1.93 KB
/
Copy pathtabs.ts
File metadata and controls
59 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
declare const __TEST__: boolean;
declare const __DEBUG__: boolean;
// Promissified version of chrome.tabs.query
export async function queryTabs(query: chrome.tabs.QueryInfo = {}): Promise<chrome.tabs.Tab[]> {
return new Promise<chrome.tabs.Tab[]>((resolve) => chrome.tabs.query(query, resolve));
}
/**
* Attempts to find the current active tab
* Despite all efforts, sometimes active tab may not be determined so we explicitly return nullable value,
* and handle this case in callers explicitly
*/
export async function getActiveTab(): Promise<chrome.tabs.Tab | null> {
let log: string | null = null;
let tab = (await queryTabs({
active: true,
lastFocusedWindow: true,
// Explicitly exclude Dark Reader's Dev Tools and other special windows from the query
windowType: 'normal',
}))[0];
if (!tab) {
tab = (await queryTabs({
active: true,
lastFocusedWindow: true,
windowType: 'app',
}))[0];
}
if (!tab) {
if (__DEBUG__ || __TEST__) {
log = 'method 1';
}
// When Dark Reader's DevTools are open, last focused window might be the DevTools window
// so we lift this restriction and try again (with the best guess)
tab = (await queryTabs({
active: true,
windowType: 'normal',
}))[0];
}
if (!tab) {
if (__DEBUG__ || __TEST__) {
log = 'method 2';
}
tab = (await queryTabs({
active: true,
windowType: 'app',
}))[0];
}
if (log) {
console.warn(`TabManager.getActiveTab() could not reliably find the active tab, picking the best guess ${log}`, tab);
}
// In rare cases tab can be null, despite what TypeScript says
return tab || null;
}
export async function getActiveTabURL(): Promise<string | null> {
const tab = await getActiveTab();
return tab && tab.url || null;
}