Skip to content

Commit f4eeaa5

Browse files
committed
feat: commands.getAll
1 parent 6a795c3 commit f4eeaa5

File tree

1 file changed

+36
-5
lines changed
  • packages/electron-chrome-extensions/src/browser/api

1 file changed

+36
-5
lines changed
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
import { ExtensionContext } from '../context'
2+
import { ExtensionEvent } from '../router'
23

3-
/**
4-
* Stub implementation for chrome.commands API.
5-
*/
64
export class CommandsAPI {
5+
private commandMap = new Map</* extensionId */ string, chrome.commands.Command[]>()
6+
77
constructor(private ctx: ExtensionContext) {
88
const handle = this.ctx.router.apiHandler()
99
handle('commands.getAll', this.getAll)
10+
11+
ctx.session.on('extension-loaded', (_event, extension) => {
12+
this.processExtension(extension)
13+
})
14+
15+
ctx.session.on('extension-unloaded', (_event, extension) => {
16+
this.removeCommands(extension)
17+
})
18+
}
19+
20+
private processExtension(extension: Electron.Extension) {
21+
const manifest: chrome.runtime.Manifest = extension.manifest
22+
if (!manifest.commands) return
23+
24+
if (!this.commandMap.has(extension.id)) {
25+
this.commandMap.set(extension.id, [])
26+
}
27+
const commands = this.commandMap.get(extension.id)!
28+
29+
for (const [name, details] of Object.entries(manifest.commands!)) {
30+
// TODO: attempt to register commands
31+
commands.push({
32+
name,
33+
description: details.description,
34+
shortcut: '',
35+
})
36+
}
37+
}
38+
39+
private removeCommands(extension: Electron.Extension) {
40+
this.commandMap.delete(extension.id)
1041
}
1142

12-
getAll() {
13-
return []
43+
getAll({ extension }: ExtensionEvent): chrome.commands.Command[] {
44+
return this.commandMap.get(extension.id) || []
1445
}
1546
}

0 commit comments

Comments
 (0)