forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabGroups.ts
More file actions
84 lines (78 loc) · 2.45 KB
/
tabGroups.ts
File metadata and controls
84 lines (78 loc) · 2.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { IS_MV3 } from "@util/constant/environment"
import { handleError } from "./common"
export async function listAllGroups(): Promise<chrome.tabGroups.TabGroup[]> {
if (!chrome.tabGroups) return []
if (IS_MV3) {
try {
return chrome.tabGroups.query({})
} catch (e) {
return []
}
} else {
return new Promise(resolve => {
chrome.tabGroups.query({}, arr => {
handleError('listAllGroups')
resolve(arr ?? [])
})
})
}
}
export async function getGroup(id: number | undefined): Promise<chrome.tabGroups.TabGroup | undefined> {
if (!id) return undefined
if (!chrome.tabGroups) return undefined
try {
if (IS_MV3) {
const group = await chrome.tabGroups.get(id)
return group
} else {
return new Promise(resolve => chrome.tabGroups.get(id, g => {
handleError('getGroup')
resolve(g)
}))
}
} catch (e) {
return undefined
}
}
export function onChanged(handler: ArgCallback<chrome.tabGroups.TabGroup>): void {
try {
if (!chrome.tabGroups) return
chrome.tabGroups.onCreated.addListener(handler)
chrome.tabGroups.onRemoved.addListener(handler)
chrome.tabGroups.onUpdated.addListener(handler)
} catch (e) {
// ignored
}
}
export function removeChangedHandler(handler: ArgCallback<chrome.tabGroups.TabGroup>): void {
try {
if (!chrome.tabGroups) return
chrome.tabGroups.onCreated.removeListener(handler)
chrome.tabGroups.onRemoved.removeListener(handler)
chrome.tabGroups.onUpdated.removeListener(handler)
} catch (e) {
// ignored
}
}
export function isValidGroup(groupId?: number): groupId is number {
if (!groupId) return false
try {
return !!groupId && groupId !== chrome.tabGroups.TAB_GROUP_ID_NONE
} catch {
return false
}
}
export const cvtGroupColor = (color?: `${chrome.tabGroups.Color}`): string => {
switch (color) {
case 'grey': return '#5F6369'
case 'blue': return '#1974E8'
case 'yellow': return '#F9AB03'
case 'red': return '#DA3025'
case 'green': return '#198139'
case 'pink': return '#D01984'
case 'purple': return '#A143F5'
case 'cyan': return '#027B84'
case 'orange': return '#FA913E'
default: return '#000'
}
}