AO3屏蔽关键词

添加动态屏蔽关键词功能,储存在Tampermonkey后台,支持添加与移除关键词,对标题、标签、summary进行检测

目前为 2024-10-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name AO3屏蔽关键词
  3. // @version 0.0.1
  4. // @description 添加动态屏蔽关键词功能,储存在Tampermonkey后台,支持添加与移除关键词,对标题、标签、summary进行检测
  5. // @author ✌
  6. // @match https://archiveofourown.org/*
  7. // @match https://zyfzd.top/*
  8. // @match https://bk.jdkg.fun/*
  9. // @namespace https://gf.qytechs.cn/users/1384897
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. // ========================
  16.  
  17. let blacklist = GM_getValue('blacklist', []);
  18.  
  19. // 动态更新屏蔽词
  20. function updateBlacklist() {
  21. const newWord = prompt("请输入要添加的屏蔽词:");
  22. if (newWord) {
  23. blacklist.push(newWord);
  24. GM_setValue('blacklist', blacklist);
  25. alert(`添加成功!当前屏蔽词列表:${blacklist.join(', ')}`);
  26. window.location.reload(); // 刷新页面以应用新的屏蔽词
  27. }
  28. }
  29.  
  30. function manageBlacklist() {
  31. let currentBlacklist = GM_getValue('blacklist', []);
  32. if (currentBlacklist.length === 0) {
  33. alert("当前没有屏蔽词。");
  34. return;
  35. }
  36.  
  37. let blacklistText = `当前屏蔽词:\n${currentBlacklist.join('\n')}`;
  38. let textarea = document.createElement('textarea');
  39. textarea.value = blacklistText;
  40. textarea.style.width = "100%";
  41. textarea.style.height = "100px";
  42. textarea.readOnly = true;
  43.  
  44. let modal = document.createElement('div');
  45. modal.style.position = 'fixed';
  46. modal.style.top = '50%';
  47. modal.style.left = '50%';
  48. modal.style.transform = 'translate(-50%, -50%)';
  49. modal.style.backgroundColor = 'white';
  50. modal.style.border = '1px solid #ccc';
  51. modal.style.padding = '20px';
  52. modal.style.zIndex = 1000;
  53. modal.style.width = '300px';
  54.  
  55. let closeButton = document.createElement('button');
  56. closeButton.innerHTML = '关闭';
  57. closeButton.style.marginTop = '10px';
  58. closeButton.onclick = () => {
  59. document.body.removeChild(modal);
  60. };
  61.  
  62. let input = document.createElement('input');
  63. input.type = 'text';
  64. input.placeholder = '请输入要移除的屏蔽词';
  65. input.style.width = '100%';
  66. input.style.marginTop = '10px';
  67.  
  68. let removeButton = document.createElement('button');
  69. removeButton.innerHTML = '移除屏蔽词';
  70. removeButton.style.marginTop = '10px';
  71. removeButton.onclick = () => {
  72. let toRemove = input.value;
  73. if (!toRemove) {
  74. alert('请输入要移除的屏蔽词。');
  75. return;
  76. }
  77.  
  78. if (currentBlacklist.includes(toRemove)) {
  79. blacklist = currentBlacklist.filter(word => word !== toRemove);
  80. GM_setValue('blacklist', blacklist);
  81. alert(`已移除屏蔽词:${toRemove}\n当前屏蔽词列表:${blacklist.join(', ')}`);
  82. document.body.removeChild(modal);
  83. window.location.reload();
  84. } else {
  85. alert("未找到该屏蔽词,请检查输入是否正确。");
  86. }
  87. };
  88.  
  89. modal.appendChild(textarea);
  90. modal.appendChild(input);
  91. modal.appendChild(removeButton);
  92. modal.appendChild(closeButton);
  93. document.body.appendChild(modal);
  94. }
  95.  
  96. // 检测关键词是否在文本中
  97. function test(keywords) {
  98. for (let k = 0; k < blacklist.length; k++) {
  99. if (keywords.indexOf(blacklist[k]) !== -1) return true;
  100. }
  101. return false;
  102. }
  103.  
  104. let processedItems = new Set();
  105.  
  106. // 处理屏蔽逻辑
  107. function applyBlacklist() {
  108. let works = document.querySelectorAll('li.work');
  109.  
  110. if (blacklist.length && works.length) {
  111. works.forEach(work => {
  112. let title = work.querySelector('h4.heading a');
  113. let tags = work.querySelectorAll('ul.tags li a.tag');
  114. let summary = work.querySelector('blockquote.summary');
  115.  
  116. let titleText = title ? title.textContent : '';
  117. let tagsText = Array.from(tags).map(tag => tag.textContent).join(' ');
  118. let summaryText = summary ? summary.textContent : '';
  119.  
  120. // 检查标题、标签或简介中是否有屏蔽词
  121. if (test(titleText) || test(tagsText) || test(summaryText)) {
  122. if (!processedItems.has(work)) {
  123. processedItems.add(work);
  124. console.log(`屏蔽了文章: ${titleText}`);
  125. work.parentElement.removeChild(work); // 移除符合条件的文章
  126. }
  127. }
  128. });
  129. }
  130. }
  131.  
  132. // 创建导航栏按钮
  133. function createNavButton() {
  134. let nav = document.querySelector('nav');
  135. if (nav) {
  136. let addButton = document.createElement('button');
  137. addButton.innerHTML = '添加屏蔽词';
  138. addButton.style.margin = '10px';
  139. addButton.style.padding = '5px';
  140. addButton.onclick = updateBlacklist;
  141. nav.appendChild(addButton);
  142.  
  143. let manageButton = document.createElement('button');
  144. manageButton.innerHTML = '管理屏蔽词';
  145. manageButton.style.margin = '10px';
  146. manageButton.style.padding = '5px';
  147. manageButton.onclick = manageBlacklist;
  148. nav.appendChild(manageButton);
  149. }
  150. }
  151.  
  152. // 页面加载后创建按钮并应用屏蔽
  153. window.addEventListener('load', () => {
  154. createNavButton();
  155. applyBlacklist();
  156. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址