« get me outta code hell

scratch-forums-no-reply-hider.js - 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
blob: 1a6f844de52cbd6b66fa83a169a63bdb0f3edaa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ==UserScript==
// @name Hide Unreplied Topics
// @namespace Violentmonkey Scripts
// @match https://scratch.mit.edu/discuss/57/*
// @grant none
// ==/UserScript==

// Customization ///////////////////////////////////////

// Should topics which only have one reply be hidden
// if that reply was not written by an ST member?
const remove1NonST = true

// Topic hiding ////////////////////////////////////////

const stMembers = 'ceebee designerd kittyloaf shruti Champ99 dietbacon chrisg codubee sgcc_ dsquare scmb1 tarmelop quacht mres ericr natalie raimondious speakvisually thisandagain pandatt brycedtea Morant cwillisf pondermake stymphalianbirb'.split(' ')

let numRemoved = 0, span

const last = arr => arr[arr.length - 1]

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)
        const latestReplier = last(node.querySelector('.tcr').textContent.trim().split(' '))
        if (replyCount === 0 || remove1NonST && replyCount === 1 && !stMembers.includes(latestReplier)) {
          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} topic${numRemoved === 1 ? '' : 's'} with no replies hidden`))
  }
})

observer.observe(document.documentElement, {
  childList: true,
  subtree: true
})