comment counter links!
[Ultimately_Untrue_Thought.git] / theme / static / js / comment_counts.js
diff --git a/theme/static/js/comment_counts.js b/theme/static/js/comment_counts.js
new file mode 100644 (file)
index 0000000..d0df09b
--- /dev/null
@@ -0,0 +1,30 @@
+function placeCommentCounts() {
+    let containers = Array.from(document.querySelectorAll(".comments-link-container"));
+    let paths = containers.map(function(span) { return span.dataset.path; });
+
+    let request = new XMLHttpRequest();
+
+    request.open('POST', 'http://unremediatedgender.space/isso/count', true);
+    request.setRequestHeader("Content-Type", "application/json");
+    request.send(JSON.stringify(paths));
+
+    request.onload = function() {
+        if (this.status >= 200 && this.status < 400) {
+            let counts = JSON.parse(this.response);
+            for (let [i, container] of containers.entries()) {
+                let a = document.createElement('a');
+                let count = counts[i];
+                let pluralizer = count === 1 ? "" : "s";
+                let text = document.createTextNode(`${count} comment${pluralizer}`);
+                a.appendChild(text);
+                a.href = `${paths[i]}#isso-thread`;
+                container.appendChild(a);
+            }
+        }
+    };
+
+}
+
+document.addEventListener("DOMContentLoaded", function() {
+    placeCommentCounts();
+});