-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathmenus.mjs
More file actions
97 lines (89 loc) · 3.17 KB
/
menus.mjs
File metadata and controls
97 lines (89 loc) · 3.17 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
import Browser from 'webextension-polyfill'
import { defaultConfig, getPreferredLanguageKey, getUserConfig } from '../config/index.mjs'
import { changeLanguage, t } from 'i18next'
import { config as menuConfig } from '../content-script/menu-tools/index.mjs'
const menuId = 'ChatGPTBox-Menu'
const onClickMenu = (info, tab) => {
const itemId = info.menuItemId.replace(menuId, '')
// sidePanel.open() must be called synchronously within the user gesture handler.
// Calling it inside a Promise callback (e.g. Browser.tabs.query().then()) breaks
// Chrome's user gesture requirement and causes the error:
// "sidePanel.open() may only be called in response to a user gesture."
if (itemId === 'openSidePanel' && menuConfig.openSidePanel?.action) {
// Keep the call synchronous to preserve the user-gesture requirement,
// but observe the returned Promise so a rejected sidePanel.open() does
// not become an unhandled rejection in the background script.
const result = menuConfig.openSidePanel.action(true, tab)
if (result && typeof result.catch === 'function') {
result.catch((error) => {
console.error('failed to open side panel', error)
})
}
return
}
Browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
const currentTab = tabs[0]
const message = {
itemId,
selectionText: info.selectionText,
useMenuPosition: tab.id === currentTab.id,
}
console.debug('menu clicked', message)
if (defaultConfig.selectionTools.includes(message.itemId)) {
Browser.tabs.sendMessage(currentTab.id, {
type: 'CREATE_CHAT',
data: message,
})
} else if (message.itemId in menuConfig) {
if (menuConfig[message.itemId].action) {
menuConfig[message.itemId].action(true, tab)
}
if (menuConfig[message.itemId].genPrompt) {
Browser.tabs.sendMessage(currentTab.id, {
type: 'CREATE_CHAT',
data: message,
})
}
}
})
}
export function refreshMenu() {
if (Browser.contextMenus.onClicked.hasListener(onClickMenu))
Browser.contextMenus.onClicked.removeListener(onClickMenu)
Browser.contextMenus.removeAll().then(async () => {
if ((await getUserConfig()).hideContextMenu) return
await getPreferredLanguageKey().then((lang) => {
changeLanguage(lang)
})
Browser.contextMenus.create({
id: menuId,
title: 'ChatGPTBox',
contexts: ['all'],
})
for (const [k, v] of Object.entries(menuConfig)) {
Browser.contextMenus.create({
id: menuId + k,
parentId: menuId,
title: t(v.label),
contexts: ['all'],
})
}
Browser.contextMenus.create({
id: menuId + 'separator1',
parentId: menuId,
contexts: ['selection'],
type: 'separator',
})
for (const index in defaultConfig.selectionTools) {
const key = defaultConfig.selectionTools[index]
const desc = defaultConfig.selectionToolsDesc[index]
Browser.contextMenus.create({
id: menuId + key,
parentId: menuId,
title: t(desc),
contexts: ['selection'],
})
}
Browser.contextMenus.onClicked.addListener(onClickMenu)
})
}