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