repeat and numbered repeats

This commit is contained in:
Utopiah
2026-09-03 08:19:15 +02:00
parent 9eea6586e8
commit 1902d46519
2 changed files with 41 additions and 5 deletions
+40 -4
View File
@@ -19,16 +19,22 @@ function onError(error) {
console.log(`Error: ${error}`)
}
// enable repeat Nth time function, e.g. 5j to move forward 5 times
let repeats = 1
let key, modifiers = []
let seq = ''
let richSeq = ''
let actionsExecuted = []
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') {
console.log('cleared by escape')
seq = ''
key = ''
modifiers = []
richSeq = ''
return
@@ -41,20 +47,29 @@ document.addEventListener("keydown", function(event) {
// 'Alt', 'Control', 'Shift'
modifiers.push( event.key )
}
console.log( key, modifiers )
console.log( key, modifiers, seq )
if (blocked) return
userActions.filter( a => a.seq == seq ).map( a => doAction( a.action ) )
// consider a regex instead
userActions.filter( a => a.regex && seq.match( a.regex )?.length).map( a => doAction( a.action ) )
// priority of seq over individual key
if (key && modifiers.length > 0 )
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 => 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)
console.log( 'repeated times x', repeats ) // for debugging
for (let i=0; i<repeats; i++) eval(action)
if (repeats > 1 && action != 'repeats = Number(seq)') { repeats = 1 } // limiting to single digit...
if ( action!="doAction(actionsExecuted.at(-1))" ) actionsExecuted.push(action)
seq = ''
key = ''
modifiers = []
}
@@ -71,6 +86,9 @@ 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 = [
{ regex: /^\d+$/, action: 'repeats = Number(seq)'},
// limited to single digits
{ regex: /^m[a-zA-Z0-9_]$/, action: 'console.log("matched regex")'},
// consider richSeq instead
{ seq: 'gtm', action: 'window.open("https://twitter-archive.benetou.fr/")'},
{ seq: 'gg', action: 'window.scrollTo(0, 0)'},
@@ -79,19 +97,33 @@ let userActions = [
// this now prevent any binding on g
{ key: 'r', action: 'window.location.reload()'},
{ key: 'x', action: 'autoscroll()'},
{ key: 'e', action: 'loadScript("https://cdn.jsdelivr.net/npm/eruda", "eruda.init()")'},
// does not work as extension but works in testing.html
{ key: 'b', action: 'history.back()'},
{ key: 'B', mods: ['Shift'], action: 'blocked = !blocked'}, // can not be unblocked, need to refresh the page
{ key: 'a', action: 'addWiki()'},
{ key: 'n', action: 'firstTab()'},
{ key: 'P', mods: ['Shift'], action: 'window.open("https://account.proton.me/")'},
{ key: 'i', action: 'window.open("https://fabien.benetou.fr")'},
// could be generalize to any bookmarks but needs to be set first, should not be hardcoded like here
// could be generalize to any bookmarks but needs to be set first, should not be hardcded like here
// for multiple ones would have to handle successive keys, e.g. gtH for go to Home
// currently handling single key with modifiers
{ key: 'h', action: 'alert("r to reload, h for help, etc")'},
{ key: 'h', mods: ['Alt'], action: 'alert("modified help!")'},
{ key: '.', action: 'doAction(actionsExecuted.at(-1))'}, // untested in the extension context, might lack scope
]
function loadScript(scriptURL, loaderCode){
let el = document.createElement('script')
el.src = scriptURL
document.head.appendChild(el)
setTimeout( _ => {
eruda.init()
// does not work either despite being loading according to the network tab
//eval( loaderCode )
}, 3000 ) // random timing, depends on networking and loading time
}
function firstTab(){
console.log( browser, browser.tabs )
// does not work, somehow browser.tabs remain undefined even with permissions accepted
@@ -128,6 +160,10 @@ function addWiki(){
var as
// see also https://twitter-archive.benetou.fr/utopiah/status/1648024456252518400/
// https://developer.mozilla.org/en-US/docs/Web/API/Device_orientation_events/Orientation_and_motion_data_explained
// https://developer.mozilla.org/en-US/docs/Web/API/Window/deviceorientation_event
//
function autoscroll(){
if (!as) {
as = setInterval( () => { window.scrollBy(0, 1) } , 100 );
+1 -1
View File
@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "keyboard",
"version": "1785936598",
"version": "1785951481",
"description": "Adds keyboard capabilities shortuts for content on your local filesystem.",