Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/background/menus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const onClickMenu = (info, tab) => {
type: 'CREATE_CHAT',
data: message,
})
} else if (message.itemId.startsWith('custom_')) {
// Handle custom selection tools
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)
Expand All @@ -37,7 +43,8 @@ export function refreshMenu() {
if (Browser.contextMenus.onClicked.hasListener(onClickMenu))
Browser.contextMenus.onClicked.removeListener(onClickMenu)
Browser.contextMenus.removeAll().then(async () => {
if ((await getUserConfig()).hideContextMenu) return
const userConfig = await getUserConfig()
if (userConfig.hideContextMenu) return

await getPreferredLanguageKey().then((lang) => {
changeLanguage(lang)
Expand All @@ -62,17 +69,33 @@ export function refreshMenu() {
contexts: ['selection'],
type: 'separator',
})

// Add default selection tools that are active
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'],
})
if (userConfig.activeSelectionTools.includes(key)) {
Browser.contextMenus.create({
id: menuId + key,
parentId: menuId,
title: t(desc),
contexts: ['selection'],
})
}
}

// Add custom selection tools that are active
userConfig.customSelectionTools.forEach((tool, i) => {
if (tool.active && tool.name) {
Browser.contextMenus.create({
id: menuId + 'custom_' + i,
parentId: menuId,
title: tool.name,
contexts: ['selection'],
})
}
})

Browser.contextMenus.onClicked.addListener(onClickMenu)
})
}
10 changes: 9 additions & 1 deletion src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,17 @@ async function prepareForRightClickMenu() {
if (message.type === 'CREATE_CHAT') {
const data = message.data
let prompt = ''
const userConfig = await getUserConfig()

if (data.itemId in toolsConfig) {
prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText)
} else if (data.itemId.startsWith('custom_')) {
// Handle custom selection tools from context menu
const customIndex = parseInt(data.itemId.replace('custom_', ''), 10)
const customTool = userConfig.customSelectionTools?.[customIndex]
if (customTool?.active && customTool?.name) {
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
Copy link
Preview

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parseInt result should be validated for NaN to prevent accessing invalid array indices. Add a check like if (!isNaN(customIndex) && customIndex >= 0) before using customIndex.

Suggested change
const customTool = userConfig.customSelectionTools?.[customIndex]
if (customTool?.active && customTool?.name) {
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
if (!isNaN(customIndex) && customIndex >= 0) {
const customTool = userConfig.customSelectionTools?.[customIndex]
if (customTool?.active && customTool?.name) {
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
}

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added NaN validation and bounds checking for parseInt to handle edge cases more robustly. The function now validates the parsed index before array access. (7a86413)

}
} else if (data.itemId in menuConfig) {
const menuItem = menuConfig[data.itemId]
if (!menuItem.genPrompt) return
Expand All @@ -298,7 +307,6 @@ async function prepareForRightClickMenu() {
: { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 }
const container = createElementAtPosition(position.x, position.y)
container.className = 'chatgptbox-toolbar-container-not-queryable'
const userConfig = await getUserConfig()
render(
<FloatingToolbar
session={initSession({
Expand Down
31 changes: 22 additions & 9 deletions src/popup/sections/SelectionTools.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'
import { useState } from 'react'
import { defaultConfig } from '../../config/index.mjs'
import { PencilIcon, TrashIcon } from '@primer/octicons-react'
import Browser from 'webextension-polyfill'

SelectionTools.propTypes = {
config: PropTypes.object.isRequired,
Expand Down Expand Up @@ -36,7 +37,7 @@ export function SelectionTools({ config, updateConfig }) {
{t('Cancel')}
</button>
<button
onClick={(e) => {
onClick={async (e) => {
e.preventDefault()
if (!editingTool.name) {
setErrorMessage(t('Name is required'))
Expand All @@ -47,14 +48,17 @@ export function SelectionTools({ config, updateConfig }) {
return
}
if (editingIndex === -1) {
updateConfig({
await updateConfig({
customSelectionTools: [...config.customSelectionTools, editingTool],
})
} else {
const customSelectionTools = [...config.customSelectionTools]
customSelectionTools[editingIndex] = editingTool
updateConfig({ customSelectionTools })
await updateConfig({ customSelectionTools })
}
Browser.runtime.sendMessage({
type: 'REFRESH_MENU',
})
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for Browser.runtime.sendMessage call. Add .catch(console.error) to handle potential messaging failures consistently with other similar calls in the codebase.

Suggested change
Browser.runtime.sendMessage({
type: 'REFRESH_MENU',
})
}).catch(console.error)

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

Copy link
Preview

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sendMessage call lacks error handling. Add .catch(console.error) to handle potential messaging failures, consistent with the pattern mentioned in the PR description.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

setEditing(false)
}}
>
Expand Down Expand Up @@ -102,11 +106,14 @@ export function SelectionTools({ config, updateConfig }) {
<input
type="checkbox"
checked={config.activeSelectionTools.includes(key)}
onChange={(e) => {
onChange={async (e) => {
const checked = e.target.checked
const activeSelectionTools = config.activeSelectionTools.filter((i) => i !== key)
if (checked) activeSelectionTools.push(key)
updateConfig({ activeSelectionTools })
await updateConfig({ activeSelectionTools })
Browser.runtime.sendMessage({
type: 'REFRESH_MENU',
})
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for Browser.runtime.sendMessage call. Add .catch(console.error) to handle potential messaging failures consistently with other similar calls in the codebase.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

Copy link
Preview

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sendMessage call lacks error handling. Add .catch(console.error) to handle potential messaging failures, consistent with the pattern mentioned in the PR description.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

}}
/>
{t(toolsConfig[key].label)}
Expand All @@ -122,10 +129,13 @@ export function SelectionTools({ config, updateConfig }) {
<input
type="checkbox"
checked={tool.active}
onChange={(e) => {
onChange={async (e) => {
const customSelectionTools = [...config.customSelectionTools]
customSelectionTools[index] = { ...tool, active: e.target.checked }
updateConfig({ customSelectionTools })
await updateConfig({ customSelectionTools })
Browser.runtime.sendMessage({
type: 'REFRESH_MENU',
})
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for Browser.runtime.sendMessage call. Add .catch(console.error) to handle potential messaging failures consistently with other similar calls in the codebase.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

Copy link
Preview

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sendMessage call lacks error handling. Add .catch(console.error) to handle potential messaging failures, consistent with the pattern mentioned in the PR description.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

}}
/>
{tool.name}
Expand All @@ -145,11 +155,14 @@ export function SelectionTools({ config, updateConfig }) {
</div>
<div
style={{ cursor: 'pointer' }}
onClick={(e) => {
onClick={async (e) => {
e.preventDefault()
const customSelectionTools = [...config.customSelectionTools]
customSelectionTools.splice(index, 1)
updateConfig({ customSelectionTools })
await updateConfig({ customSelectionTools })
Browser.runtime.sendMessage({
type: 'REFRESH_MENU',
})
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for Browser.runtime.sendMessage call. Add .catch(console.error) to handle potential messaging failures consistently with other similar calls in the codebase.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

Copy link
Preview

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sendMessage call lacks error handling. Add .catch(console.error) to handle potential messaging failures, consistent with the pattern mentioned in the PR description.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, address this feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted refreshContextMenu() helper function to eliminate code duplication across multiple sendMessage calls. (7a86413)

}}
>
<TrashIcon />
Expand Down