Skip to content

Commit 06179d9

Browse files
Add custom Selection Tools support to right-click context menu
Co-authored-by: PeterDaveHello <[email protected]>
1 parent ac11758 commit 06179d9

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/background/menus.mjs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const onClickMenu = (info, tab) => {
1919
type: 'CREATE_CHAT',
2020
data: message,
2121
})
22+
} else if (message.itemId.startsWith('custom_')) {
23+
// Handle custom selection tools
24+
Browser.tabs.sendMessage(currentTab.id, {
25+
type: 'CREATE_CHAT',
26+
data: message,
27+
})
2228
} else if (message.itemId in menuConfig) {
2329
if (menuConfig[message.itemId].action) {
2430
menuConfig[message.itemId].action(true, tab)
@@ -37,7 +43,8 @@ export function refreshMenu() {
3743
if (Browser.contextMenus.onClicked.hasListener(onClickMenu))
3844
Browser.contextMenus.onClicked.removeListener(onClickMenu)
3945
Browser.contextMenus.removeAll().then(async () => {
40-
if ((await getUserConfig()).hideContextMenu) return
46+
const userConfig = await getUserConfig()
47+
if (userConfig.hideContextMenu) return
4148

4249
await getPreferredLanguageKey().then((lang) => {
4350
changeLanguage(lang)
@@ -62,15 +69,32 @@ export function refreshMenu() {
6269
contexts: ['selection'],
6370
type: 'separator',
6471
})
72+
73+
// Add default selection tools that are active
6574
for (const index in defaultConfig.selectionTools) {
6675
const key = defaultConfig.selectionTools[index]
6776
const desc = defaultConfig.selectionToolsDesc[index]
68-
Browser.contextMenus.create({
69-
id: menuId + key,
70-
parentId: menuId,
71-
title: t(desc),
72-
contexts: ['selection'],
73-
})
77+
if (userConfig.activeSelectionTools.includes(key)) {
78+
Browser.contextMenus.create({
79+
id: menuId + key,
80+
parentId: menuId,
81+
title: t(desc),
82+
contexts: ['selection'],
83+
})
84+
}
85+
}
86+
87+
// Add custom selection tools that are active
88+
for (let i = 0; i < userConfig.customSelectionTools.length; i++) {
89+
const tool = userConfig.customSelectionTools[i]
90+
if (tool.active && tool.name) {
91+
Browser.contextMenus.create({
92+
id: menuId + 'custom_' + i,
93+
parentId: menuId,
94+
title: tool.name,
95+
contexts: ['selection'],
96+
})
97+
}
7498
}
7599

76100
Browser.contextMenus.onClicked.addListener(onClickMenu)

src/content-script/index.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,19 @@ async function prepareForRightClickMenu() {
284284
if (message.type === 'CREATE_CHAT') {
285285
const data = message.data
286286
let prompt = ''
287+
const userConfig = await getUserConfig()
288+
287289
if (data.itemId in toolsConfig) {
288290
prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText)
291+
} else if (data.itemId.startsWith('custom_')) {
292+
// Handle custom selection tools from context menu
293+
const customIndex = parseInt(data.itemId.replace('custom_', ''))
294+
if (customIndex >= 0 && customIndex < userConfig.customSelectionTools.length) {
295+
const customTool = userConfig.customSelectionTools[customIndex]
296+
if (customTool.active && customTool.name) {
297+
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
298+
}
299+
}
289300
} else if (data.itemId in menuConfig) {
290301
const menuItem = menuConfig[data.itemId]
291302
if (!menuItem.genPrompt) return
@@ -298,7 +309,6 @@ async function prepareForRightClickMenu() {
298309
: { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 }
299310
const container = createElementAtPosition(position.x, position.y)
300311
container.className = 'chatgptbox-toolbar-container-not-queryable'
301-
const userConfig = await getUserConfig()
302312
render(
303313
<FloatingToolbar
304314
session={initSession({

0 commit comments

Comments
 (0)