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
125 lines (113 loc) · 4.08 KB
/
Copy pathtab.ts
File metadata and controls
125 lines (113 loc) · 4.08 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
/**
* 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)
}))
}
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<C extends tt4b.tab.ReqCode>(tabId: number, code: C, data?: tt4b.tab.ReqData<C>): Promise<tt4b.tab.ResData<C> | undefined> {
const request: tt4b.tab.Request<C> = { code, data: data as tt4b.tab.ReqData<C> }
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject('sendMsg2Tab timeout'), 2000)
chrome.tabs.sendMessage<tt4b.tab.Request<C>, tt4b.tab.Response<C>>(tabId, request, response => {
const sendError = handleError('sendMsg2Tab')
clearTimeout(timeout)
if (response?.code === 'success') {
resolve(response.data as tt4b.tab.ResData<C> | undefined)
return
}
if (response?.code === 'fail') {
reject(new Error(response.msg ?? sendError ?? 'Unknown error'))
return
}
reject(new Error(sendError ?? 'Unknown error'))
})
})
}
export async function trySendMsg2Tab<C extends tt4b.tab.ReqCode>(
tabId: number,
code: C,
data?: tt4b.tab.ReqData<C>
): Promise<tt4b.tab.ResData<C> | undefined> {
try {
return await sendMsg2Tab(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)
})
})
}