-
Notifications
You must be signed in to change notification settings - Fork 715
/
menus.mjs
78 lines (72 loc) · 2.34 KB
/
menus.mjs
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
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) => {
Browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
const currentTab = tabs[0]
const message = {
itemId: info.menuItemId.replace(menuId, ''),
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)
})
}