Skip to content

Commit fe4b856

Browse files
Fix parseInt usage and add error handling for sendMessage calls
Co-authored-by: PeterDaveHello <[email protected]>
1 parent 7cd1777 commit fe4b856

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/content-script/index.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,13 @@ async function prepareForRightClickMenu() {
290290
prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText)
291291
} else if (data.itemId.startsWith('custom_')) {
292292
// Handle custom selection tools from context menu
293-
const customIndex = parseInt(data.itemId.replace('custom_', ''))
294-
if (customIndex >= 0 && customIndex < userConfig.customSelectionTools.length) {
293+
const customIndex = parseInt(data.itemId.replace('custom_', ''), 10)
294+
if (
295+
!isNaN(customIndex) &&
296+
customIndex >= 0 &&
297+
userConfig.customSelectionTools &&
298+
customIndex < userConfig.customSelectionTools.length
299+
) {
295300
const customTool = userConfig.customSelectionTools[customIndex]
296301
if (customTool.active && customTool.name) {
297302
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)

src/popup/sections/SelectionTools.jsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ export function SelectionTools({ config, updateConfig }) {
5656
customSelectionTools[editingIndex] = editingTool
5757
await updateConfig({ customSelectionTools })
5858
}
59-
Browser.runtime.sendMessage({
60-
type: 'REFRESH_MENU',
61-
})
59+
Browser.runtime
60+
.sendMessage({
61+
type: 'REFRESH_MENU',
62+
})
63+
.catch(console.error)
6264
setEditing(false)
6365
}}
6466
>
@@ -111,9 +113,11 @@ export function SelectionTools({ config, updateConfig }) {
111113
const activeSelectionTools = config.activeSelectionTools.filter((i) => i !== key)
112114
if (checked) activeSelectionTools.push(key)
113115
await updateConfig({ activeSelectionTools })
114-
Browser.runtime.sendMessage({
115-
type: 'REFRESH_MENU',
116-
})
116+
Browser.runtime
117+
.sendMessage({
118+
type: 'REFRESH_MENU',
119+
})
120+
.catch(console.error)
117121
}}
118122
/>
119123
{t(toolsConfig[key].label)}
@@ -133,9 +137,11 @@ export function SelectionTools({ config, updateConfig }) {
133137
const customSelectionTools = [...config.customSelectionTools]
134138
customSelectionTools[index] = { ...tool, active: e.target.checked }
135139
await updateConfig({ customSelectionTools })
136-
Browser.runtime.sendMessage({
137-
type: 'REFRESH_MENU',
138-
})
140+
Browser.runtime
141+
.sendMessage({
142+
type: 'REFRESH_MENU',
143+
})
144+
.catch(console.error)
139145
}}
140146
/>
141147
{tool.name}
@@ -160,9 +166,11 @@ export function SelectionTools({ config, updateConfig }) {
160166
const customSelectionTools = [...config.customSelectionTools]
161167
customSelectionTools.splice(index, 1)
162168
await updateConfig({ customSelectionTools })
163-
Browser.runtime.sendMessage({
164-
type: 'REFRESH_MENU',
165-
})
169+
Browser.runtime
170+
.sendMessage({
171+
type: 'REFRESH_MENU',
172+
})
173+
.catch(console.error)
166174
}}
167175
>
168176
<TrashIcon />

0 commit comments

Comments
 (0)