sequences

This commit is contained in:
Utopiah
2026-08-05 18:14:23 +02:00
parent 89c4fba7ce
commit 86f0c98ed0
+23 -2
View File
@@ -20,10 +20,18 @@ function onError(error) {
}
let key, modifiers = []
let seq = ''
document.addEventListener("keydown", function(event) {
if (event.key.length === 1 || event.key === event.code) {
// 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc
key = event.key
if (key == 'Escape') {
seq = ''
modifiers = []
return
}
if ( event.key.length === 1) seq += key // does not take modifiers into account per key, e.g. IoT works but not ^i^o^t
} else {
// 'Alt', 'Control', 'Shift'
modifiers.push( event.key )
@@ -31,12 +39,20 @@ document.addEventListener("keydown", function(event) {
console.log( key, modifiers )
if (blocked) return
if (key && modifiers.length > 0 )
userActions.filter( a => key == a.key && a.mods && a.mods[0] == modifiers[0] ).map( a => eval( a.action ) )
userActions.filter( a => a.key && key == a.key && a.mods && a.mods[0] == modifiers[0] ).map( a => doAction( a.action ) )
// take single modifiers into account, must be more restrictve
else
userActions.filter( a => key == a.key && typeof ( a.mods ) === 'undefined' ).map( a => eval( a.action ) )
userActions.filter( a => a.key && key == a.key && typeof ( a.mods ) === 'undefined' ).map( a => doAction( a.action ) )
userActions.filter( a => a.seq == seq ).map( a => doAction( a.action ) )
})
function doAction( action ){
console.log( 'executing', action ) // for debugging
eval(action)
seq = ''
modifiers = []
}
document.addEventListener("keyup", function(event) {
if (event.key.length === 1 || event.key === event.code) {
// 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc
@@ -50,6 +66,11 @@ document.addEventListener("keyup", function(event) {
// could then grow userActions by the user themselves, save in localStorage, etc without having to rebuild extension
// note that i is different from I (which is here captured as I + Shift)
let userActions = [
{ seq: 'gtm', action: 'window.open("https://twitter-archive.benetou.fr/")'},
{ seq: 'gg', action: 'window.scrollTo(0, 0)'},
{ key: 'G', mods: ['Shift'], action: 'window.scrollTo(0, document.body.scrollHeight)'},
{ key: 'E', mods: ['Shift'], action: 'window.location += "?action=edit"'},
// this now prevent any binding on g
{ key: 'r', action: 'window.location.reload()'},
{ key: 'x', action: 'autoscroll()'},
{ key: 'b', action: 'history.back()'},