屏蔽百度右侧热搜

屏蔽百度搜索结果页右侧的“百度热搜”板块

  1. // ==UserScript==
  2. // @name 屏蔽百度右侧热搜
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 屏蔽百度搜索结果页右侧的“百度热搜”板块
  6. // @author YourName
  7. // @match https://www.baidu.com/s?*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const targetSelectors = [
  16. '#content_right', // 右侧容器ID
  17. '.s-hotsearch-wrapper', // 热搜容器类
  18. '#rsv-right', // 备用右侧容器ID
  19. '.s-news-rank-content' // 新闻排行容器类
  20. ];
  21.  
  22. function hideHotSearch() {
  23. // 通过选择器查找元素
  24. for (const selector of targetSelectors) {
  25. const element = document.querySelector(selector);
  26. if (element) {
  27. element.style.display = 'none';
  28. return true;
  29. }
  30. }
  31.  
  32. // 通过文本内容查找(适配动态更新)
  33. const xpath = '//*[contains(text(), "百度热搜") or contains(text(), "热搜新闻")]/ancestor::div[1]';
  34. const heading = document.evaluate(
  35. xpath,
  36. document,
  37. null,
  38. XPathResult.FIRST_ORDERED_NODE_TYPE,
  39. null
  40. ).singleNodeValue;
  41.  
  42. if (heading) {
  43. heading.style.display = 'none';
  44. return true;
  45. }
  46.  
  47. return false;
  48. }
  49.  
  50. // 立即执行屏蔽
  51. if (hideHotSearch()) return;
  52.  
  53. // 动态内容监听
  54. const observer = new MutationObserver((mutations) => {
  55. mutations.forEach(() => {
  56. if (hideHotSearch()) observer.disconnect();
  57. });
  58. });
  59.  
  60. // 监听整个文档的变化
  61. observer.observe(document.documentElement, {
  62. childList: true,
  63. subtree: true,
  64. attributes: false,
  65. characterData: false
  66. });
  67.  
  68. // 10秒后自动停止监听
  69. setTimeout(() => observer.disconnect(), 10000);
  70. })();

QingJ © 2025

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