forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.ts
More file actions
57 lines (48 loc) · 1.91 KB
/
Copy pathruntime.ts
File metadata and controls
57 lines (48 loc) · 1.91 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
export function getRuntimeId(): string {
return chrome.runtime.id
}
export function getRuntimeName(): string {
return chrome.runtime.getManifest().name
}
export function getIconUrl(): string {
return getUrl('static/images/icon.png')
}
export function onRuntimeMessage(handler: ChromeMessageHandler): void {
// Be careful!!!
// Can't use await/async in callback parameter
chrome.runtime.onMessage.addListener((message: tt4b.mq.Request<tt4b.mq.ReqCode>, sender: chrome.runtime.MessageSender, sendResponse: tt4b.mq.Callback<tt4b.mq.ReqCode>) => {
void handler(message, sender)
.then((response: tt4b.mq.Response<tt4b.mq.ReqCode>) => {
sendResponse(response)
})
.catch((err: unknown) => {
const msg = err instanceof Error ? err.message : String(err)
console.error('onRuntimeMessage handler error', err)
sendResponse({ code: 'fail', msg })
})
// 'return true' will force chrome to wait for the response processed in the above promise.
// @see https://github.com/mozilla/webextension-polyfill/issues/130
return true
})
}
export function onInstalled(handler: (reason: ChromeOnInstalledReason) => void): void {
chrome.runtime.onInstalled.addListener(detail => handler(detail.reason))
}
export function getVersion(): string {
return chrome.runtime.getManifest().version
}
export function setUninstallURL(url: string): Promise<void> {
return new Promise(resolve => chrome.runtime.setUninstallURL(url, resolve))
}
/**
* Get the url of this extension
*
* @param path The path relative to the root directory of this extension
*/
export function getUrl(path: string): string {
return chrome.runtime.getURL(path)
}
export async function isAllowedFileSchemeAccess(): Promise<boolean> {
const res = await chrome.extension?.isAllowedFileSchemeAccess?.()
return !!res
}