Greasy Fork 支持简体中文。

nga.blocker

Block threads by titles and authors

  1. // ==UserScript==
  2. // @name nga.blocker
  3. // @namespace http://ngacn.cc/
  4. // @version 0.1
  5. // @description Block threads by titles and authors
  6. // @author OpenGG
  7. // @match https://bbs.ngacn.cc/thread.php?fid=*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. var MatchWithArr = function MatchWithArr(fn) {
  15. return function (compare, arr) {
  16. for (var i = arr.length - 1; i > -1; --i) {
  17. if (fn(compare, arr[i])) {
  18. return true;
  19. }
  20. }
  21. return false;
  22. };
  23. };
  24.  
  25. var random = Math.floor(Math.random() * Math.pow(2, 53));
  26.  
  27. var clsHide = '_hydra_hidden_nga_' + random;
  28.  
  29. var clsOptions = '_hydra_options_nga_' + random;
  30.  
  31. var clsBtn = '_hydra_btn_nga_' + random;
  32.  
  33. var css = '.' + clsHide + '{display:none}.' + clsBtn + '{position:absolute;top:100px;right:30px;opacity:0.6}.' + clsBtn + ':hover{opacity:1}.' + clsOptions + '{position:absolute;top:130px;right:30px;width:600px;padding:10px;background:#abc}.' + clsOptions + ' label{display:block}';
  34.  
  35. var addStyle = function addStyle() {
  36. var style = document.createElement('style');
  37.  
  38. style.textContent = css;
  39.  
  40. (document.head || document.documentElement).appendChild(style);
  41. };
  42.  
  43. var hideEl = function hideEl(el) {
  44. el.classList.add(clsHide);
  45. };
  46.  
  47. var blockedTopics = (localStorage.blockedTopics || '').split(/\r?\n/);
  48.  
  49. var blockedAuthors = (localStorage.blockedAuthors || '').split(/\r?\n/);
  50.  
  51. var block = function block() {
  52. var rows = document.querySelectorAll('.topicrow');
  53.  
  54. var matchAuthor = MatchWithArr(function (compare, item) {
  55. var matches = compare.match(/uid=(\d+)$/);
  56. var uid = matches && matches[1];
  57. return item === uid;
  58. });
  59.  
  60. var matchTopic = MatchWithArr(function (compare, item) {
  61. return compare.indexOf(item) !== -1;
  62. });
  63.  
  64. for (var i = rows.length - 1; i > -1; --i) {
  65. var row = rows[i];
  66.  
  67. if (blockedAuthors.length > 0) {
  68. var author = row.querySelector('a.author');
  69. if (matchAuthor(author.href, blockedAuthors)) {
  70. hideEl(row);
  71. continue;
  72. }
  73. }
  74.  
  75. if (blockedAuthors.length > 0) {
  76. var topic = row.querySelector('.topic');
  77. if (matchTopic(topic.textContent, blockedTopics)) {
  78. hideEl(row);
  79. continue;
  80. }
  81. }
  82. }
  83. };
  84.  
  85. var optionsShow = false;
  86. var hideOptions = function hideOptions() {
  87. optionsShow = false;
  88.  
  89. var el = document.querySelector('.' + clsOptions);
  90.  
  91. if (el) {
  92. el.parentNode.removeChild(el);
  93. }
  94. };
  95.  
  96. var showOptions = function showOptions() {
  97. optionsShow = true;
  98. var container = document.createElement('div');
  99.  
  100. container.className = clsOptions;
  101.  
  102. container.innerHTML = '<label>屏蔽标题关键字(每行一个):<textarea placeholder="一行一个关键字"></textarea></label><label>屏蔽楼主UID(每行一个):<textarea placeholder="一行一个UID"></textarea></label><button>保存</button>';
  103.  
  104. var _container$querySelec = container.querySelectorAll('textarea'),
  105. blockTopicEl = _container$querySelec[0],
  106. blockAuthorEl = _container$querySelec[1];
  107.  
  108. var save = container.querySelector('button');
  109.  
  110. blockTopicEl.value = blockedTopics.join('\n');
  111.  
  112. blockAuthorEl.value = blockedAuthors.join('\n');
  113.  
  114. save.addEventListener('click', function () {
  115. localStorage.blockedTopics = blockTopicEl.value;
  116.  
  117. localStorage.blockedAuthors = blockAuthorEl.value;
  118.  
  119. hideOptions();
  120.  
  121. location.reload();
  122. }, false);
  123.  
  124. document.body.appendChild(container);
  125. };
  126.  
  127. var addControls = function addControls() {
  128. var el = document.createElement('button');
  129.  
  130. el.className = clsBtn;
  131.  
  132. el.textContent = '屏蔽设置';
  133.  
  134. el.onclick = function () {
  135. if (optionsShow) {
  136. hideOptions();
  137. } else {
  138. showOptions();
  139. }
  140. };
  141.  
  142. document.body.appendChild(el);
  143. };
  144.  
  145. var init = function init() {
  146. document.removeEventListener('DOMContentLoaded', init, false);
  147.  
  148. addStyle();
  149. addControls();
  150. block();
  151. };
  152.  
  153. if (document.readyState === 'interactive' || document.readyState === 'complete') {
  154. init();
  155. } else {
  156. document.addEventListener('DOMContentLoaded', init, false);
  157. }
  158. })();