fuck csdn and geek-docs

在搜索结果中屏蔽指定域名。支持 Google / Baidu / Bing / 360 搜索

当前为 2025-03-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name fuck csdn and geek-docs
  3. // @namespace null
  4. // @version 0.1
  5. // @icon https://www.google.com/s2/favicons?sz=64&domain=csdn.net
  6. // @description 在搜索结果中屏蔽指定域名。支持 Google / Baidu / Bing / 360 搜索
  7. // @license GNU-GPLv3
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // 要屏蔽的域名列表
  13. const blockedDomains = ['csdn.net', 'blog.csdn.net', 'time.geekbang.org', 'geekbang.org','geek-docs.com'];
  14.  
  15. // 搜索引擎域名列表
  16. const searchEngines = [
  17. 'www.baidu.com/s',
  18. 'www.google.com/search',
  19. 'www.bing.com/search',
  20. 'www.so.com/s'
  21. ];
  22.  
  23. // 创建提示框样式
  24. const style = document.createElement('style');
  25. style.textContent = `
  26. .toast-notification {
  27. position: fixed;
  28. bottom: 20px;
  29. right: 20px;
  30. background: #333;
  31. color: white;
  32. padding: 12px 24px;
  33. border-radius: 4px;
  34. z-index: 9999;
  35. opacity: 0;
  36. transition: opacity 0.3s ease-in-out;
  37. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  38. user-select:none;
  39. }
  40. .toast-notification.show {
  41. opacity: 1;
  42. user-select:none;
  43. }
  44. .blocked-overlay {
  45. position: fixed;
  46. user-select:none;
  47. top: 0;
  48. left: 0;
  49. width: 100%;
  50. height: 100%;
  51. background: #000;
  52. color: white;
  53. display: flex;
  54. justify-content: center;
  55. align-items: center;
  56. z-index: 99999;
  57. font-size: 24px;
  58. font-family: Arial, sans-serif;
  59. opacity: 1;
  60. }
  61. `;
  62. document.head.appendChild(style);
  63.  
  64. // 显示提示框函数
  65. function showToast(message, duration = 2000) {
  66. const toast = document.createElement('div');
  67. toast.className = 'toast-notification';
  68. toast.textContent = message;
  69. document.body.appendChild(toast);
  70. // 显示提示框
  71. setTimeout(() => toast.classList.add('show'), 10);
  72. // 定时移除提示框
  73. setTimeout(() => {
  74. toast.classList.remove('show');
  75. setTimeout(() => toast.remove(), 300);
  76. }, duration);
  77. }
  78.  
  79. function isBlockedDomain() {
  80. return blockedDomains.some(domain =>
  81. window.location.hostname.includes(domain)
  82. );
  83. }
  84.  
  85. function isSearchEngine() {
  86. const currentPath = window.location.hostname + window.location.pathname;
  87. return searchEngines.some(engine => currentPath.includes(engine));
  88. }
  89.  
  90. function showBlockedPage() {
  91. document.body.innerHTML = '';
  92. const overlay = document.createElement('div');
  93. overlay.className = 'blocked-overlay';
  94. overlay.textContent = '当前域名已被屏蔽';
  95. document.body.appendChild(overlay);
  96. }
  97.  
  98. function hideBlockedSites() {
  99. const filters = ".source_1Vdff, .iUh30, .b_attribution, .g-linkinfo-a, .c-abstract, .c-title".split(", ")
  100. const Elements = document.querySelectorAll(".result.c-container, .g, .b_algo, .res-list, .c-container, [class*='result']");
  101. let blockedCount = 0;
  102. let blockedUrls = [];
  103. Elements.forEach(function(Item) {
  104. let shouldBlock = false;
  105. // 检查元素本身的文本内容
  106. if (Item.textContent && blockedDomains.some(domain => Item.textContent.toLowerCase().includes(domain))) {
  107. shouldBlock = true;
  108. }
  109. // 检查特定选择器
  110. if (!shouldBlock) {
  111. for (const filter of filters) {
  112. const selectedContent = Item.querySelector(filter);
  113. if (selectedContent !== null) {
  114. const contentText = selectedContent.innerText.toLowerCase();
  115. if (blockedDomains.some(domain => contentText.includes(domain))) {
  116. shouldBlock = true;
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. if (shouldBlock) {
  123. // 尝试获取链接URL
  124. const link = Item.querySelector('a');
  125. if (link && link.href) {
  126. blockedUrls.push(link.href);
  127. }
  128. Item.parentNode.removeChild(Item);
  129. blockedCount++;
  130. }
  131. });
  132. if (blockedCount > 0) {
  133. console.log(`[Domain Blocker] 已去除 ${blockedCount} 条屏蔽域名的内容`);
  134. console.log(`已屏蔽的链接:\n${blockedUrls.map(url => `- ${url}`).join('\n')}`);
  135. showToast(`已屏蔽 ${blockedCount} 条内容`);
  136. }
  137. }
  138.  
  139. // 主函数
  140. function main() {
  141. if (isBlockedDomain()) {
  142. showBlockedPage();
  143. } else if (isSearchEngine()) {
  144. hideBlockedSites();
  145. // 监听页面变化(针对动态加载的搜索结果)
  146. const observer = new MutationObserver(() => {
  147. hideBlockedSites();
  148. });
  149. observer.observe(document.body, { childList: true, subtree: true });
  150. }
  151. }
  152.  
  153. // 运行主函数
  154. main();