Greasy Fork镜像 支持简体中文。

百度廣告(首尾推廣及右側廣告)清理

徹底清理百度搜索(www.baidu.com)結果首尾的推廣廣告、二次頑固廣告、右側廣告,去除重定向,刪除百家號

安裝腳本?
作者推薦腳本

您可能也會喜歡 怠惰小説下載器

安裝腳本
  1. // ==UserScript==
  2. // @name 百度广告(首尾推广及右侧广告)清理
  3. // @name:zh-CN 百度广告(首尾推广及右侧广告)清理
  4. // @name:zh-TW 百度廣告(首尾推廣及右側廣告)清理
  5. // @name:en Kill Baidu AD
  6. // @namespace hoothin
  7. // @version 1.23.11
  8. // @description 彻底清理百度搜索(www.baidu.com)结果首尾的推广广告、二次顽固广告、右侧广告,去除重定向,删除百家号
  9. // @description:zh-CN 彻底清理百度搜索(www.baidu.com)结果首尾的推广广告、二次顽固广告、右侧广告,去除重定向,移除百家号
  10. // @description:zh-TW 徹底清理百度搜索(www.baidu.com)結果首尾的推廣廣告、二次頑固廣告、右側廣告,去除重定向,刪除百家號
  11. // @description:en Just Kill Baidu AD
  12. // @author hoothin
  13. // @match *://www.baidu.com/*
  14. // @match *://m.baidu.com/*
  15. // @match *://gf.qytechs.cn/*/scripts/24192-*
  16. // @grant GM_xmlhttpRequest
  17. // @grant GM_addStyle
  18. // @grant GM_getValue
  19. // @grant GM_setValue
  20. // @grant GM_openInTab
  21. // @grant GM_registerMenuCommand
  22. // @grant GM_unregisterMenuCommand
  23. // @connect *
  24. // @run-at document-start
  25. // @license MIT License
  26. // @compatible chrome 测试通过
  27. // @compatible firefox 测试通过
  28. // @compatible opera 未测试
  29. // @compatible safari 未测试
  30. // ==/UserScript==
  31.  
  32. (function() {
  33. 'use strict';
  34. var MO = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, killBaijiaType = 1, killRight, killRightStyle, killRightRegister, hidePicture, hidePictureStyle, hidePictureRegister, blackList;
  35. if (MO) {
  36. var observer = new MO(function(records) {
  37. records.map(function(record) {
  38. if (record.addedNodes.length) {
  39. [].forEach.call(record.addedNodes, function(addedNode) {
  40. clearOneAD(addedNode);
  41. });
  42. }
  43. if (record.removedNodes.length) {
  44. [].forEach.call(record.removedNodes, function(removedNode) {
  45. if (killRight && removedNode === killRightStyle) {
  46. document.head.appendChild(killRightStyle);
  47. document.body.classList.add("killRight");
  48. } else if (hidePicture && removedNode === hidePictureStyle) {
  49. document.head.appendChild(hidePictureStyle);
  50. }
  51. });
  52. }
  53. });
  54. });
  55. var option = {
  56. 'childList': true,
  57. 'subtree': true
  58. };
  59. observer.observe(document, option);
  60. }
  61.  
  62. function checkBaijia(item) {
  63. var itemHref = item.querySelector("a").href;
  64. item.style.display = "none";
  65. if (itemHref.indexOf("baidu.com") == -1) return;
  66. var gmxhr = GM_xmlhttpRequest({
  67. url: itemHref,
  68. headers: {
  69. "Accept": "text/html"
  70. },
  71. method: "head",
  72. onreadystatechange: function(response) {
  73. if (response.readyState == 4) {
  74. if (response.finalUrl.indexOf("baijiahao.baidu.com") != -1) {
  75. item.remove();
  76. } else {
  77. item.style.display = "";
  78. }
  79. gmxhr.abort();
  80. }
  81. }
  82. });
  83. }
  84.  
  85. function checkMobileItem(item) {
  86. let mu = item.dataset.log;
  87. if (mu) {
  88. try {
  89. mu = JSON.parse(mu).mu;
  90. } catch (e) {
  91. return;
  92. }
  93. }
  94. if (!mu || mu === 'null') return;
  95. if (/^https:\/\/baijiahao\.baidu\.com/.test(mu)) {
  96. item.remove();
  97. return;
  98. } else {
  99. let title = item.querySelector('[data-module="title"]');
  100. let isBlack = checkBlackList(mu, title && title.innerText);
  101. if (isBlack) {
  102. item.classList.add("blocked");
  103. return;
  104. }
  105. }
  106. }
  107.  
  108. function checkLeftItem(item) {
  109. let mu = item.getAttribute("mu");
  110. if (mu && mu !== 'null' && mu.indexOf("http") == 0 && mu.indexOf("nourl") == -1 && item.getAttribute("tpl") != "short_video") {
  111. if (/^https:\/\/baijiahao\.baidu\.com/.test(mu)) {
  112. item.remove();
  113. return;
  114. } else {
  115. let title = item.querySelector('h3');
  116. let isBlack = checkBlackList(mu, title && title.innerText);
  117. if (isBlack) {
  118. item.classList.add("blocked");
  119. return;
  120. }
  121. let link = item.querySelector("a[href*='www.baidu.com/link']");
  122. if (link) {
  123. link.href = mu;
  124. item.setAttribute("mu", "");
  125. }
  126. }
  127. }
  128. let s = item.getAttribute("style");
  129. if (s && /display:(table|block)\s!important/.test(s)) {
  130. item.remove();
  131. } else {
  132. let baozhang = item.querySelector("[data-baodata]");
  133. if (baozhang) {
  134. item.remove();
  135. return;
  136. }
  137. var span = item.querySelector("div>span");
  138. if (span && span.innerHTML == "广告") {
  139. item.remove();
  140. }
  141. [].forEach.call(item.querySelectorAll("span,a"), function(span) {
  142. if (span && (span.innerHTML == "广告" || span.getAttribute("data-tuiguang"))) {
  143. item.remove();
  144. }
  145. });
  146. if (killBaijiaType == 2) {
  147. [].forEach.call(item.querySelectorAll("a>div>span+img"), function(img) {
  148. if (img && /^https?:\/\/pic\.rmb\.bdstatic\.com/.test(img.src)) {
  149. //checkBaijia(item);
  150. item.remove();
  151. }
  152. });
  153. }
  154. }
  155. }
  156.  
  157. function checkRightTable(item) {
  158. for (let i = 0; i < item.length; i++) {
  159. let d = item[i];
  160. if (d.id != "con-ar") {
  161. d.remove();
  162. }
  163. }
  164. }
  165.  
  166. function checkRightAd(item) {
  167. var nra = item.querySelectorAll("a");
  168. for (let i = 0; i < nra.length; i++) {
  169. let d = nra[i];
  170. if (d.innerHTML == "广告") {
  171. item.remove();
  172. break;
  173. }
  174. }
  175. }
  176.  
  177. function removeEcAd(ele) {
  178. var mAds = ele.querySelectorAll(".ec_wise_ad,.ec_youxuan_card,.page-banner");
  179. for (let i = 0; i < mAds.length; i++) {
  180. let mAd = mAds[i];
  181. mAd.remove();
  182. }
  183. var baiduapp = ele.querySelector("#copyright+div");
  184. if (baiduapp && baiduapp.querySelector("[ref='open']")) {
  185. baiduapp.remove();
  186. }
  187. var list = ele.querySelectorAll(ele.id === "results" ? ".result" : "#results>.result");
  188. for (let i = 0; i < list.length; i++) {
  189. let item = list[i];
  190. checkMobileItem(item);
  191. }
  192. }
  193.  
  194. function clearAD() {
  195. if (!document.querySelectorAll) return;
  196. if (killRight) {
  197. document.body.classList.add("killRight");
  198. } else document.body.classList.remove("killRight");
  199. removeEcAd(document);
  200. var list = document.querySelectorAll("#content_left>div,#content_left>table");
  201. for (let i = 0; i < list.length; i++) {
  202. let item = list[i];
  203. checkLeftItem(item);
  204. }
  205.  
  206. var eb = document.querySelectorAll("#content_right>table>tbody>tr>td>div");
  207. checkRightTable(eb);
  208.  
  209. var nr = document.querySelector("#content_right>div>div>div");
  210. if (nr) {
  211. checkRightAd(nr);
  212. }
  213. }
  214.  
  215. function clearOneAD(ele) {
  216. if (ele.nodeType != 1) return;
  217. if (ele.classList.contains("ec-tuiguang") || ele.classList.contains("ec_wise_ad") || ele.classList.contains("ec_youxuan_card") || ele.classList.contains("page-banner")) {
  218. ele.remove();
  219. return;
  220. }
  221. if (ele.parentNode && ele.parentNode.id == "content_left" && (ele.nodeName == "DIV" || ele.nodeName == "TABLE")) {
  222. checkLeftItem(ele);
  223. } else if (ele.parentNode && ele.parentNode.id == "content_right") {
  224. if (ele.nodeName == "TABLE") {
  225. var eb = ele.querySelectorAll("tbody>tr>td>div");
  226. checkRightTable(eb);
  227. }
  228. if (ele.nodeName == "DIV") {
  229. var nr = ele.querySelector("div>div");
  230. if (nr) {
  231. checkRightAd(nr);
  232. }
  233. }
  234. } else {
  235. removeEcAd(ele);
  236. let eles = ele.querySelectorAll("#content_left>div,#content_left>table");
  237. [].forEach.call(eles, e => {clearOneAD(e)});
  238. }
  239. }
  240. function initCss() {
  241. killRightStyle && killRightStyle.parentNode && killRightStyle.parentNode.removeChild(killRightStyle);
  242. GM_addStyle(`
  243. body:not(.showBlocked) .blocked {
  244. display: none!important;
  245. }
  246. body.showBlocked .blocked {
  247. background: linen;
  248. }
  249. `);
  250. if (killRight) {
  251. killRightStyle = GM_addStyle(`
  252. #content_right,[tpl="recommend_list"],#rs_new {
  253. display: none;
  254. }
  255. #container.sam_newgrid {
  256. width: calc(100% - 150px);
  257. }
  258. #container.sam_newgrid #content_left {
  259. column-width: 590px;
  260. width: 100%;
  261. }
  262. #content_left .result-op, #content_left .result {
  263. page-break-inside: avoid;
  264. }
  265. `);
  266. }
  267. hidePictureStyle && hidePictureStyle.parentNode && hidePictureStyle.parentNode.removeChild(hidePictureStyle);
  268. if (hidePicture) {
  269. hidePictureStyle = GM_addStyle(`
  270. #wrapper_wrapper {
  271. background: rgba(228, 228, 228, 0.29);
  272. }
  273. #content_left .cos-color-bg-page {
  274. background-color: unset;
  275. }
  276. #head:not(.s-skin-hasbg),
  277. [tpl="app/head-tab"] {
  278. background: #f7f7f7;
  279. }
  280. #container a,
  281. #container a em {
  282. text-decoration: auto!important;
  283. }
  284. #result_logo {
  285. opacity: 0.75;
  286. }
  287. .cos-col[style*="width:33"],.cos-col[style*="width:25"],#content_left .c-span2,#content_left .c-span3,#content_left .c-span4,#content_left .c-span6>.c-img,[tpl="short_video"],[data-module="gp-peopleintro-img-click"],div[class^="img-container"],[data-module="normal-img"],[role="img"],.c-gap-inner-y-img,div[class*="only-multi-image"],.sc-image-rounded-tl-base>.cos-row>.cos-col:first-child,[class^="image-content"] {
  288. display: none;
  289. }
  290. #wrapper>.result-molecule.new-pmd {
  291. box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
  292. }
  293. .new-pmd .c-span9,.new-pmd .c-span6,.new-pmd .c-span10,.cos-col[style*="width:33"]+div,.cos-col[style*="width:25"]+div,.sc-image-rounded-tl-base>.cos-row>.cos-col:first-child+div {
  294. width: 100%!important;
  295. }
  296. a,a:active,#content_right a {
  297. color: rgb(80,80,80);
  298. }
  299. em {
  300. color: #4E6EF2!important;
  301. }
  302. .c-container.result[tpl], .result-op.c-container[tpl], .result-op.xpath-log.new-pmd[tpl] {
  303. transition: box-shadow 350ms;
  304. box-shadow: 0 2px 5px 0px rgba(0, 0, 0, .1);
  305. padding: 16px;
  306. box-sizing: content-box;
  307. }
  308. .c-container.result[tpl] h3, .result-op.c-container[tpl] h3, .result-op.xpath-log.new-pmd[tpl] h3 {
  309. padding-bottom: 10px;
  310. border-bottom: 1px solid rgba(211, 211, 211, 0.63);
  311. }
  312. .c-group-wrapper {
  313. box-shadow: unset;
  314. margin-left: unset;
  315. margin-right: unset;
  316. }
  317. .sc-image {
  318. background-image: none !important;
  319. }
  320. .wrapper_new #head.no-box-shadow, .wrapper_new #head.no-box-shadow.s_down {
  321. box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
  322. }
  323. [class*=card-shadow] {
  324. box-shadow: unset!important;
  325. }
  326. #wrapper #content_left .op-soft-title h3>a:after, #wrapper #content_left .result h3>a:after, #wrapper #content_left .result-op h3>a.sc-link:after, #wrapper #content_left > .c-container h3>a:after, #wrapper #content_left .op-soft-title a:visited:after, #wrapper #content_left .result h3>a:visited:after, #wrapper #content_left .result-op h3>a.sc-link:visited:after, #wrapper #content_left > .c-container h3>a:visited:after {
  327. content: "";
  328. position: relative;
  329. border-bottom: 2px solid #3075d0;
  330. bottom: -2px;
  331. display: block;
  332. left: 100%;
  333. width: 0;
  334. transition: width 300ms, left 300ms;
  335. }
  336. #wrapper #content_left .op-soft-title a:hover:after, #wrapper #content_left .result a:hover:after, #wrapper #content_left .result-op a.sc-link:hover:after, #wrapper #content_left > .c-container a:hover:after, #wrapper #content_left .op-soft-title a:visited:hover:after, #wrapper #content_left .result a:visited:hover:after, #wrapper #content_left .result-op a.sc-link:visited:hover:after, #wrapper #content_left > .c-container a:visited:hover:after {
  337. left: 0;
  338. width: 100%;
  339. transition: width 300ms;
  340. }
  341. #wrapper #content_left > .c-container:hover, #wrapper #content_left > .result:hover, #wrapper #content_left > .result-op:hover {
  342. box-shadow: 0 2px 10px 1px rgba(0, 0, 0, .3);
  343. }
  344. `);
  345. }
  346. }
  347.  
  348. function checkBlackList(url, title) {
  349. if (!blackList || !blackList.forEach) return false;
  350. for (let i = 0; i < blackList.length; i++) {
  351. try {
  352. let isBlack = checkBlack(url, title, blackList[i]);
  353. if (isBlack) return true;
  354. } catch (e) {
  355. console.log(e);
  356. }
  357. }
  358. return false;
  359. }
  360.  
  361. function checkBlack(url, title, pattern) {
  362. pattern = pattern && pattern.replace(/\s*#.*/, "");
  363. if (!pattern || !pattern.trim() || pattern.indexOf("#") === 0 || pattern.indexOf("!") === 0 || pattern.indexOf(" ") === 0 || pattern.indexOf("Update") === 0) return false;
  364. if (pattern.indexOf("/") === 0) {
  365. let match = pattern.match(/^\/(.*)\/(\w*)$/);
  366. if (match) {
  367. return new RegExp(match[1], match[2]).test(url);
  368. }
  369. }
  370. if (pattern.indexOf("title") === 0) {
  371. let match = pattern.replace("title", "").trim();
  372. if (match.indexOf("/") === 0) {
  373. match = match.match(/^\/(.*)\/(\w*)$/);
  374. if (match) {
  375. return new RegExp(match[1], match[2]).test(title);
  376. }
  377. } else if (match.indexOf("=~") === 0) {
  378. match = match.match(/^=~\s*\/(.*)\/(\w*)$/);
  379. if (match) {
  380. return new RegExp(match[1], match[2]).test(title);
  381. }
  382. } else if (match.indexOf("=") === 0) {
  383. match = match.match(/^=\s*"(.*)"$/);
  384. if (match) {
  385. return title === match[1];
  386. }
  387. } else if (match.indexOf("^=") === 0) {
  388. match = match.match(/^\^=\s*"(.*)"$/);
  389. if (match) {
  390. return title.indexOf(match[1]) === 0;
  391. }
  392. } else if (match.indexOf("$=") === 0) {
  393. match = match.match(/^\$=\s*"(.*)"$/);
  394. if (match) {
  395. return title.endsWith(match[1]);
  396. }
  397. } else if (match.indexOf("*=") === 0) {
  398. match = match.match(/^\*=\s*"(.*)"$/);
  399. if (match) {
  400. return title.indexOf(match[1]) !== -1;
  401. }
  402. }
  403. }
  404. return matchPattern(url, pattern);
  405. }
  406.  
  407. function matchPattern(url, pattern) {
  408. if (pattern === '<all_urls>') {
  409. return true;
  410. }
  411. try {
  412. let match = pattern.match(/^(\*|[\w-]+):\/{2,3}(?:(\*|\*\.[^/*]+|[^/*]+)\/)?(.*)$/);
  413. if (!match) return url.indexOf(pattern) !== -1;
  414. const [, scheme, host, path] = match;
  415. const urlScheme = url.split(':')[0];
  416. const urlParam = new URL(url);
  417. if (scheme === '*' || urlScheme === scheme) {
  418. if (host !== '*') {
  419. const urlHost = urlParam.hostname;
  420. if (host.startsWith('*')) {
  421. const hostPattern = host.slice(2);
  422. if (!urlHost.endsWith(hostPattern)) return false;
  423. } else {
  424. if (urlHost !== host) return false;
  425. }
  426. }
  427. const urlPath = urlParam.pathname + urlParam.search;
  428. const pathRegex = new RegExp(`^${path.replace(/([\.\?])/g, '\\$1').replace(/\*/g, '.*')}$`);
  429. return pathRegex.test(urlPath);
  430. }
  431. } catch(e) {}
  432. return false;
  433. }
  434.  
  435. function registerMenuCommand() {
  436. initCss();
  437. if (window.top !== window.self) return;
  438. hidePictureRegister = GM_registerMenuCommand(hidePicture ? "✅ 恢复图片视频与样式" : "❌ 隐藏图片视频并简化样式", () => {
  439. GM_unregisterMenuCommand(killRightRegister);
  440. GM_unregisterMenuCommand(hidePictureRegister);
  441. hidePicture = !hidePicture;
  442. GM_setValue("hidePicture", hidePicture);
  443. registerMenuCommand();
  444. });
  445. killRightRegister = GM_registerMenuCommand(killRight ? "✅ 恢复右边栏与布局" : "❌ 隐藏右边栏并多列显示", () => {
  446. GM_unregisterMenuCommand(killRightRegister);
  447. GM_unregisterMenuCommand(hidePictureRegister);
  448. killRight = !killRight;
  449. GM_setValue("killRight", killRight);
  450. registerMenuCommand();
  451. });
  452. }
  453.  
  454. function run() {
  455. try {
  456. killRight = !!GM_getValue("killRight");
  457. hidePicture = !!GM_getValue("hidePicture");
  458. blackList = GM_getValue("blackList") || [];
  459. if (location.host === "gf.qytechs.cn") {
  460. function initConfig() {
  461. let parent = document.querySelector('#additional-info');
  462. let baseCon = document.createElement('div');
  463. baseCon.style.margin = '20px';
  464. parent.insertBefore(baseCon, parent.children[0]);
  465. let checkIndex = 0;
  466. let createCheckbox = (name, defaultValue) => {
  467. let box = document.createElement('div');
  468. let checkbox = document.createElement('input');
  469. checkbox.type = 'checkbox';
  470. checkbox.checked = defaultValue;
  471. let id = 'stcnsc-checkbox' + checkIndex++;
  472. checkbox.id = id;
  473. let label = document.createElement('label');
  474. label.setAttribute('for', id);
  475. label.innerText = name;
  476. box.appendChild(checkbox);
  477. box.appendChild(label);
  478. baseCon.appendChild(box);
  479. return checkbox;
  480. };
  481. let hidePictureInput = createCheckbox('隐藏图片并简化样式', hidePicture);
  482. let killRightInput = createCheckbox('隐藏右边栏并多列显示', killRight);
  483. let importInput = document.createElement('textarea');
  484. importInput.placeholder = '订阅 uBlacklist 规则:如 https://git.io/ublacklist';
  485. importInput.style.width = '100%';
  486. importInput.style.marginBottom = '10px';
  487. baseCon.appendChild(importInput);
  488. let blackListInput = document.createElement('textarea');
  489. blackListInput.placeholder = '*://*.12345.cn/*\n*://*.54321.com/*\n一行一条';
  490. blackListInput.style.width = '100%';
  491. blackListInput.style.minHeight = "60px";
  492. blackListInput.value = blackList.join("\n");
  493. baseCon.appendChild(blackListInput);
  494. function saveBlackList() {
  495. GM_setValue("blackList", blackList);
  496. }
  497. let saveBtn = document.createElement('button');
  498. saveBtn.innerText = '保存';
  499. saveBtn.style.display = 'block';
  500. saveBtn.style.fontSize = 'x-large';
  501. saveBtn.style.fontWeight = 'bold';
  502. saveBtn.style.pointerEvents = 'all';
  503. saveBtn.style.cursor = 'pointer';
  504. saveBtn.addEventListener("click", function(e) {
  505. hidePicture = hidePictureInput.checked;
  506. killRight = killRightInput.checked;
  507. GM_setValue("hidePicture", hidePicture);
  508. GM_setValue("killRight", killRight);
  509. if (importInput.value) {
  510. alert("读取规则中……");
  511. let importUrls = importInput.value.trim().split("\n");
  512. importUrls.forEach(url => {
  513. url = url && url.trim();
  514. if (!url) return;
  515. GM_xmlhttpRequest({
  516. url: url,
  517. onload: function(res) {
  518. let result = res.response || res.responseText;
  519. if (!result) return;
  520. blackList = blackList.concat(result.split("\n"));
  521. blackList = blackList.filter((value, index) => blackList.indexOf(value) === index);
  522. saveBlackList();
  523. blackListInput.value = blackList.join("\n");
  524. },
  525. onerror: function(e) {
  526. console.log(e);
  527. alert("网络错误,导入失败");
  528. }
  529. });
  530. });
  531. } else {
  532. blackList = blackListInput.value.split("\n");
  533. saveBlackList();
  534. }
  535. alert("设置完毕");
  536. });
  537. baseCon.appendChild(saveBtn);
  538. baseCon.appendChild(document.createElement("hr"));
  539. }
  540. if (document.readyState == "complete") {
  541. initConfig();
  542. } else {
  543. document.addEventListener("readystatechange", e => {
  544. if (document.readyState == "complete") {
  545. initConfig();
  546. }
  547. });
  548. }
  549. return;
  550. }
  551. registerMenuCommand();
  552. GM_registerMenuCommand("👁️ 检查屏蔽元素", () => {
  553. alert(`屏蔽元素数量:${document.body.querySelectorAll(".blocked").length}`)
  554. document.body.classList.toggle("showBlocked");
  555. });
  556. GM_registerMenuCommand("🔧 打开设置页", () => {
  557. GM_openInTab("https://gf.qytechs.cn/scripts/24192", {active: true});
  558. });
  559. } catch(e) {}
  560. clearAD();
  561. }
  562.  
  563. if (document.readyState == "complete" || document.readyState == "interactive") {
  564. run();
  565. } else {
  566. document.addEventListener("readystatechange", e => {
  567. if (document.readyState == "interactive") {
  568. run();
  569. }
  570. });
  571. }
  572. })();

QingJ © 2025

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