GPT-4· openai
Collective Cognition — Donated Chats
how do i add keyboard shortcuts to a site with a tampermonkey script?
- User
how do i add keyboard shortcuts to a site with a tampermonkey script?
- Assistant
Use
addEventListenerto capture keypress events. Define shortcuts inside the event listener.- Create Tampermonkey script.
- Use
document.addEventListener('keydown', function(event) { ... }).
Example:
// ==UserScript== // @name Keyboard Shortcuts // @match *://*.example.com/* // ==/UserScript== document.addEventListener('keydown', function(event) { const key = event.key; if (event.ctrlKey && key === 'a') { // Ctrl + A action console.log('Ctrl+A pressed'); } else if (event.altKey && key === 'z') { // Alt + Z action console.log('Alt+Z pressed'); } });This will add keyboard shortcuts
Ctrl+AandAlt+Zfor any page under*.example.com. Customize@matchand shortcuts as needed.