ycombinator-keys/keyboard-watcher.js
2022-09-16 11:52:40 +02:00

55 lines
1.6 KiB
JavaScript

let selectedId = -1;
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var charStr = String.fromCharCode(charCode);
switch(charStr) {
case 'j':
handlePrevious()
selectedId = selectedId + 1;
highlightSelected();
break;
case 'k':
if (selectedId > 0) {
handlePrevious()
selectedId = selectedId - 1;
}
highlightSelected();
break;
case 'h':
document.querySelectorAll(".itemlist tbody tr td.subtext .clicky")[selectedId].click();
highlightSelected();
break
case 'r':
location.reload()
case 'o':
targetBlankClickAndReset(document.querySelectorAll(".itemlist tbody tr.athing td.title a")[selectedId])
break;
case 'c':
targetBlankClickAndReset(document.querySelectorAll(".itemlist tbody tr td.subtext a:not(.clicky):not(.hnuser)")[selectedId]);
break;
}
};
function highlightSelected() {
if (selectedId != -1) {
document.querySelectorAll(".itemlist tbody tr.athing")[selectedId].style.boxShadow = "0px 0px 10px 4px rgba(0,0,0,0.73)";
}
}
function handlePrevious() {
if (selectedId != -1) {
document.querySelectorAll(".itemlist tbody tr.athing")[selectedId].style.boxShadow = "initial";
}
}
function targetBlankClickAndReset(el) {
el.target = "_blank";
setTimeout(() => {
el.click();
el.target = "";
})
}