ext-commands.js (2621B)
1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim: set sts=2 sw=2 et tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 "use strict"; 8 9 ChromeUtils.defineESModuleGetters(this, { 10 ExtensionShortcuts: "resource://gre/modules/ExtensionShortcuts.sys.mjs", 11 }); 12 13 this.commands = class extends ExtensionAPIPersistent { 14 PERSISTENT_EVENTS = { 15 onCommand({ fire }) { 16 const { extension } = this; 17 const { tabManager } = extension; 18 19 let listener = (eventName, commandName) => { 20 let nativeTab = tabTracker.activeTab; 21 tabManager.addActiveTabPermission(nativeTab); 22 fire.async(commandName, tabManager.convert(nativeTab)); 23 }; 24 this.on("command", listener); 25 return { 26 unregister: () => this.off("command", listener), 27 convert(_fire) { 28 fire = _fire; 29 }, 30 }; 31 }, 32 onChanged({ fire }) { 33 let listener = (eventName, changeInfo) => { 34 fire.async(changeInfo); 35 }; 36 this.on("shortcutChanged", listener); 37 return { 38 unregister: () => this.off("shortcutChanged", listener), 39 convert(_fire) { 40 fire = _fire; 41 }, 42 }; 43 }, 44 }; 45 46 static onUninstall(extensionId) { 47 return ExtensionShortcuts.removeCommandsFromStorage(extensionId); 48 } 49 50 async onManifestEntry() { 51 let shortcuts = new ExtensionShortcuts({ 52 extension: this.extension, 53 onCommand: name => this.emit("command", name), 54 onShortcutChanged: changeInfo => this.emit("shortcutChanged", changeInfo), 55 }); 56 this.extension.shortcuts = shortcuts; 57 await shortcuts.loadCommands(); 58 await shortcuts.register(); 59 } 60 61 onShutdown() { 62 this.extension.shortcuts.unregister(); 63 } 64 65 getAPI(context) { 66 return { 67 commands: { 68 getAll: () => this.extension.shortcuts.allCommands(), 69 update: args => this.extension.shortcuts.updateCommand(args), 70 reset: name => this.extension.shortcuts.resetCommand(name), 71 openShortcutSettings: () => 72 this.extension.shortcuts.openShortcutSettings(), 73 onCommand: new EventManager({ 74 context, 75 module: "commands", 76 event: "onCommand", 77 inputHandling: true, 78 extensionApi: this, 79 }).api(), 80 onChanged: new EventManager({ 81 context, 82 module: "commands", 83 event: "onChanged", 84 extensionApi: this, 85 }).api(), 86 }, 87 }; 88 } 89 };