CBDS

屏蔽百度搜索中的广告类、添加 “屏蔽” 来屏蔽该网站、结果链接均为 直链、屏蔽右边杂项。

  1. // ==UserScript==
  2. // @name CBDS
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.2
  5. // @license MIT
  6. // @description 屏蔽百度搜索中的广告类、添加 “屏蔽” 来屏蔽该网站、结果链接均为 直链、屏蔽右边杂项。
  7. // @author Kros
  8. // @match https://www.baidu.com/*
  9. // @icon https://www.baidu.com/favicon.ico
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. document.addEventListener('DOMContentLoaded', function () {
  18.  
  19.  
  20. // ----- ----- 添加样式
  21. function TJ_YnSi() {
  22. // 添加样式
  23. let hd = document.head;
  24.  
  25. // 创建 style
  26. let style = document.createElement("style");
  27. style.id = "CBDS";
  28. hd.appendChild(style);
  29. let sty = document.getElementById("CBDS");
  30. sty.innerHTML += '\n.pbs{ background-color: #333333; color: #ffffff; width: auto; height: 20px; line-height: 20px; text-align: center; font-size: 12px; padding: 0 10px; border-radius: 10px; position: absolute; top: 10px; right: 10px; z-index: 10; cursor: not-allowed; } \n';
  31.  
  32. sty.innerHTML += `#container{ width: 100vw !important; padding: 0px !important; margin-left: 0 !important; }\n`;
  33. sty.innerHTML += `#sx, #content_left, #xg{ padding: 0px !important; }\n`;
  34. sty.innerHTML += `@media (max-width: 1000px){\n#sx, #content_left, #rs_new{ width: 80% !important; margin-left: calc((100vw - 80%) / 2) !important; }\n}\n`;
  35. sty.innerHTML += `@media (min-width: 1001px){\n#sx, #content_left, #rs_new{ width: 800px !important; margin-left: calc((100vw - 800px) / 2) !important; }\n}\n`;
  36. /* background-image: linear-gradient(to right, #ed765e 0%, #ed765e 80%, #fea858 100%); */
  37. sty.innerHTML += `#content_left>div{ background-color: #ffffff; width: calc(100% - 40px); padding: 20px; border: solid 1px #333333; border-radius: 10px; position: relative; }\n`;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. function cad() {
  47. let z = document.getElementById("content_left");
  48. let c = z.children;
  49. // 获取 直链
  50. for (let i = 0; i < c.length; i++) {
  51. let yl = c[i].getAttribute("mu");
  52. if (!yl) c[i].remove();
  53. }
  54. }
  55.  
  56. function pgbt() {
  57. let p = document.getElementById("page");
  58. let p1 = p.children[0];
  59. let pa = p1.children;
  60. for (let i = 0; i < pa.length; i++) {
  61. pa[i].onclick = function () {
  62. console.log("AD click");
  63. closeAD();
  64. }
  65. }
  66. }
  67.  
  68. // ----- ----- 删除加载广告
  69. function closeAD() {
  70.  
  71. // 给检索工具添加id及样式
  72. let sx = document.getElementById("container").querySelector("div");
  73. sx.id = "sx";
  74.  
  75. let lastTime = performance.now();
  76. let interval = 10; // 1秒的时间间隔
  77. let numTime = 0;
  78.  
  79. function checkElement() {
  80. const now = performance.now();
  81. const elapsed = now - lastTime;
  82.  
  83. if (elapsed >= interval) {
  84. // 重置计时器
  85. lastTime = now;
  86. cad();
  87.  
  88. // 如果页面已经加载完成,‌停止检查
  89. if (document.readyState === 'complete') {
  90. if (numTime == 300) {
  91. console.log("AD is close");
  92. return;
  93. }
  94. numTime++;
  95. }
  96.  
  97. // 否则,‌继续下一轮检查
  98. requestAnimationFrame(checkElement);
  99.  
  100. } else {
  101. // 如果还没有到1秒,‌再次调用requestAnimationFrame
  102. requestAnimationFrame(checkElement);
  103. }
  104. }
  105.  
  106. // 开始第一轮检查
  107. requestAnimationFrame(checkElement);
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. // 获取body元素
  116. var targetNode = document.body;
  117.  
  118. // 配置观察选项
  119. var config = {
  120. attributes: true, // 监听属性变化
  121. childList: true, // 监听子节点变化
  122. subtree: true, // 监听所有下级节点变化
  123. characterData: true, // 监听节点内容或节点文本的变动
  124. attributeOldValue: true, // 监听属性变化前的旧值
  125. characterDataOldValue: true // 监听节点内容或节点文本变化前的旧值
  126. };
  127.  
  128. // 当观察到变化时执行的回调函数
  129. var callback = function (mutationsList, observer) {
  130. for (var mutation of mutationsList) {
  131. if (mutation.type === 'characterData') {
  132. // ----- ----- 删除 右边
  133. let yb = document.getElementById("content_right");
  134. if (yb) yb.remove();
  135. pgbt();
  136. TJ_YnSi();
  137. BL_jeGo();
  138. }
  139. }
  140. };
  141.  
  142. // 创建一个观察器实例并传入回调函数
  143. var observer = new MutationObserver(callback);
  144.  
  145. // 使用上面的配置开始观察目标节点
  146. observer.observe(targetNode, config);
  147.  
  148. // 随后,‌body的任何变化都会触发callback函数
  149. // 例如:‌document.body.appendChild(newElement); 或 document.body.textContent = '新的内容';
  150.  
  151. // 当不再需要监听变化时,‌可以停止观察
  152. // observer.disconnect();
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. // ----- ----- 遍历结果
  164. function BL_jeGo() {
  165. /* 获取结果框 */
  166. let z = document.getElementById("content_left");
  167. let c = z.children;
  168.  
  169. // 遍历className
  170. for (let i = 0; i < c.length; i++) {
  171.  
  172. // 获取 直链
  173. let yl = c[i].getAttribute("mu");
  174. // 判断是否存在原链地址
  175. if (yl) {
  176. let h3 = c[i].querySelector("h3");
  177. // 判断是否存在标题
  178. if (h3) {
  179. let a = h3.firstChild;
  180. if (a) {
  181. a.setAttribute("href", yl);
  182. // 底名 直链
  183. let m = c[i].getElementsByClassName("c-color-gray")[0];
  184. if (m) {
  185. m.parentNode.setAttribute("href", yl);
  186. }
  187. }
  188.  
  189.  
  190. // 添加 屏蔽 按钮
  191. let d = document.createElement("div");
  192. d.className = "pbs";
  193. d.removeAttribute("data-tools");
  194. d.removeAttribute("aria-hidden");
  195. d.innerText = "屏 蔽";
  196.  
  197. // 点击事件
  198. d.onclick = () => {
  199. console.log("OK");
  200. };
  201.  
  202. // 添加元素
  203. c[i].appendChild(d);
  204. }
  205. }
  206. }
  207. }
  208.  
  209.  
  210.  
  211.  
  212. // ----- ----- 添加界面
  213. var cbds = {
  214. // 按钮
  215. an: function () {
  216. let form = document.getElementById("form");
  217. let p = form.parentNode;
  218. let d = document.createElement("div");
  219. d.id = "cbds_N";
  220. d.innerText = "CBDS";
  221. d.onclick = function () {
  222. let j = document.getElementById("cbds_J");
  223. j.style.display = "block";
  224. };
  225. let s = document.getElementById("CBDS");
  226. s.innerHTML += '\n#cbds_N{ background-color: #333333; color: #ffffff; width: 36px; height: 36px; line-height: 36px; text-align: center; font-size: 12px; border-radius: 50%; margin-top: 15px; margin-left: 30px; float: left; cursor: not-allowed; } \n';
  227.  
  228. p.appendChild(d);
  229. },
  230.  
  231. // 界面
  232. jm: function () {
  233. // 创建界面
  234. let j = document.createElement("div");
  235. j.id = "cbds_J";
  236. j.style.display = "none";
  237.  
  238. // 标题
  239. let b = document.createElement("div");
  240. b.id = "cbds_B";
  241. b.innerText = "C B D S";
  242.  
  243. // 列表
  244. let u = document.createElement("ul");
  245. u.id = "cbds_U";
  246.  
  247. // 项
  248. let l = document.createElement("li");
  249. l.className = "cbds_L";
  250. l.innerText = "第一条";
  251.  
  252. // 按钮
  253. let s = document.createElement("div");
  254. s.id = "cbds_S";
  255. s.innerText = "保 存";
  256. s.onclick = function () {
  257. j.style.display = "none";
  258. };
  259.  
  260. // ----- 合成
  261. u.appendChild(l);
  262. j.appendChild(b);
  263. j.appendChild(u);
  264. j.appendChild(s);
  265.  
  266.  
  267. // ----- 加载样式
  268. let st = document.getElementById("CBDS");
  269. function css() {
  270. st.innerHTML += '\n#cbds_J{ background-color: #ffffff; width: 200px; height: auto; overflow: hidden; border: solid #000000 1px; border-radius: 10px; position: fixed; top: 0; right: 0; z-index: 500; user-select: none; } \n';
  271. st.innerHTML += '\n#cbds_B{ background-color: #333333; color: #ffffff; width: 100%; height: 30px; line-height: 30px; text-align: center; font-size: 20px; } \n';
  272. st.innerHTML += '\n#cbds_U{ width: 100%; height: 200px; } \n';
  273. st.innerHTML += '\n.cbds_L{ width: 180px; height: 30px; line-height: 30px; border-bottom: solid #888888 1px; padding: 0 10px; } \n';
  274. st.innerHTML += '\n#cbds_S{ background-color: #333333; color: #ffffff; width: 100%; height: 30px; line-height: 30px; text-align: center; font-size: 20px; } \n';
  275. }
  276. css();
  277.  
  278.  
  279. // 加载界面
  280. document.body.appendChild(j);
  281. }
  282. };
  283. cbds.an();
  284. cbds.jm();
  285.  
  286. });
  287. })();