Skip to content

Instantly share code, notes, and snippets.

@po-miyasaka
Last active December 9, 2022 18:44
Show Gist options
  • Save po-miyasaka/24301ab9a2a0c09c34c77876c5423b48 to your computer and use it in GitHub Desktop.
Save po-miyasaka/24301ab9a2a0c09c34c77876c5423b48 to your computer and use it in GitHub Desktop.
Bookmarklet: Making Keyboard-shortcuts for an arbitrary website
javascript:
const targets = document.getElementsByClassName('css-8ushbk');
const keyBinds = [
{
key: 'ArrowRight',
defaultFunction: () => { targets[2].click() },
commandFunction: () => {},
optionFunction: () => {},
shiftFunction: () => {}
},
{
key: 'ArrowLeft',
defaultFunction: () => { targets[1].click() },
commandFunction: () => {},
optionFunction: () => {},
shiftFunction: () => {}
},
{
key: 'ArrowUp',
defaultFunction: () => { targets[0].click() },
commandFunction: () => {},
optionFunction: () => {},
shiftFunction: () => {}
},
{
key: 'ArrowDown',
defaultFunction: () => { },
commandFunction: () => {},
optionFunction: () => {},
shiftFunction: () => {}
},
];
document.addEventListener('keydown', (event) => {
console.log(event.key);
const keyData = keyBinds.filter( (keyBind) => { return keyBind.key === event.key })[0];
if (keyData == null) {
return
}
if (event.altKey) {
keyData.optionFunction()
}
else if (event.shiftKey) {
keyData.shiftFunction()
}
else if (event.metaKey) {
keyData.commandFunction()
}
else {
keyData.defaultFunction()
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment