scratch-forums-no-reply-hider.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // ==UserScript==
  2. // @name Hide Unreplied Topics
  3. // @namespace Violentmonkey Scripts
  4. // @match https://scratch.mit.edu/discuss/57/*
  5. // @grant none
  6. // ==/UserScript==
  7. // Customization ///////////////////////////////////////
  8. // Should topics which only have one reply be hidden
  9. // if that reply was not written by an ST member?
  10. const remove1NonST = true
  11. // Should topics that you created ever be hidden?
  12. const hideOwnTopics = false
  13. // Topic hiding ////////////////////////////////////////
  14. 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(' ')
  15. let numRemoved = 0, span, ownUsername
  16. const last = arr => arr[arr.length - 1]
  17. const observer = new MutationObserver(mutations => {
  18. if (!ownUsername && window.Scratch && Scratch.INIT_DATA.LOGGED_IN_USER.model) {
  19. ownUsername = Scratch.INIT_DATA.LOGGED_IN_USER.model.username
  20. }
  21. let dirty = false
  22. for (const mutation of mutations) {
  23. for (const node of mutation.addedNodes) {
  24. if (!node.matches) {
  25. continue
  26. }
  27. if (node.matches('.box-content tr')) {
  28. const replyCount = parseInt(node.querySelector('.tc2').textContent)
  29. const latestReplier = last(node.querySelector('.tcr').textContent.trim().split(' '))
  30. const author = last(node.querySelector('.tcl').textContent.trim().split(' '))
  31. if ((hideOwnTopics || author !== ownUsername) && (replyCount === 0 || remove1NonST && replyCount === 1 && !stMembers.includes(latestReplier))) {
  32. node.remove()
  33. numRemoved++
  34. dirty = true
  35. }
  36. }
  37. if (!span && node.matches('.box-head h4')) {
  38. span = document.createElement('span')
  39. span.style.color = 'red'
  40. node.appendChild(span)
  41. }
  42. }
  43. }
  44. if (span && dirty) {
  45. while (span.firstChild) {
  46. span.removeChild(span.firstChild)
  47. }
  48. span.appendChild(document.createTextNode(` - ${numRemoved} topic${numRemoved === 1 ? '' : 's'} with no replies hidden`))
  49. }
  50. })
  51. observer.observe(document.documentElement, {
  52. childList: true,
  53. subtree: true
  54. })