« get me outta code hell

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-comment-blocker.js
diff options
context:
space:
mode:
Diffstat (limited to 'scratch-comment-blocker.js')
-rw-r--r--scratch-comment-blocker.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/scratch-comment-blocker.js b/scratch-comment-blocker.js
new file mode 100644
index 0000000..3e3823d
--- /dev/null
+++ b/scratch-comment-blocker.js
@@ -0,0 +1,55 @@
+// ==UserScript==
+// @name Scratch Comment Blocker
+// @namespace Violentmonkey Scripts
+// @match *://scratch.mit.edu/*
+// @grant none
+// ==/UserScript==
+
+// Customization ///////////////////////////////////////
+
+// Should comments written by any blocked users be
+// hidden?
+const blockComments = true
+
+// Should whole threads which ever had any posts by any
+// blocked users be hidden?
+const blockWholeThreads = true
+
+// Blocked users ///////////////////////////////////////
+
+// Just fill in the usernames of the users you choose
+// to block here.
+const blockedUsers = []
+
+// Comment blocking ////////////////////////////////////
+
+if (blockComments) {
+  const commentsContainer = document.querySelector('#comments ul.comments')
+
+  if (commentsContainer) {
+    const observer = new MutationObserver(mutations => {
+      for (const mutation of mutations) {
+        for (const addedNode of mutation.addedNodes) {
+          if (typeof addedNode.classList === 'undefined') continue
+          if (addedNode.classList.contains('top-level-reply') === false) continue
+          const topComment = addedNode
+          const comments = [topComment, ...topComment.getElementsByClassName('reply')]
+
+          for (const comment of comments) {
+            const nameEl = comment.querySelector('.name a')
+            if (nameEl && blockedUsers.includes(nameEl.textContent)) {
+              if (blockWholeThreads) {
+                topComment.remove()
+                break
+              } else {
+                comment.remove()
+              }
+            }
+          }
+        }
+      }
+    })
+
+    observer.observe(commentsContainer, {childList: true})
+  }
+}