-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathcommands.mjs
More file actions
36 lines (33 loc) · 1.28 KB
/
commands.mjs
File metadata and controls
36 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Browser from 'webextension-polyfill'
import { config as menuConfig } from '../content-script/menu-tools/index.mjs'
export function registerCommands() {
Browser.commands.onCommand.addListener(async (command, tab) => {
const message = {
itemId: command,
selectionText: '',
useMenuPosition: false,
}
console.debug('command triggered', message)
if (command in menuConfig) {
if (menuConfig[command].action) {
// The action may return a Promise (e.g. openSidePanel returns the
// chrome.sidePanel.open() Promise). Keep the call synchronous so the
// user-gesture context is preserved, but observe the Promise so a
// rejection does not become an unhandled rejection in the background.
const result = menuConfig[command].action(true, tab)
if (result && typeof result.catch === 'function') {
result.catch((error) => {
console.error(`failed to run command action "${command}"`, error)
})
}
}
if (menuConfig[command].genPrompt) {
const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0]
Browser.tabs.sendMessage(currentTab.id, {
type: 'CREATE_CHAT',
data: message,
})
}
}
})
}