Skip to content

Commit 792b6bc

Browse files
author
octo-patch
committed
fix: call sidePanel.open() synchronously to fix context menu side panel (fixes #857)
Chrome's sidePanel.open() must be called synchronously within a user gesture handler. Previously, the call was inside a Browser.tabs.query() Promise callback, which breaks the user gesture chain and causes: Error: sidePanel.open() may only be called in response to a user gesture. Fixed by extracting the itemId before the async query and handling the openSidePanel action synchronously at the top of the event handler, before any async operations.
1 parent 46acd1c commit 792b6bc

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/background/menus.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import { config as menuConfig } from '../content-script/menu-tools/index.mjs'
55

66
const menuId = 'ChatGPTBox-Menu'
77
const onClickMenu = (info, tab) => {
8+
const itemId = info.menuItemId.replace(menuId, '')
9+
10+
// sidePanel.open() must be called synchronously within the user gesture handler.
11+
// Calling it inside a Promise callback (e.g. Browser.tabs.query().then()) breaks
12+
// Chrome's user gesture requirement and causes the error:
13+
// "sidePanel.open() may only be called in response to a user gesture."
14+
if (itemId === 'openSidePanel' && menuConfig.openSidePanel?.action) {
15+
menuConfig.openSidePanel.action(true, tab)
16+
return
17+
}
18+
819
Browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
920
const currentTab = tabs[0]
1021
const message = {
11-
itemId: info.menuItemId.replace(menuId, ''),
22+
itemId,
1223
selectionText: info.selectionText,
1324
useMenuPosition: tab.id === currentTab.id,
1425
}

0 commit comments

Comments
 (0)