X-Git-Url: https://git.jacobcasper.com/?p=hnim.git;a=blobdiff_plain;f=hnim.js;h=7ac3f5d9ee704cdbee50f6829def5178fb51eb23;hp=ead93105f2eb5b7963efe5bd35f6bfedc1852f95;hb=e10798def56ccc77315816ca54e32e623cf49dd5;hpb=cf7b74e9651507b504f690fadadd14b3580c4d37 diff --git a/hnim.js b/hnim.js index ead9310..7ac3f5d 100644 --- a/hnim.js +++ b/hnim.js @@ -1,26 +1,31 @@ (() => { const comments = document.querySelectorAll("tr.comtr"); + const items = document.querySelectorAll("table.itemlist tr.athing"); const morelink = document.querySelectorAll("a.morelink"); - const elements = [...Array.from(comments), ...Array.from(morelink)] + const elements = [ + ...Array.from(items), + ...Array.from(comments), + ...Array.from(morelink), + ]; let elementsIndex = 0; - let selectedComment = elements[elementsIndex] - selectedComment.style.outline = '1px dashed black'; + let selectedElement = elements[elementsIndex]; + selectedElement.style.outline = '1px dashed black'; const visible = (element) => { let bounds = element.getBoundingClientRect(); - return bounds.top >= 0 && bounds.bottom <= window.innerHeight; + return Math.abs(bounds.bottom - bounds.top) > window.innerHeight || (bounds.top >= 0 && bounds.bottom <= window.innerHeight); } const changeWithVisibleCallback = (comment, callback) => { if (comment == null) { return; } - selectedComment.style.outline = ''; - selectedComment = comment; - selectedComment.style.outline = '1px dashed black'; - if (!visible(comment)) { + selectedElement.style.outline = ''; + selectedElement = comment; + selectedElement.style.outline = '1px dashed black'; + while (!visible(comment)) { callback(); } } @@ -32,46 +37,48 @@ // Curry callback for moving downpage const changeDownpage = () => { - elementsIndex = Math.min(elementsIndex + 1, elements.length); - changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY + selectedComment.offsetHeight)}); + elementsIndex = Math.min(elementsIndex + 1, elements.length - 1); + changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY + selectedElement.offsetHeight)}); } // Curry callback for moving up page const changeUppage = () => { elementsIndex = Math.max(elementsIndex - 1, 0); - changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY - selectedComment.offsetHeight)}); + changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY - selectedElement.offsetHeight)}); } document.addEventListener("click", (e) => { change(e.target.closest("tr.athing")); }) document.addEventListener("keydown", (e) => { - if (e.isComposing) { + if (e.target.type === 'textarea' || e.isComposing) { return; } switch (e.key) { case "j": do { changeDownpage(); - } while (selectedComment.classList.contains("noshow")); + } while (selectedElement.classList.contains("noshow")); break; case "k": do { changeUppage(); - } while (selectedComment.classList.contains("noshow")); + } while (selectedElement.classList.contains("noshow")); break; case "m": case "Enter": - let togg = selectedComment.querySelector(".togg"); + let togg = selectedElement.querySelector(".togg"); if (togg) { togg.click(); } else if (elementsIndex == elements.length - 1) { - selectedComment.click(); + selectedElement.click(); } break; + case "o": + window.location.href = "https://news.ycombinator.com/item?id=" + selectedElement.id; case "p": - while (selectedComment.querySelector("td.ind").firstElementChild.width != 0) { + while (selectedElement.querySelector("td.ind").firstElementChild.width != 0) { changeUppage(); } break;