script.js (3787B)
1 /* keyboard buttons for surf */ 2 /* Christian hahn <ch radamanthys de> wrote the original code */ 3 4 function 5 keyboardButtons() 6 { 7 const modKey = "f"; /* used to initiate keyboardButtons mode */ 8 const escKey = "Escape"; /* used to escape keyboardButtons mode (you can also release alt) */ 9 const labelStyle = ` 10 box-sizing: border-box; 11 position: absolute; 12 display: inline; 13 width: auto; 14 height: auto; 15 margin: 0; 16 z-index: 99999; 17 18 padding: 2px; 19 border: 1px solid black; 20 border-radius: 0; 21 22 color: black; 23 font-size: 10px; 24 font-weight: normal; 25 font-family: sans-serif; 26 font-decoration: none; 27 text-transform: none; 28 `; 29 const normalColor = "yellow"; 30 const highlightColor = "red"; 31 32 var labels = {}; 33 var input = ""; 34 35 function 36 updateLabelColor() 37 { 38 for (let id in labels) { 39 if (input && id.startsWith(input)) labels[id].elem.style.backgroundColor = highlightColor; 40 else labels[id].elem.style.backgroundColor = normalColor; 41 } 42 } 43 44 /* by default, this function chooses some sequence of letters, change it to what you like */ 45 function 46 numberToLabel(n) 47 { 48 ++n; 49 const alphabet = "abcdeghijklmnopqstuvwxyz"; 50 /* r is removed as it reloads keyboardButtons */ 51 52 var str = ""; 53 for (;n; n = Math.floor(n/alphabet.length)) { 54 str += alphabet[n%alphabet.length]; 55 } 56 return str; 57 } 58 59 function 60 Label(button, text) 61 { 62 this.button = button; 63 64 this.elem = document.createElement("span"); 65 this.elem.innerHTML = text; 66 this.elem.style = labelStyle; 67 this.elem.style.visibility = "hidden"; 68 const pos = this.button.getBoundingClientRect(); 69 this.elem.style.left = pos.left + scrollX + "px"; 70 this.elem.style.top = pos.top + scrollY + "px"; 71 document.body.appendChild(this.elem); 72 } 73 74 function 75 createLabels() 76 { 77 for (let id in labels) labels[id].elem.parentNode.removeChild(labels[id].elem); 78 labels = {}; 79 80 var buttons = Array.from(document.getElementsByTagName("a")) 81 .concat(Array.from(document.getElementsByTagName("button"))); 82 for (let i = 0; i < buttons.length; i++) { 83 const text = numberToLabel(i); 84 labels[text] = new Label(buttons[i], text); 85 } 86 updateLabelColor(); 87 } 88 89 /* main */ 90 createLabels(); 91 /* set key handlers */ 92 addEventListener("keydown", function (e) { 93 if (e.key === modKey) { 94 input = ""; 95 for (let id in labels) labels[id].elem.style.visibility = "visible"; 96 updateLabelColor(); 97 } else if (e.getModifierState(modKey)) { 98 if (e.key === escKey || e.key === 'r') { 99 if (e.key === 'r') createLabels(); /* reload labels */ 100 input = ""; 101 for (let id in labels) labels[id].elem.style.visibility = "hidden"; 102 } else if (e.key.length === 1) { 103 input += e.key; 104 } 105 updateLabelColor(); 106 } 107 }); 108 addEventListener("keyup", function (e) { 109 if (e.key === modKey) { 110 if (labels[input] !== undefined) { 111 labels[input].button.click(); 112 } 113 input = ""; 114 for (let id in labels) labels[id].elem.style.visibility = "hidden"; 115 } 116 }); 117 } 118 119 if (document.readyState === "complete") keyboardButtons(); 120 else document.addEventListener("readystatechange", function (e) { 121 if (e.target.readyState === "complete") keyboardButtons(); 122 }); 123