🏠 Home 

Greasy Fork is available in English.

搜索引擎过滤器

过滤搜索页垃圾信息,并将重定向网址解析为直接网址


安装此脚本?
  1. // ==UserScript==
  2. // @name SearchEngine-Filter
  3. // @name:zh-CN 搜索引擎过滤器
  4. // @namespace https://greasyfork.org/zh-CN/users/42351
  5. // @version 1.8
  6. // @description Filter search page spam, And resolve redirect URL into direct
  7. // @description:zh-CN 过滤搜索页垃圾信息,并将重定向网址解析为直接网址
  8. // @icon64 https://antecer.gitlab.io/amusingdevice/icon/antecer.ico
  9. // @icon https://antecer.gitlab.io/amusingdevice/icon/antecer.ico
  10. // @author Antecer
  11. // @include http*://*.baidu.com*
  12. // @include http*://*.bing.com/search?*
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @connect *
  17. // @run-at document-body
  18. // @compatible chrome 测试通过
  19. // @compatible firefox 未测试
  20. // @compatible opera 未测试
  21. // @compatible safari 未测试
  22. // ==/UserScript==
  23. (async () => {
  24. // 识别百度网页
  25. if (!location.host.endsWith('baidu.com')) return;
  26. // 识别百度搜索页 与 搜图页
  27. if (!(location.href.includes('baidu.com/s?') || location.href.includes('baidu.com/pcpage/similar?'))) return;
  28. // 读取脚本配置
  29. let scriptCfgs = {
  30. unRedirect: GM_getValue('unRedirect', true), // 反重定向(默认启用)
  31. unExperience: GM_getValue('unExperience', true), // 屏蔽百度经验(默认启用)
  32. unOtherQuery: GM_getValue('unOtherQuery', true), // 屏蔽其他人还在搜(默认启用)
  33. unExpert: GM_getValue('unExpert', true), // 屏蔽百度健康(默认启用)
  34. unHotQuery: GM_getValue('unHotQuery', false), // 屏蔽百度热搜榜(默认禁用)
  35. unSimilar: GM_getValue('unSimilar', false), // 屏蔽相关搜索(默认禁用)
  36. unGame: GM_getValue('unGame', false), // 屏蔽百度游戏(默认禁用)
  37. unTuiguang: GM_getValue('unTuiguang', true), // 屏蔽百度推广(默认启用)
  38. adBlock: GM_getValue('adBlock', true), // 屏蔽广告(默认启用)
  39. unRogue: GM_getValue('unRogue', 'hao123.com|2345.com') // 屏蔽流氓网站(默认启用,清空参数表示禁用)
  40. };
  41. // 创建sleep方法(用于async/await的延时处理)
  42. const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  43. // 创建超时变量,防止长时间循环遍历document
  44. const timeoutValue = 10;
  45. // 创建选择器,用于判断element是否已加载
  46. const isBodyLoading = async (css) => {
  47. let timeout = timeoutValue;
  48. while (!document.querySelector(css) && timeout) {
  49. --timeout;
  50. await Sleep(1000);
  51. }
  52. };
  53. // 功能模块
  54. const Steps = {
  55. unRedirect: async () => {
  56. await isBodyLoading(`[href*="baidu.com/link?"],[href*="baidu.com/api/proxy?"]`);
  57. let allowUpgrade = document.createElement(`meta`);
  58. allowUpgrade.setAttribute('http-equiv', 'Content-Security-Policy');
  59. allowUpgrade.setAttribute('content', 'upgrade-insecure-requests');
  60. document.head.append(allowUpgrade);
  61. document.querySelectorAll(`[href*="baidu.com/link?"],[href*="baidu.com/api/proxy?"]`).forEach((element) => {
  62. (async (item) => {
  63. let reTry = false;
  64. let thisXhr = GM_xmlhttpRequest({
  65. url: item.href,
  66. method: 'GET',
  67. onreadystatechange: (r###lt) => {
  68. if (r###lt.readyState > 2) {
  69. item.href = r###lt.finalUrl;
  70. thisXhr.abort();
  71. }
  72. },
  73. onerror: (err) => {
  74. reTry = true;
  75. console.error(`[Baidu-Filter] Call HEAD Failed!`, err);
  76. }
  77. });
  78. })(element);
  79. });
  80. },
  81. unExperience: async () => {
  82. await isBodyLoading(`[href*="jingyan.baidu.com"]`);
  83. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  84. if (item.querySelector(`[href*="jingyan.baidu.com"]`)) item.remove();
  85. });
  86. },
  87. unOtherQuery: async () => {
  88. await isBodyLoading(`.r###lt-op`);
  89. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  90. if (item.innerHTML.includes(`>其他人还在搜<`)) item.remove();
  91. });
  92. },
  93. unExpert: async () => {
  94. while (!document.querySelector(`[href*="expert.baidu.com"]`)) await Sleep(1000);
  95. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  96. if (item.querySelector(`[href*="expert.baidu.com"]`)) item.remove();
  97. });
  98. },
  99. unTuiguang: async () => {
  100. while (!document.querySelector(`[data-tuiguang]`)) await Sleep(1000);
  101. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  102. if (item.querySelector(`span[data-tuiguang]`)) item.remove();
  103. });
  104. },
  105. unGame: async()=> {
  106. await isBodyLoading(`a[href*="lewan.baidu.com"]`);
  107. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  108. if (item.querySelector(`a[href*="lewan.baidu.com"]`)) item.remove();
  109. });
  110. },
  111. unHotQuery: async () => {
  112. await isBodyLoading(`[title="百度热榜"]`);
  113. document.querySelectorAll(`#content_right`).forEach((item) => {
  114. if (item.querySelector(`[title="百度热榜"]`)) item.remove();
  115. });
  116. },
  117. unSimilar: async () => {
  118. while (!document.querySelector(`#rs`)) await Sleep(1000);
  119. document.querySelectorAll(`[id="rs"]`).forEach((item) => {
  120. if (item.querySelector(`table a[href^="/s?wd="]`)) item.remove();
  121. });
  122. },
  123. adBlock: async () => {
  124. while (!document.querySelector(`.ec_tuiguang_pplink`)) await Sleep(1000);
  125. document.querySelectorAll(`#content_left>div`).forEach((item) => {
  126. if (item.innerHTML.includes(`>广告</span>`)) item.remove();
  127. });
  128. },
  129. unRogue: async () => {
  130. let timeout = timeoutValue;
  131. let rogueList = scriptCfgs.unRogue;
  132. let rogueRegExp = new RegExp(rogueList);
  133. while (rogueList && timeout) {
  134. --timeout;
  135. let nodes = document.querySelectorAll('a');
  136. for (let i = nodes.length; i > 0; ) {
  137. if (nodes[--i].href.match(rogueRegExp)) {
  138. i = timeout = 0;
  139. }
  140. }
  141. await Sleep(1000);
  142. }
  143. let rList = document.querySelectorAll(`#content_left>div`);
  144. for (let i = rList.length; i > 0; ) {
  145. --i;
  146. let cList = rList[i].querySelectorAll('a');
  147. for (let n = cList.length; n > 0; ) {
  148. --n;
  149. if (cList[n].href.match(rogueRegExp)) {
  150. rList[i].remove();
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. };
  157. // 检查并执行已启用的功能
  158. const runScript = async () => {
  159. let loopNum = 3; // 执行次数
  160. let timeout = 5; // 间隔时间
  161. while (loopNum--) {
  162. Object.keys(scriptCfgs).forEach((key) => {
  163. GM_setValue(key, scriptCfgs[key]); // 保存脚本配置
  164. if (scriptCfgs[key]) Steps[key](); // 执行脚本功能
  165. });
  166. for (let i = timeout; i-- > 0; ) await Sleep(1000);
  167. }
  168. };
  169. // 监听页面变化
  170. document.querySelector('title').addEventListener('DOMNodeInserted', () => runScript(), false);
  171. // 运行脚本
  172. runScript();
  173. })();
  174. (async()=>{
  175. // 识别必应网页
  176. if (!location.host.endsWith('bing.com')) return;
  177. // 读取脚本配置
  178. let scriptCfgs = {
  179. unRogue: GM_getValue('unRogue', 'hao123.com|2345.com') // 屏蔽流氓网站(默认启用,清空参数表示禁用)
  180. };
  181. // 创建sleep方法(用于async/await的延时处理)
  182. const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  183. // 功能模块
  184. const Steps = {
  185. unRogue: async()=>{
  186. let rogueList = scriptCfgs.unRogue;
  187. let rogueRegExp = new RegExp(rogueList);
  188. let rList = document.querySelectorAll(`main>ol>li`);
  189. for (let i = rList.length; i > 0; ) {
  190. --i;
  191. let cList = rList[i].querySelectorAll('a');
  192. for (let n = cList.length; n > 0; ) {
  193. --n;
  194. if (cList[n].href.match(rogueRegExp)) {
  195. rList[i].remove();
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. };
  202. // 检查并执行已启用的功能
  203. const runScript = async () => {
  204. let loopNum = 3; // 执行次数
  205. let timeout = 5; // 间隔时间
  206. while (loopNum--) {
  207. Object.keys(scriptCfgs).forEach((key) => {
  208. GM_setValue(key, scriptCfgs[key]); // 保存脚本配置
  209. if (scriptCfgs[key]) Steps[key](); // 执行脚本功能
  210. });
  211. for (let i = timeout; i-- > 0; ) await Sleep(1000);
  212. }
  213. };
  214. // 监听页面变化
  215. document.querySelector('title').addEventListener('DOMNodeInserted', () => runScript(), false);
  216. // 运行脚本
  217. runScript();
  218. })();