Adding Commands
Intro
Commands in Saka Key are functions that take exactly one argument - the keydown event that triggered the command. For most commands, this argument can be ignored. For example, the command that goes back in history is:
function goBack () {
window.history.go(-1);
}
Commands can optionally return the next mode. For example, the command that switches to Hints mode, to open a link in a new window is:
function openLinkInNewWindow (event) {
event.hintType = 'newWindow';
return 'Hints';
};
Attaching hintType to the event is useful because the event is passed to the next mode's (in this case Hints') onEnter
function.
Background Page Commands
Commands can message the background page if they require access to privileged APIs or persistent data. Saka Key provides a function background
explicitly for this purpose. background
takes two arguments:
- The name of the function to execute in the background page
- The argument that should be passed to this function, if any
function closeTab () {
background('closeTab');
}
The function that is called on the background page takes two arguments:
- The argument passed as the second parameter to
background
- The id of the message sender (see Mosi)
async function closeTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true });
await browser.tabs.remove(tab.id);
}
Useful Directories
- Command Mode: /src/modes/command/
- Client Commands: /src/modes/command/client/commands
- Background Commands: /src/modes/command/background/
- Command Bindings: /src/options/Keybindings
Step-by-step
- Add a client command
- If needed, add a corresponding background command
- Add a new keybinding option for your command here Add default bindings for your command to the built-in profiles here
Example
Commit a640f33a8dfc0a53b3524790c18cccf39bae0bcd shows how the "Hard Refresh Tab" command was added.