Initial commit
authorJacob Casper <dev@jacobcasper.com>
Sun, 20 Oct 2019 23:00:25 +0000 (18:00 -0500)
committerJacob Casper <dev@jacobcasper.com>
Mon, 21 Oct 2019 08:28:26 +0000 (03:28 -0500)
README.md [new file with mode: 0644]
hnim.js [new file with mode: 0644]
hnim.xpi [new file with mode: 0644]
manifest.json [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
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 (file)
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 (file)
index 0000000..aff0e73
Binary files /dev/null and b/hnim.xpi differ
diff --git a/manifest.json b/manifest.json
new file mode 100644 (file)
index 0000000..f95615d
--- /dev/null
@@ -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"]
+    }
+  ]
+}