百度搜索广告清理助手

移除百度搜索的广告如搜索结果里的百度推广

  1. // ==UserScript==
  2. // @namespace https://github.com/JackieZheng
  3.  
  4. // @name 百度搜索广告清理助手
  5. // @name:en Remove Baidu Search Side AD
  6. // @name:zh 百度搜索广告清理助手
  7. // @name:zh-CN 百度搜索广告清理助手
  8.  
  9. // @description 移除百度搜索的广告如搜索结果里的百度推广
  10. // @description:en Remove Baidu search advertising
  11. // @name:zh 移除百度搜索的广告如搜索结果里的百度推广
  12. // @name:zh-CN 移除百度搜索的广告如搜索结果里的百度推广
  13.  
  14. // @homepageURL https://github.com/JackieZheng/remove-baidu-search-ad/
  15. // @supportURL https://github.com/JackieZheng/remove-baidu-search-ad/issues/
  16.  
  17. // @compatible chrome 67.0.3381.1 + TamperMonkey + 脚本 0.2.0 测试通过
  18. // @compatible firefox 未测试
  19. // @compatible opera 未测试
  20. // @compatible safari 未测试
  21.  
  22. // @author Jackie
  23. // @version 0.2.6
  24. // @license LGPLv3
  25.  
  26. // @match http://www.baidu.com/s*
  27. // @match https://www.baidu.com/s*
  28. // @grant none
  29. // @run-at document-end
  30. // ==/UserScript==
  31. 'use strict';
  32.  
  33. function clearBaiduSearchAD () {
  34. // 移除网页右边的推广
  35. var div = document.getElementById("ec_im_container");
  36. if (div) {
  37. div.parentNode.removeChild(div);
  38. }
  39.  
  40. // 移除搜索结果头部与尾部的推广
  41. Array.prototype.forEach.call(document.body.querySelectorAll("#content_left>div,#content_left>table"), function(e) {
  42. var a = e.getAttribute("style");
  43. if (a && /display:(table|block)\s!important/.test(a)) {
  44. e.parentNode.removeChild(e);
  45. }
  46. });
  47. // 移除搜索结果头部 shadow-root
  48. Array.prototype.forEach.call(document.body.querySelectorAll(".c-container /deep/ .c-container"), function(ad) {
  49. if (ad) {
  50. ad.parentNode.removeChild(ad);
  51. }
  52. });
  53. // 暴力清除“广告”
  54. Array.prototype.forEach.call(document.body.querySelectorAll("#content_left>div>div>span"), function(e) {
  55. var st = e.innerText;
  56. var ad=e.parentNode.parentNode;
  57. if (st=="广告") {
  58. ad.parentNode.removeChild(ad);
  59. }
  60. });
  61. }
  62.  
  63. clearBaiduSearchAD();
  64. document.getElementById("su").addEventListener('click', function() {
  65. setTimeout(clearBaiduSearchAD, 1000);
  66. }, false);
  67. document.getElementById("kw").addEventListener('keyup', function() {
  68. setTimeout(clearBaiduSearchAD, 1000);
  69. }, false);
  70. document.getElementById("container").addEventListener('DOMSubtreeModified',function(){
  71. setTimeout(clearBaiduSearchAD, 0);
  72. },false);
  73. window.onload = function () {
  74. setTimeout(clearBaiduSearchAD, 1000);
  75. };
  76. window.onscroll = function(){
  77. setTimeout(clearBaiduSearchAD, 0);
  78. }
  79. document.body.onmousemove = function(){
  80. setTimeout(clearBaiduSearchAD, 0);
  81. }