« get me outta code hell

scratch-forums-no-reply-hider - scratch-userscripts - Handy userscripts for the Scratch website - issues/ideas: https://notabug.org/towerofnix/scratch-userscripts/issues
about summary refs log tree commit diff
path: root/scratch-forums-no-reply-hider.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-08-08 18:08:46 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-08 18:08:46 -0300
commit135ebe8cbad8ae5e700762d8c68c1a8985af44b3 (patch)
tree2cbf640da9a69718f50054f76e4e9cac72c5820d /scratch-forums-no-reply-hider.js
parenta4036f238d855d88542eeeb57792f991892772f8 (diff)
scratch-forums-no-reply-hider
Diffstat (limited to 'scratch-forums-no-reply-hider.js')
-rw-r--r--scratch-forums-no-reply-hider.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/scratch-forums-no-reply-hider.js b/scratch-forums-no-reply-hider.js
new file mode 100644
index 0000000..0b02b2e
--- /dev/null
+++ b/scratch-forums-no-reply-hider.js
@@ -0,0 +1,46 @@
+// ==UserScript==
+// @name Hide Unreplied Posts
+// @namespace Violentmonkey Scripts
+// @match https://scratch.mit.edu/discuss/57/*
+// @grant none
+// ==/UserScript==
+
+let numRemoved = 0, span
+
+const observer = new MutationObserver(mutations => {
+  let dirty = false
+  for (const mutation of mutations) {
+    for (const node of mutation.addedNodes) {
+      if (!node.matches) {
+        continue
+      }
+
+      if (node.matches('.box-content tr')) {
+        const replyCount = parseInt(node.querySelector('.tc2').textContent)
+        if (replyCount === 0) {
+          node.remove()
+          numRemoved++
+          dirty = true
+        }
+      }
+
+      if (!span && node.matches('.box-head h4')) {
+        span = document.createElement('span')
+        span.style.color = 'red'
+        node.appendChild(span)
+      }
+    }
+  }
+
+  if (span && dirty) {
+    while (span.firstChild) {
+      span.removeChild(span.firstChild)
+    }
+    span.appendChild(document.createTextNode(` - ${numRemoved} post${numRemoved === 1 ? '' : 's'} with no replies hidden`))
+  }
+})
+
+observer.observe(document.documentElement, {
+  childList: true,
+  subtree: true
+})