Skip to content

Commit c8bfcf3

Browse files
committed
fix(menus): observe Browser.tabs.sendMessage promises in onClickMenu
`Browser.tabs.sendMessage()` (via webextension-polyfill) returns a Promise that commonly rejects in normal extension usage — when no content script is listening yet, on restricted pages (chrome://, about:, etc.), or after an extension reload makes old content scripts stale. Both call sites in onClickMenu were leaving the returned Promise unobserved, so a rejection would surface as an unhandled rejection in the background script. Attach a .catch() to both sendMessage call sites (selectionTools branch and the menuConfig genPrompt branch), mirroring the handling already applied to Browser.tabs.query() and the menu/command actions in this PR. This completes the unhandled-rejection story for the whole onClickMenu handler. Co-Authored-By: Octopus <liyuan851277048@icloud.com>
1 parent 2a82410 commit c8bfcf3

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

src/background/menus.mjs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,18 @@ const onClickMenu = (info, tab) => {
5454
console.debug('menu clicked', message)
5555

5656
if (defaultConfig.selectionTools.includes(message.itemId)) {
57-
Browser.tabs.sendMessage(currentTab.id, {
58-
type: 'CREATE_CHAT',
59-
data: message,
60-
})
57+
// Browser.tabs.sendMessage() (via webextension-polyfill) returns a
58+
// Promise that commonly rejects (no content script listening, restricted
59+
// pages such as chrome://, stale content scripts after extension reload)
60+
// — observe it so we don't leak unhandled rejections in the background.
61+
Browser.tabs
62+
.sendMessage(currentTab.id, {
63+
type: 'CREATE_CHAT',
64+
data: message,
65+
})
66+
.catch((error) => {
67+
console.error(`failed to send CREATE_CHAT message for "${message.itemId}"`, error)
68+
})
6169
} else if (message.itemId in menuConfig) {
6270
if (menuConfig[message.itemId].action) {
6371
// Several actions in menuConfig are async (e.g. tabs/windows calls)
@@ -79,10 +87,17 @@ const onClickMenu = (info, tab) => {
7987
}
8088

8189
if (menuConfig[message.itemId].genPrompt) {
82-
Browser.tabs.sendMessage(currentTab.id, {
83-
type: 'CREATE_CHAT',
84-
data: message,
85-
})
90+
// Same rationale as the sendMessage call above — observe the Promise
91+
// so a rejected sendMessage (no content script, restricted page, etc.)
92+
// doesn't surface as an unhandled rejection in the background.
93+
Browser.tabs
94+
.sendMessage(currentTab.id, {
95+
type: 'CREATE_CHAT',
96+
data: message,
97+
})
98+
.catch((error) => {
99+
console.error(`failed to send CREATE_CHAT message for "${message.itemId}"`, error)
100+
})
86101
}
87102
}
88103
})

0 commit comments

Comments
 (0)