| 1 | (() => { |
| 2 | const comments = document.querySelectorAll("tr.comtr"); |
| 3 | const morelink = document.querySelectorAll("a.morelink"); |
| 4 | |
| 5 | const elements = [...Array.from(comments), ...Array.from(morelink)] |
| 6 | |
| 7 | let elementsIndex = 0; |
| 8 | let selectedComment = elements[elementsIndex] |
| 9 | selectedComment.style.outline = '1px dashed black'; |
| 10 | |
| 11 | const visible = (element) => { |
| 12 | let bounds = element.getBoundingClientRect(); |
| 13 | return bounds.top >= 0 && bounds.bottom <= window.innerHeight; |
| 14 | } |
| 15 | |
| 16 | const changeWithVisibleCallback = (comment, callback) => { |
| 17 | if (comment == null) { |
| 18 | return; |
| 19 | } |
| 20 | selectedComment.style.outline = ''; |
| 21 | selectedComment = comment; |
| 22 | selectedComment.style.outline = '1px dashed black'; |
| 23 | if (!visible(comment)) { |
| 24 | callback(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | const change = (comment) => { |
| 29 | elementsIndex = elements.indexOf(comment); |
| 30 | changeWithVisibleCallback(comment, () => {}); |
| 31 | } |
| 32 | |
| 33 | // Curry callback for moving downpage |
| 34 | const changeDownpage = () => { |
| 35 | elementsIndex = Math.min(elementsIndex + 1, elements.length); |
| 36 | changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY + selectedComment.offsetHeight)}); |
| 37 | } |
| 38 | |
| 39 | // Curry callback for moving up page |
| 40 | const changeUppage = () => { |
| 41 | elementsIndex = Math.max(elementsIndex - 1, 0); |
| 42 | changeWithVisibleCallback(elements[elementsIndex], () => {window.scrollTo(0, window.scrollY - selectedComment.offsetHeight)}); |
| 43 | } |
| 44 | |
| 45 | document.addEventListener("click", (e) => { |
| 46 | change(e.target.closest("tr.athing")); |
| 47 | }) |
| 48 | document.addEventListener("keydown", (e) => { |
| 49 | if (e.isComposing) { |
| 50 | return; |
| 51 | } |
| 52 | switch (e.key) { |
| 53 | case "j": |
| 54 | do { |
| 55 | changeDownpage(); |
| 56 | } while (selectedComment.classList.contains("noshow")); |
| 57 | break; |
| 58 | case "k": |
| 59 | do { |
| 60 | changeUppage(); |
| 61 | } while (selectedComment.classList.contains("noshow")); |
| 62 | break; |
| 63 | case "m": |
| 64 | case "Enter": |
| 65 | let togg = selectedComment.querySelector(".togg"); |
| 66 | if (togg) { |
| 67 | togg.click(); |
| 68 | } |
| 69 | else if (elementsIndex == elements.length - 1) { |
| 70 | selectedComment.click(); |
| 71 | } |
| 72 | break; |
| 73 | case "p": |
| 74 | while (selectedComment.querySelector("td.ind").firstElementChild.width != 0) { |
| 75 | changeUppage(); |
| 76 | } |
| 77 | break; |
| 78 | default: |
| 79 | //noop |
| 80 | } |
| 81 | }) |
| 82 | })(); |