Skip to content

Commit 810e8d3

Browse files
author
octo-patch
committed
fix(menus): observe sidePanel.open promise in command handler & guard against missing chrome.sidePanel
Addresses two follow-up review findings on PR #963: 1. devin-ai-integration[bot]: src/background/commands.mjs:15 had the same unhandled-rejection pattern as menus.mjs. The keyboard command path now mirrors the menus.mjs treatment: call the action synchronously to preserve user-gesture context, then attach a .catch() to the returned thenable so a rejected sidePanel.open() does not bubble up as an unhandled rejection. 2. coderabbitai[bot]: src/content-script/menu-tools/index.mjs:62-69 called chrome.sidePanel.open directly, which throws synchronously in browsers where chrome.sidePanel is not defined (e.g. Firefox). Guard the call with a typeof chrome / chrome.sidePanel check and return a rejected Promise so the caller's .catch() handles it uniformly with API rejections.
1 parent 327c967 commit 810e8d3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/background/commands.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ export function registerCommands() {
1212

1313
if (command in menuConfig) {
1414
if (menuConfig[command].action) {
15-
menuConfig[command].action(true, tab)
15+
// The action may return a Promise (e.g. openSidePanel returns the
16+
// chrome.sidePanel.open() Promise). Keep the call synchronous so the
17+
// user-gesture context is preserved, but observe the Promise so a
18+
// rejection does not become an unhandled rejection in the background.
19+
const result = menuConfig[command].action(true, tab)
20+
if (result && typeof result.catch === 'function') {
21+
result.catch((error) => {
22+
console.error(`failed to run command action "${command}"`, error)
23+
})
24+
}
1625
}
1726

1827
if (menuConfig[command].genPrompt) {

src/content-script/menu-tools/index.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export const config = {
6262
action: (fromBackground, tab) => {
6363
console.debug('action is from background', fromBackground)
6464
if (fromBackground) {
65+
// eslint-disable-next-line no-undef
66+
if (typeof chrome === 'undefined' || !chrome.sidePanel?.open) {
67+
// sidePanel API is not available in this browser (e.g. Firefox)
68+
return Promise.reject(new Error('chrome.sidePanel API is not available'))
69+
}
6570
// eslint-disable-next-line no-undef
6671
return chrome.sidePanel.open({ windowId: tab.windowId, tabId: tab.id })
6772
}

0 commit comments

Comments
 (0)