diff --git a/kbd.js b/kbd.js index 214d1de..2e1f690 100644 --- a/kbd.js +++ b/kbd.js @@ -1,15 +1,37 @@ -// update the version number in manifest in order to be able to reload via Firefox itself console.log('extension loaded kbd'); let blocked = false -document.addEventListener("keypress", function(event) { - console.log('events ' + event); - console.log('pressed ' + event.key); + +let key, modifiers = [] +document.addEventListener("keydown", function(event) { + if (event.key.length === 1 || event.key === event.code) { + // 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc + key = event.key + } else { + // 'Alt', 'Control', 'Shift' + modifiers.push( event.key ) + } + console.log( key, modifiers ) if (blocked) return - userActions.filter( a => event.key == a.key ).map( a => eval( a.action ) ) + if (key && modifiers.length > 0 ) + userActions.filter( a => key == a.key && a.mods && a.mods[0] == modifiers[0] ).map( a => eval( 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 ) ) +}) + +document.addEventListener("keyup", function(event) { + if (event.key.length === 1 || event.key === event.code) { + // 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc + key = null + } else { + // 'Alt', 'Control', 'Shift' + modifiers = modifiers.filter(item => item !== event.key) + } }) // 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 capture as I + Shift) let userActions = [ { key: 'r', action: 'window.location.reload()'}, { key: 'x', action: 'autoscroll()'}, @@ -18,19 +40,23 @@ let userActions = [ { key: 'r', action: 'window.location.reload()'}, { key: 'h', action: 'alert("r to reload, h for help, etc")'}, // could have example with modifier, e.g. Ctrl or Shift - // or multiple modifiers + // or multiple modifiers + { key: 'h', mods: ['Alt'], action: 'alert("modified help!")'}, ] // from https://gist.github.com/Utopiah/525718c8a60ee2d9e25d7724e9dbb355 const month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ][new Date().getMonth()]; const year = new Date().getFullYear(); const url = `https://change.to.your/PIM/Notes${month}${year}`; +// const url = `https://fabien.benetou.fr/PersonalInformationStream/WithoutNotes${month}${year}`; +// should be set via options but currently not used function addWiki(){ fetch(url+'?action=source') .then( response => { return response.text() } ) .then( data => { var writeURL = url + '?action=edit'; var selection = window.getSelection().toString(); var content = "post=1&author=PIM_kdbext&authpw=change_this_edit_password&text="+data+'%0a* ' + // should be set via options but currently not used + window.location.href.replace(/\?utm_source.*/,''); if (selection) content += '%0a** ' + selection; fetch(writeURL, { method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'}, body: content })