mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Sample chrome.commands.extension
This commit is contained in:
13
commands/README.md
Normal file
13
commands/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# commands
|
||||
|
||||
This extension includes:
|
||||
|
||||
* a background script, "background.js"
|
||||
|
||||
All it does is:
|
||||
|
||||
* register a shortcut (Ctrl+Shift+Y) to send a command to the extension (Command+Shift+Y on a Mac).",
|
||||
|
||||
It shows:
|
||||
|
||||
* how to use chrome.commands to register keyboard shortcuts for your extension.
|
||||
27
commands/background.js
Normal file
27
commands/background.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Returns all of the registered extension commands for this extension
|
||||
* and their shortcut (if active).
|
||||
*
|
||||
* Since there is only one registered command in this sample extension,
|
||||
* the returned `commandsArray` will look like the following:
|
||||
* [{
|
||||
* name: "toggle-feature",
|
||||
* description: "Send a 'toggle-feature' event to the extension"
|
||||
* shortcut: "Ctrl+Shift+Y"
|
||||
* }]
|
||||
*/
|
||||
chrome.commands.getAll(function(commandsArray) {
|
||||
commands.forEach(function(command) {
|
||||
console.log(command);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Fired when a registered command is activated using a keyboard shortcut.
|
||||
*
|
||||
* In this sample extension, there is only one registered command: "Ctrl+Shift+Y".
|
||||
* On Mac, this command will automatically be converted to "Command+Shift+Y".
|
||||
*/
|
||||
chrome.commands.onCommand.addListener(function(command) {
|
||||
console.log("onCommand event received for message: ", command);
|
||||
});
|
||||
22
commands/manifest.json
Normal file
22
commands/manifest.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "Sample Commands Extension",
|
||||
"description": "Press Ctrl+Shift+Y to send an event (Command+Shift+Y on a Mac).",
|
||||
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/commands",
|
||||
"manifest_version": 2,
|
||||
"version": "1.0",
|
||||
"strict_min_version": "48.0a1",
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
},
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"id": "commands@mozilla.org"
|
||||
}
|
||||
},
|
||||
"commands": {
|
||||
"toggle-feature": {
|
||||
"suggested_key": { "default": "Ctrl+Shift+Y" },
|
||||
"description": "Send a 'toggle-feature' event to the extension"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user