forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-menu.ts
More file actions
34 lines (30 loc) · 1.02 KB
/
context-menu.ts
File metadata and controls
34 lines (30 loc) · 1.02 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
import { IS_ANDROID } from "@util/constant/environment"
import { handleError } from "./common"
function onClick(id: string, handler: Function) {
if (IS_ANDROID) {
return
}
chrome.contextMenus?.onClicked?.addListener(({ menuItemId }) => menuItemId === id && handler?.())
}
export async function createContextMenu(props: ChromeContextMenuCreateProps): Promise<void> {
const { id, onclick: clickHandler } = props
if (IS_ANDROID || !id) {
return
}
// Add listener by param
delete props.onclick
return new Promise(resolve => chrome.contextMenus?.create?.(props, () => {
handleError('createContextMenu')
clickHandler && onClick(id, clickHandler)
resolve()
}))
}
export async function updateContextMenu(menuId: string, props: ChromeContextMenuUpdateProps): Promise<void> {
if (IS_ANDROID) {
return
}
return new Promise(resolve => chrome.contextMenus?.update?.(menuId, props, () => {
handleError('updateContextMenu')
resolve()
}))
}