Skip to content

Commit 7a86413

Browse files
Reduce code duplication and improve robustness: extract refreshContextMenu helper and add parseInt validation
Co-authored-by: PeterDaveHello <[email protected]>
1 parent ee84477 commit 7a86413

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/background/menus.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export function refreshMenu() {
8585
}
8686

8787
// Add custom selection tools that are active
88-
userConfig.customSelectionTools.forEach((tool, i) => {
89-
if (tool.active && tool.name) {
88+
userConfig.customSelectionTools?.forEach((tool, i) => {
89+
if (tool?.active && tool?.name) {
9090
Browser.contextMenus.create({
9191
id: menuId + 'custom_' + i,
9292
parentId: menuId,

src/content-script/index.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,11 @@ async function prepareForRightClickMenu() {
291291
} else if (data.itemId.startsWith('custom_')) {
292292
// Handle custom selection tools from context menu
293293
const customIndex = parseInt(data.itemId.replace('custom_', ''), 10)
294-
const customTool = userConfig.customSelectionTools?.[customIndex]
295-
if (customTool?.active && customTool?.name) {
296-
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
294+
if (!isNaN(customIndex) && customIndex >= 0) {
295+
const customTool = userConfig.customSelectionTools?.[customIndex]
296+
if (customTool?.active && customTool?.name) {
297+
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
298+
}
297299
}
298300
} else if (data.itemId in menuConfig) {
299301
const menuItem = menuConfig[data.itemId]

src/popup/sections/SelectionTools.jsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const defaultTool = {
1818
active: true,
1919
}
2020

21+
// Helper function to refresh context menu
22+
const refreshContextMenu = () => {
23+
Browser.runtime.sendMessage({
24+
type: 'REFRESH_MENU',
25+
})
26+
}
27+
2128
export function SelectionTools({ config, updateConfig }) {
2229
const { t } = useTranslation()
2330
const [editing, setEditing] = useState(false)
@@ -56,9 +63,7 @@ export function SelectionTools({ config, updateConfig }) {
5663
customSelectionTools[editingIndex] = editingTool
5764
await updateConfig({ customSelectionTools })
5865
}
59-
Browser.runtime.sendMessage({
60-
type: 'REFRESH_MENU',
61-
})
66+
refreshContextMenu()
6267
setEditing(false)
6368
}}
6469
>
@@ -111,9 +116,7 @@ export function SelectionTools({ config, updateConfig }) {
111116
const activeSelectionTools = config.activeSelectionTools.filter((i) => i !== key)
112117
if (checked) activeSelectionTools.push(key)
113118
await updateConfig({ activeSelectionTools })
114-
Browser.runtime.sendMessage({
115-
type: 'REFRESH_MENU',
116-
})
119+
refreshContextMenu()
117120
}}
118121
/>
119122
{t(toolsConfig[key].label)}
@@ -133,9 +136,7 @@ export function SelectionTools({ config, updateConfig }) {
133136
const customSelectionTools = [...config.customSelectionTools]
134137
customSelectionTools[index] = { ...tool, active: e.target.checked }
135138
await updateConfig({ customSelectionTools })
136-
Browser.runtime.sendMessage({
137-
type: 'REFRESH_MENU',
138-
})
139+
refreshContextMenu()
139140
}}
140141
/>
141142
{tool.name}
@@ -160,9 +161,7 @@ export function SelectionTools({ config, updateConfig }) {
160161
const customSelectionTools = [...config.customSelectionTools]
161162
customSelectionTools.splice(index, 1)
162163
await updateConfig({ customSelectionTools })
163-
Browser.runtime.sendMessage({
164-
type: 'REFRESH_MENU',
165-
})
164+
refreshContextMenu()
166165
}}
167166
>
168167
<TrashIcon />

0 commit comments

Comments
 (0)