|
1 | 1 | import { ExtensionContext } from '../context' |
| 2 | +import { ExtensionEvent } from '../router' |
2 | 3 |
|
3 | | -/** |
4 | | - * Stub implementation for chrome.commands API. |
5 | | - */ |
6 | 4 | export class CommandsAPI { |
| 5 | + private commandMap = new Map</* extensionId */ string, chrome.commands.Command[]>() |
| 6 | + |
7 | 7 | constructor(private ctx: ExtensionContext) { |
8 | 8 | const handle = this.ctx.router.apiHandler() |
9 | 9 | 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) |
10 | 41 | } |
11 | 42 |
|
12 | | - getAll() { |
13 | | - return [] |
| 43 | + getAll({ extension }: ExtensionEvent): chrome.commands.Command[] { |
| 44 | + return this.commandMap.get(extension.id) || [] |
14 | 45 | } |
15 | 46 | } |
0 commit comments