forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.ts
More file actions
138 lines (124 loc) · 4.41 KB
/
tab.ts
File metadata and controls
138 lines (124 loc) · 4.41 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2023-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { IS_MV3 } from '@util/constant/environment'
import { handleError } from "./common"
export function getTab(id: number): Promise<ChromeTab | undefined> {
if (id < 0) {
return Promise.resolve(undefined)
}
return new Promise(resolve => chrome.tabs.get(id, tab => {
handleError("getTab")
resolve(tab)
}))
}
export function resetTabUrl(tabId: number, url: string): Promise<void> {
return new Promise(resolve => chrome.tabs.update(tabId, {
url: url,
highlighted: true,
}, () => resolve()))
}
export async function getRightOf(target: ChromeTab): Promise<ChromeTab | undefined> {
if (!target) return
const { windowId, index } = target
return new Promise(resolve => chrome.tabs.query({ windowId }, tabs => {
const rightTab = tabs
?.sort?.((a, b) => (a?.index ?? -1) - (b?.index ?? -1))
?.filter?.(t => t.index > index)
?.[0]
resolve(rightTab)
}))
}
export function getCurrentTab(): Promise<ChromeTab | undefined> {
return new Promise(resolve => chrome.tabs.getCurrent(tab => {
handleError("getCurrentTab")
resolve(tab)
}))
}
export function createTab(param: chrome.tabs.CreateProperties | string): Promise<ChromeTab> {
const prop: chrome.tabs.CreateProperties = typeof param === 'string' ? { url: param } : param
return new Promise(resolve => chrome.tabs.create(prop, tab => {
handleError("getTab")
resolve(tab)
}))
}
/**
* Create one tab after current tab.
*
* Must not be invoked in background.js
*/
export async function createTabAfterCurrent(url: string, currentTab?: ChromeTab): Promise<ChromeTab> {
if (!currentTab) {
currentTab = await getCurrentTab()
}
if (!currentTab) {
// Current tab not found
return createTab(url)
} else {
const { windowId, index: currentIndex } = currentTab
return createTab({
url,
windowId,
index: (currentIndex ?? -1) + 1
})
}
}
export function listTabs(query?: chrome.tabs.QueryInfo): Promise<ChromeTab[]> {
query = query || {}
return new Promise(resolve => chrome.tabs.query(query, tabs => {
handleError("listTabs")
resolve(tabs || [])
}))
}
export function sendMsg2Tab<T = any, R = any>(tabId: number, code: timer.mq.ReqCode, data?: T): Promise<R> {
const request: timer.mq.Request<T> = { code, data }
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject('sendMsg2Tab timeout'), 2000)
chrome.tabs.sendMessage<timer.mq.Request<T>, timer.mq.Response>(tabId, request, response => {
const sendError = handleError('sendMsg2Tab')
clearTimeout(timeout)
const resCode = response?.code
resCode === 'success' && resolve(response.data)
reject(new Error(response?.msg ?? sendError ?? 'Unknown error'))
})
})
}
export async function trySendMsg2Tab<T = any, R = any>(
tabId: number,
code: timer.mq.ReqCode,
data?: T
): Promise<R | undefined> {
try {
return await sendMsg2Tab<T, R>(tabId, code, data)
} catch (e) {
console.warn(`Errored to send message to tab: tabId=${tabId}, code=${code}, data=${JSON.stringify(data)}`, e)
return Promise.resolve(undefined)
}
}
type TabHandler<Event> = (tabId: number, ev: Event, tab: ChromeTab) => void
export function onTabActivated(handler: (tabId: number, info: ChromeTabActiveInfo) => void): void {
chrome.tabs.onActivated.addListener(activeInfo => {
handleError("tabActivated")
handler(activeInfo.tabId, activeInfo)
})
}
export function onTabUpdated(handler: TabHandler<ChromeTabUpdatedInfo>): void {
chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: ChromeTabUpdatedInfo, tab: ChromeTab) => {
handleError("tabUpdated")
handler(tabId, changeInfo, tab)
})
}
export function updateTab(tabId: number, updateProperties: chrome.tabs.UpdateProperties): Promise<ChromeTab | undefined> {
if (IS_MV3) {
return chrome.tabs.update(tabId, updateProperties)
}
return new Promise((resolve) => {
chrome.tabs.update(tabId, updateProperties, (tab) => {
handleError("updateTab")
resolve(tab)
})
})
}