Rllmukzen Threadshitter

Really ignore ignored users

目前为 2019-07-28 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Rllmukzen Threadshitter
  3. // @description Really ignore ignored users
  4. // @namespace https://github.com/insin/greasemonkey/
  5. // @version 5
  6. // @match https://www.rllmukforum.com/index.php*
  7. // ==/UserScript==
  8.  
  9. function addStyle(css) {
  10. let $style = document.createElement('style')
  11. $style.appendChild(document.createTextNode(css))
  12. document.querySelector('head').appendChild($style)
  13. }
  14.  
  15. function TopicPage() {
  16. let ignoredUserIds = JSON.parse(localStorage.ignoredUserIds || '[]')
  17.  
  18. // Hide "You've chosen to ignore content by <ignored user>"
  19. addStyle(`
  20. .ipsComment_ignored {
  21. display: none;
  22. }
  23. `)
  24.  
  25. // Hide posts containing elements which have an ignored user id as a specified
  26. // data attribute.
  27. function hidePostsByDataAttribute(elements, dataAttribute) {
  28. elements.forEach(el => {
  29. if (!ignoredUserIds.includes(el.dataset[dataAttribute])) return
  30. let post = el.closest('article.ipsComment')
  31. if (post.style.display == 'none') return
  32. post.style.display = 'none'
  33. })
  34. }
  35.  
  36. // Hide posts which quote ignored users
  37. function processQuotes(context) {
  38. hidePostsByDataAttribute(
  39. context.querySelectorAll('[data-ipsquote-userid]'),
  40. 'ipsquoteUserid'
  41. )
  42. }
  43.  
  44. // Hide posts which @-mention ignored users
  45. function processMentions(context) {
  46. hidePostsByDataAttribute(
  47. context.querySelectorAll('[data-mentionid]'),
  48. 'mentionid'
  49. )
  50. }
  51.  
  52. // Hide the unread comment separator if all subseqent posts are hidden
  53. function updateUnreadCommentSeparator() {
  54. let separator = document.querySelector('hr.ipsCommentUnreadSeperator')
  55. if (!separator) return
  56. let hasVisiblePost = false
  57. let sibling = separator.nextElementSibling
  58. while (sibling) {
  59. if (sibling.matches('article.ipsComment') &&
  60. !sibling.classList.contains('ipsHide') &&
  61. sibling.style.display != 'none') {
  62. hasVisiblePost = true
  63. break
  64. }
  65. sibling = sibling.nextElementSibling
  66. }
  67. separator.style.display = hasVisiblePost ? '' : 'none'
  68. }
  69.  
  70. // Process all posts on the current page
  71. function processPosts(context = document) {
  72. processQuotes(context)
  73. processMentions(context)
  74. }
  75.  
  76. // Process initial posts
  77. processPosts()
  78. updateUnreadCommentSeparator()
  79.  
  80. // Watch for posts being replaced when paging
  81. new MutationObserver(mutations =>
  82. mutations.forEach(mutation => {
  83. if (mutation.oldValue == 'true') {
  84. processPosts()
  85. updateUnreadCommentSeparator()
  86. }
  87. })
  88. ).observe(document.querySelector('div.cTopic'), {
  89. attributes: true,
  90. attributeFilter: ['animating'],
  91. attributeOldValue: true,
  92. })
  93.  
  94. // Watch for new posts being loaded into the current page
  95. new MutationObserver(mutations => {
  96. mutations.forEach(mutation =>
  97. mutation.addedNodes.forEach(processPosts)
  98. )
  99. updateUnreadCommentSeparator()
  100. }).observe(document.querySelector('#elPostFeed > form'), {
  101. childList: true,
  102. })
  103. }
  104.  
  105. function IgnoredUsersPage() {
  106. // Sync ignored user ids
  107. localStorage.ignoredUserIds = JSON.stringify(
  108. Array.from(document.querySelectorAll('[data-ignoreuserid]')).map(el =>
  109. el.dataset.ignoreuserid
  110. )
  111. )
  112. }
  113.  
  114. let page
  115. if (location.href.includes('index.php?/topic/')) {
  116. page = TopicPage
  117. } else if (location.href.includes('index.php?/ignore/')) {
  118. page = IgnoredUsersPage
  119. }
  120.  
  121. if (page) {
  122. page()
  123. }