百度首页编辑

隐藏热搜,删除广告和垃圾元素

  1. // ==UserScript==
  2. // @name 百度首页编辑
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 隐藏热搜,删除广告和垃圾元素
  6. // @author 捈荼
  7. // @license Apache License 2.0
  8. // @match https://www.baidu.com
  9. // @run-at document-idie
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // ==/UserScript==
  13.  
  14. // Commented by ChatGPT.
  15.  
  16. // Declare an array containing the elements to be toggled
  17. var hotSearchElemrnt = [
  18. document.getElementsByClassName("c-font-normal c-color-gray2 hot-refresh")[0],
  19. document.getElementsByClassName("s-news-rank-content")[0]
  20. ];
  21.  
  22. // Function to toggle the display style of the elements in the hotSearchElemrnt array
  23. function _changeState() {
  24. hotSearchElemrnt.forEach((e) => { e.style.display = e.style.display == 'none' ? '' : 'none'; });
  25. }
  26.  
  27. (function () {
  28. 'use strict';
  29.  
  30. // Check if the user has previously decided whether to remove trash elements
  31. if (GM_getValue('settingsDoRemoveTrushElements', -1) == -1) {
  32. // If not, prompt the user to confirm whether they want to remove trash elements
  33. const settingsRemoveTrushElements = confirm('是否需要删除广告等元素?');
  34. // Store the user's decision in local storage
  35. GM_setValue('settingsDoRemoveTrushElements', settingsRemoveTrushElements);
  36. }
  37. // If the user has previously confirmed they want to remove trash elements, or if they just confirmed it
  38. if (GM_getValue('settingsDoRemoveTrushElements', -1) == true) {
  39. // Declare an object containing the class and id names of trash elements
  40. const trushElement = {
  41. class: ['s-news-list-wrapper c-container c-feed-box', 's-menu-item current', 'water-container', 's-loading s-opacity-border4-top'],
  42. id: ['s_menu_mine', 's-top-left', 'u1', 'bottom_layer', 's_side_wrapper']
  43. };
  44. // Remove all elements with the specified class names
  45. trushElement.class.forEach((ele) => { document.getElementsByClassName(ele)[0].remove(); });
  46. // Remove all elements with the specified id names
  47. trushElement.id.forEach((id) => { document.getElementById(id).remove(); });
  48. }
  49.  
  50. // Check if the user has previously decided whether to hide hot search
  51. if (GM_getValue('settingsDoHideHotSearch', -1) == -1) {
  52. // If not, prompt the user to confirm whether they want to hide hot search
  53. const settingsRemoveTrushElements = confirm('是否需要隐藏热搜?');
  54. // Store the user's decision in local storage
  55. GM_setValue('settingsDoHideHotSearch', settingsRemoveTrushElements);
  56. }
  57. // If the user has previously confirmed they want to hide hot search, or if they just confirmed it
  58. if (GM_getValue('settingsDoHideHotSearch', -1) == true) {
  59. // Toggle the display style of the hot search elements
  60. _changeState();
  61. // Create a new image element
  62. const newIMG = document.createElement('img');
  63. // Set the source of the image to a data URI
  64. newIMG.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAAAUBAMAAAD1iJl/AAAAKlBMVEVHcEyRlaORlaORlaKQlaSRlaORlaOSlKSRlaOQlqOQlaKRlaORlaORlaMHlKbCAAAADXRSTlMAM/IhHV1lGVxhHO1ry8GEFgAAAHRJREFUGNNdzbENgCAQheFn7KwIG9DYmdjYG1dwHEvncBdnoL9dRE7wHVSX+79wGE7w63fgiI5XPq64JNCmm+TGJsy8JJX2gVHQ4Ai5v4DHn9WpNvIl0q9fNbc182ntBikwKLPRopeJRZk1qDlXWIuAZS7TA0pEMyTsvCrkAAAAAElFTkSuQmCC';
  65. // Set the alt attribute of the image
  66. newIMG.alt = 's-resize';
  67. // Set the width and height of the image
  68. newIMG.width = '17';
  69. newIMG.height = '10';
  70. // Set the id of the image
  71. newIMG.id = 's-resize-usr-modified';
  72. // Set the inline style of the image
  73. newIMG.style = 'margin: 0px 0px 0px 10px';
  74. // Get the parent element of the image
  75. const parent = document.getElementsByClassName('c-font-medium c-color-t title-text')[0];
  76. // Remove the child element (arrow icon) from the parent element
  77. parent.removeChild(document.getElementsByClassName('c-icon arrow')[0]);
  78. // Append the new image to the parent element
  79. document.getElementsByClassName('c-font-medium c-color-t title-text')[0].appendChild(newIMG);
  80. // Get the hot search toggle link element
  81. const keyLink = document.getElementsByClassName('s-opacity-border1-bottom')[0].firstElementChild;
  82. keyLink.setAttribute("href", "javascript:;");
  83. keyLink.onclick = () => {
  84. // Toggle the display style of the hot search elements
  85. _changeState();
  86. // Modify the image
  87. const img = document.getElementById('s-resize-usr-modified');
  88. if (img.getAttribute('alt') == 's-resize-1') {
  89. img.setAttribute('src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjBAMAAABm2DcrAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAqUExURUdwTJGVo5GVo5GVopCVpJGVo5GVo5KUpJGVo5CWo5CVopGVo5GVo5GVoweUpsIAAAANdFJOUwAz8iEdXWUZXGEc7WvLwYQWAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAP0lEQVQoz2NgOcIAA0aXBGDM2LuKcNG7cGFGXYSw0EALK8OEc+9exRRFqEUygW6CSGHWi3D/akT4sk+B0AwMADIvMyQyQzjIAAAAAElFTkSuQmCC');
  90. img.setAttribute('alt', 's-resize-2');
  91. img.setAttribute('width', '10');
  92. img.setAttribute('height', '17');
  93. } else {
  94. img.setAttribute('src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAAAUBAMAAAD1iJl/AAAAKlBMVEVHcEyRlaORlaORlaKQlaSRlaORlaOSlKSRlaOQlqOQlaKRlaORlaORlaMHlKbCAAAADXRSTlMAM/IhHV1lGVxhHO1ry8GEFgAAAHRJREFUGNNdzbENgCAQheFn7KwIG9DYmdjYG1dwHEvncBdnoL9dRE7wHVSX+79wGE7w63fgiI5XPq64JNCmm+TGJsy8JJX2gVHQ4Ai5v4DHn9WpNvIl0q9fNbc182ntBikwKLPRopeJRZk1qDlXWIuAZS7TA0pEMyTsvCrkAAAAAElFTkSuQmCC');
  95. img.setAttribute('alt', 's-resize-1');
  96. img.setAttribute('width', '17');
  97. img.setAttribute('height', '10');
  98. }
  99. return false;
  100. }
  101. }
  102. })();