Example for Short Cut Plugin
Here is example that can be further extended.
This place of the code where we can extend the to add more custom shortcuts:
const keySequences = {
'Ctrl+J+Ctrl+C': copyPageTitleAndURLAsMarkdown,
'Ctrl+K+Ctrl+L': () => alert('Ctrl+K followed by Ctrl+L pressed!')
// Add more sequences here
};
Tamper monkey user script:
// ==UserScript==
// @name Keyboard Shortcuts
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Example of using Tampermonkey to assign keyboard shortcut sequences
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
function myNotify(msg){
console.log(msg);
alert(msg);
}
// Function to copy the current page title and URL as markdown
function copyPageTitleAndURLAsMarkdown() {
const title = document.title;
const url = window.location.href;
const markdown = `[${title}](${url})`;
navigator.clipboard.writeText(markdown).then(() => {
myNotify('Copied to clipboard: ' + markdown);
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
(function() {
'use strict';
let sequence = [];
// Configuration for key sequences and their actions
const keySequences = {
'Ctrl+J+Ctrl+C': copyPageTitleAndURLAsMarkdown,
'Ctrl+K+Ctrl+L': () => alert('Ctrl+K followed by Ctrl+L pressed!')
// Add more sequences here
};
// Function to perform actions based on sequence
function performAction(seqString) {
if (keySequences[seqString]) {
keySequences[seqString]();
}
}
// Add event listener for keydown events
document.addEventListener('keydown', function(event) {
console.log("SHORT-CUTS: ", event.key, event.ctrlKey, event.altKey, event.shiftKey);
console.log('SEQUENCE: ', sequence.join(' , ') || 'NONE');
// does not equal control
if (event.ctrlKey && event.key != 'Control') {
sequence.push('Ctrl+' + event.key.toUpperCase());
} else {
// Clear sequence on non-control key press
sequence = [];
}
if (sequence.length === 2) {
const seqString = sequence.join('+');
performAction(seqString);
// Clear sequence after action
sequence = [];
}
});
})();
Backlinks