CSDN滚啊

屏蔽搜索结果中出现的一切有关CSDN的选项(支持Google、百度、Bing)

  1. // ==UserScript==
  2. // @name CSDN滚啊
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  5. // @description 屏蔽搜索结果中出现的一切有关CSDN的选项(支持Google、百度、Bing)
  6. // @author xiaoma
  7. // @match *://www.google.com/search*
  8. // @match *://www.google.*/search*
  9. // @match *://www.baidu.com/s*
  10. // @match *://www.bing.com/search*
  11. // @grant none
  12. // @run-at document-end
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. /*
  17. MIT License
  18.  
  19. Copyright (c) 2024 xiaoma
  20.  
  21. Permission is hereby granted, free of charge, to any person obtaining a copy
  22. of this software and associated documentation files (the "Software"), to deal
  23. in the Software without restriction, including without limitation the rights
  24. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. copies of the Software, and to permit persons to whom the Software is
  26. furnished to do so, subject to the following conditions:
  27.  
  28. The above copyright notice and this permission notice shall be included in all
  29. copies or substantial portions of the Software.
  30.  
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  37. SOFTWARE.
  38. */
  39.  
  40. (function () {
  41. 'use strict';
  42.  
  43. // 百度搜索结果选择器
  44. const baiduSelectors = {
  45. results: '#content_left > div.result, #content_left > div.result-op',
  46. title: 'h3.t a, h3.c-title a',
  47. url: '.c-showurl'
  48. };
  49.  
  50. // 屏蔽CSDN链接
  51. function blockCSDNLinks() {
  52. const csdnDomain = 'csdn.net';
  53. const csdnKeywords = ['csdn'];
  54.  
  55. // 通用链接检查策略(适用于Google、Bing等)
  56. const links = document.querySelectorAll('a');
  57. links.forEach(link => {
  58. let shouldBlock = false;
  59.  
  60. // 检查链接href是否包含CSDN域名
  61. if (link.href && link.href.toLowerCase().includes(csdnDomain)) {
  62. shouldBlock = true;
  63. }
  64.  
  65. // 检查链接文本是否包含CSDN关键词
  66. if (link.textContent && csdnKeywords.some(keyword =>
  67. link.textContent.toLowerCase().includes(keyword))) {
  68. shouldBlock = true;
  69. }
  70.  
  71. if (shouldBlock) {
  72. // 查找不同搜索引擎的搜索结果容器
  73. const resultItem = link.closest('.b_ans, .b_widgetContainer, .b_algo') || // Bing
  74. link.closest('.MjjYud') || // Google (这个类名相对稳定)
  75. link.closest('[data-rpos]') || // Google备选方案
  76. link.closest('[jscontroller="SC7lYd"]') || // Google另一个备选
  77. link.closest('div[class*="result"]'); // 通用备选方案
  78.  
  79. if (resultItem) {
  80. resultItem.style.display = 'none';
  81. console.log('已屏蔽CSDN搜索结果:', resultItem);
  82. }
  83. }
  84. });
  85.  
  86. // 百度搜索特殊处理(保持原有逻辑)
  87. const baiduResults = document.querySelectorAll(baiduSelectors.results);
  88. baiduResults.forEach(result => {
  89. const titleEl = result.querySelector(baiduSelectors.title);
  90. const urlEl = result.querySelector(baiduSelectors.url);
  91. if (!titleEl && !urlEl) return;
  92.  
  93. const titleText = titleEl?.textContent?.toLowerCase() || '';
  94. const urlText = urlEl?.textContent?.toLowerCase() || '';
  95. const href = titleEl?.href?.toLowerCase() || '';
  96.  
  97. if (titleText.includes('csdn') || urlText.includes('csdn') || href.includes('csdn.net')) {
  98. result.style.display = 'none';
  99. console.log('已屏蔽百度CSDN搜索结果:', result);
  100. }
  101. });
  102.  
  103. // 额外检查:通过文本内容查找CSDN相关结果
  104. const textElements = document.querySelectorAll('*');
  105. textElements.forEach(element => {
  106. // 只检查包含CSDN文本的特定元素
  107. if (element.textContent &&
  108. (element.textContent.includes('CSDN博客') ||
  109. element.textContent.includes('blog.csdn.net'))) {
  110.  
  111. // 查找父级搜索结果容器
  112. const resultContainer = element.closest('.MjjYud') ||
  113. element.closest('[data-rpos]') ||
  114. element.closest('.b_ans, .b_widgetContainer, .b_algo') ||
  115. element.closest('div[class*="result"]');
  116.  
  117. if (resultContainer) {
  118. resultContainer.style.display = 'none';
  119. console.log('通过文本内容屏蔽CSDN结果:', resultContainer);
  120. }
  121. }
  122. });
  123.  
  124. // 隐藏百度右侧栏
  125. const rightColumn = document.getElementById('content_right');
  126. if (rightColumn) {
  127. rightColumn.style.display = 'none';
  128. }
  129. }
  130.  
  131. // 防抖函数
  132. function debounce(func, wait) {
  133. let timeout;
  134. return function () {
  135. const context = this;
  136. const args = arguments;
  137. clearTimeout(timeout);
  138. timeout = setTimeout(() => {
  139. func.apply(context, args);
  140. }, wait);
  141. };
  142. }
  143.  
  144. // 滚动事件监听
  145. window.addEventListener('scroll', debounce(function () {
  146. const scrollHeight = Math.max(
  147. document.documentElement.scrollHeight,
  148. document.body.scrollHeight
  149. );
  150. const scrollTop = window.pageYOffset ||
  151. document.documentElement.scrollTop ||
  152. document.body.scrollTop;
  153. const clientHeight = window.innerHeight ||
  154. document.documentElement.clientHeight ||
  155. document.body.clientHeight;
  156.  
  157. if (scrollHeight - scrollTop - clientHeight < 100) {
  158. requestAnimationFrame(() => {
  159. blockCSDNLinks();
  160. });
  161. }
  162. }, 200));
  163.  
  164. // DOM变化监听
  165. const observer = new MutationObserver(debounce(function (mutations) {
  166. mutations.forEach(function (mutation) {
  167. if (mutation.addedNodes.length) {
  168. blockCSDNLinks();
  169. }
  170. });
  171. }, 200));
  172.  
  173. const config = {
  174. childList: true,
  175. subtree: true
  176. };
  177.  
  178. const targetNode = document.body;
  179. observer.observe(targetNode, config);
  180.  
  181. // 初始执行
  182. blockCSDNLinks();
  183. })();

QingJ © 2025

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