Skip to content

Commit 327c967

Browse files
committed
fix(menus): observe sidePanel.open promise to avoid unhandled rejections
The synchronous menuConfig.openSidePanel.action(true, tab) call is intentional — chrome.sidePanel.open() must run inside the user-gesture callback or it throws 'sidePanel.open() may only be called in response to a user gesture'. We don't want to break that. But the action used to be async, so the chrome.sidePanel.open Promise was discarded and any rejection (e.g. an invalid tab/window combo) would surface as an unhandled rejection in the background script. Two small changes: - menu-tools/index.mjs: drop the async wrapper and return the chrome.sidePanel.open() promise directly so the caller can observe it. - background/menus.mjs: still call the action synchronously (gesture preserved) but, if it returned a thenable, attach a .catch that logs the failure instead of letting it become unhandled. Addresses review feedback from CodeRabbit and gemini-code-assist.
1 parent 792b6bc commit 327c967

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/background/menus.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ const onClickMenu = (info, tab) => {
1212
// Chrome's user gesture requirement and causes the error:
1313
// "sidePanel.open() may only be called in response to a user gesture."
1414
if (itemId === 'openSidePanel' && menuConfig.openSidePanel?.action) {
15-
menuConfig.openSidePanel.action(true, tab)
15+
// Keep the call synchronous to preserve the user-gesture requirement,
16+
// but observe the returned Promise so a rejected sidePanel.open() does
17+
// not become an unhandled rejection in the background script.
18+
const result = menuConfig.openSidePanel.action(true, tab)
19+
if (result && typeof result.catch === 'function') {
20+
result.catch((error) => {
21+
console.error('failed to open side panel', error)
22+
})
23+
}
1624
return
1725
}
1826

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ export const config = {
5959
},
6060
openSidePanel: {
6161
label: 'Open Side Panel',
62-
action: async (fromBackground, tab) => {
62+
action: (fromBackground, tab) => {
6363
console.debug('action is from background', fromBackground)
6464
if (fromBackground) {
6565
// eslint-disable-next-line no-undef
66-
chrome.sidePanel.open({ windowId: tab.windowId, tabId: tab.id })
67-
} else {
68-
// side panel is not supported
66+
return chrome.sidePanel.open({ windowId: tab.windowId, tabId: tab.id })
6967
}
68+
// side panel is not supported
69+
return undefined
7070
},
7171
},
7272
closeAllChats: {

0 commit comments

Comments
 (0)