scratch-comment-blocker.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // ==UserScript==
  2. // @name Scratch Comment Blocker
  3. // @namespace Violentmonkey Scripts
  4. // @match *://scratch.mit.edu/*
  5. // @grant none
  6. // ==/UserScript==
  7. // Customization ///////////////////////////////////////
  8. // Should comments written by any blocked users be
  9. // hidden?
  10. const blockComments = true
  11. // Should whole threads which ever had any posts by any
  12. // blocked users be hidden?
  13. const blockWholeThreads = true
  14. // Blocked users ///////////////////////////////////////
  15. // Just fill in the usernames of the users you choose
  16. // to block here.
  17. const blockedUsers = []
  18. // Comment blocking ////////////////////////////////////
  19. if (blockComments) {
  20. const commentsContainer = document.querySelector('#comments ul.comments')
  21. if (commentsContainer) {
  22. const observer = new MutationObserver(mutations => {
  23. for (const mutation of mutations) {
  24. for (const addedNode of mutation.addedNodes) {
  25. if (typeof addedNode.classList === 'undefined') continue
  26. if (addedNode.classList.contains('top-level-reply') === false) continue
  27. const topComment = addedNode
  28. const comments = [topComment, ...topComment.getElementsByClassName('reply')]
  29. for (const comment of comments) {
  30. const nameEl = comment.querySelector('.name a')
  31. if (nameEl && blockedUsers.includes(nameEl.textContent)) {
  32. if (blockWholeThreads) {
  33. topComment.remove()
  34. break
  35. } else {
  36. comment.remove()
  37. }
  38. }
  39. }
  40. }
  41. }
  42. })
  43. observer.observe(commentsContainer, {childList: true})
  44. }
  45. }