From: Jacob Casper Date: Sun, 20 Oct 2019 23:00:25 +0000 (-0500) Subject: Initial commit X-Git-Tag: v0.3~5 X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;h=ffe0b2f424c1a01aa565807020ae13933b1c0d4c;p=hnim.git Initial commit --- ffe0b2f424c1a01aa565807020ae13933b1c0d4c diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff7ffdb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Adds some VIM keybindings to HackerNews comments. + +Download [hnim.xpi](hnim.xpi) diff --git a/hnim.js b/hnim.js new file mode 100644 index 0000000..2ec6a73 --- /dev/null +++ b/hnim.js @@ -0,0 +1,66 @@ +(() => { + const comments = document.querySelector(".comment-tree > tbody"); + + let selectedComment = comments.firstElementChild + selectedComment.style.outline = '1px dashed black'; + + const visible = (element) => { + let bounds = element.getBoundingClientRect(); + return 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)) { + callback(); + } + } + + const change = (comment) => { + changeWithVisibleCallback(comment, () => {}); + } + + // Curry callback for moving downpage + const changeDownpage = (comment) => { + changeWithVisibleCallback(comment, () => {window.scrollTo(0, window.scrollY + comment.offsetHeight)}); + } + + // Curry callback for moving up page + const changeUppage = (comment) => { + changeWithVisibleCallback(comment, () => {window.scrollTo(0, window.scrollY - comment.offsetHeight)}); + } + + document.addEventListener("click", (e) => { + change(e.target.closest("tr.athing")); + }) + document.addEventListener("keydown", (e) => { + if (e.isComposing) { + return; + } + if (e.key == "j") { + do { + changeDownpage(selectedComment.nextElementSibling); + } while (selectedComment.classList.contains("noshow")); + } + if (e.key == "k") { + do { + changeUppage(selectedComment.previousElementSibling); + } while (selectedComment.classList.contains("noshow")); + } + if (e.key == "Enter") { + let togg = selectedComment.querySelector(".togg"); + let more = selectedComment.querySelector(".morelink"); + if (togg) { + togg.click(); + } + else if (more) { + more.click(); + } + } + }) +})(); diff --git a/hnim.xpi b/hnim.xpi new file mode 100644 index 0000000..aff0e73 Binary files /dev/null and b/hnim.xpi differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..f95615d --- /dev/null +++ b/manifest.json @@ -0,0 +1,14 @@ +{ + "manifest_version": 2, + "name": "HNim", + "version": "0.1", + + "description": "Adds some VIM keybindings to HackerNews comments", + + "content_scripts": [ + { + "matches": ["*://news.ycombinator.com/*"], + "js": ["hnim.js"] + } + ] +}