搜索引擎过滤器

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

当前为 2021-02-28 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                 SearchEngine-Filter
// @name:zh-CN           搜索引擎过滤器
// @namespace            https://greasyfork.org/zh-CN/users/42351
// @version              1.6
// @description          Filter search page spam, And resolve redirect URL into direct
// @description:zh-CN    过滤搜索页垃圾信息,并将重定向网址解析为直接网址
// @icon64               https://antecer.gitlab.io/amusingdevice/icon/antecer.ico
// @icon                 https://antecer.gitlab.io/amusingdevice/icon/antecer.ico
// @author               Antecer
// @include              http*://*.baidu.com/s?*
// @include              http*://*.bing.com/search?*
// @grant                GM_xmlhttpRequest
// @grant                GM_getValue
// @grant                GM_setValue
// @connect              *
// @run-at               document-body
// @compatible           chrome 测试通过
// @compatible           firefox 未测试
// @compatible           opera 未测试
// @compatible           safari 未测试
// ==/UserScript==

(async () => {
    // 识别百度网页
    if (!location.host.endsWith('baidu.com')) return;
	// 读取脚本配置
	let scriptCfgs = {
		unRedirect: GM_getValue('unRedirect', true), // 反重定向(默认启用)
		unExperience: GM_getValue('unExperience', true), // 屏蔽百度经验(默认启用)
		unOtherQuery: GM_getValue('unOtherQuery', true), // 屏蔽其他人还在搜(默认启用)
		unExpert: GM_getValue('unExpert', true), // 屏蔽百度健康(默认启用)
		unHotQuery: GM_getValue('unHotQuery', false), // 屏蔽百度热搜榜(默认禁用)
		unSimilar: GM_getValue('unSimilar', false), // 屏蔽相关搜索(默认禁用)
		unGame: GM_getValue('unGame', false), // 屏蔽百度游戏(默认禁用)
		unTuiguang: GM_getValue('unTuiguang', true), // 屏蔽百度推广(默认启用)
		adBlock: GM_getValue('adBlock', true), // 屏蔽广告(默认启用)
		unRogue: GM_getValue('unRogue', 'hao123.com|2345.com') // 屏蔽流氓网站(默认启用,清空参数表示禁用)
	};

	// 创建sleep方法(用于async/await的延时处理)
	const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
	// 创建超时变量,防止长时间循环遍历document
	const timeoutValue = 10;
	// 创建选择器,用于判断element是否已加载
	const isBodyLoading = async (css) => {
		let timeout = timeoutValue;
		while (!document.querySelector(css) && timeout) {
			--timeout;
			await Sleep(1000);
		}
	};

	// 功能模块
	const Steps = {
		unRedirect: async () => {
			await isBodyLoading(`[href*="baidu.com/link?"],[href*="baidu.com/api/proxy?"]`);
			let allowUpgrade = document.createElement(`meta`);
			allowUpgrade.setAttribute('http-equiv', 'Content-Security-Policy');
			allowUpgrade.setAttribute('content', 'upgrade-insecure-requests');
			document.head.append(allowUpgrade);
			document.querySelectorAll(`[href*="baidu.com/link?"],[href*="baidu.com/api/proxy?"]`).forEach((element) => {
				(async (item) => {
					let reTry = false;
					let thisXhr = GM_xmlhttpRequest({
						url: item.href,
						method: 'GET',
						onreadystatechange: (result) => {
							if (result.readyState > 2) {
								item.href = result.finalUrl;
								thisXhr.abort();
							}
						},
						onerror: (err) => {
							reTry = true;
							console.error(`[Baidu-Filter] Call HEAD Failed!`, err);
						}
					});
				})(element);
			});
		},
		unExperience: async () => {
			await isBodyLoading(`[href*="jingyan.baidu.com"]`);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`[href*="jingyan.baidu.com"]`)) item.remove();
			});
		},
		unOtherQuery: async () => {
			await isBodyLoading(`.result-op`);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.innerHTML.includes(`>其他人还在搜<`)) item.remove();
			});
		},
		unExpert: async () => {
			while (!document.querySelector(`[href*="expert.baidu.com"]`)) await Sleep(1000);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`[href*="expert.baidu.com"]`)) item.remove();
			});
		},
		unTuiguang: async () => {
			while (!document.querySelector(`[data-tuiguang]`)) await Sleep(1000);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`span[data-tuiguang]`)) item.remove();
			});
		},
		unGame: async()=> {
			await isBodyLoading(`a[href*="lewan.baidu.com"]`);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`a[href*="lewan.baidu.com"]`)) item.remove();
			});
		},
		unHotQuery: async () => {
			await isBodyLoading(`[title="百度热榜"]`);
			document.querySelectorAll(`#content_right`).forEach((item) => {
				if (item.querySelector(`[title="百度热榜"]`)) item.remove();
			});
		},
		unSimilar: async () => {
			while (!document.querySelector(`#rs`)) await Sleep(1000);
			document.querySelectorAll(`[id="rs"]`).forEach((item) => {
				if (item.querySelector(`table a[href^="/s?wd="]`)) item.remove();
			});
		},
		adBlock: async () => {
			while (!document.querySelector(`.ec_tuiguang_pplink`)) await Sleep(1000);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.innerHTML.includes(`>广告</span>`)) item.remove();
			});
		},
		unRogue: async () => {
			let timeout = timeoutValue;
			let rogueList = scriptCfgs.unRogue;
			let rogueRegExp = new RegExp(rogueList);
			while (rogueList && timeout) {
				--timeout;
				let nodes = document.querySelectorAll('a');
				for (let i = nodes.length; i > 0; ) {
					if (nodes[--i].href.match(rogueRegExp)) {
						i = timeout = 0;
					}
				}
				await Sleep(1000);
			}
			let rList = document.querySelectorAll(`#content_left>div`);
			for (let i = rList.length; i > 0; ) {
				--i;
				let cList = rList[i].querySelectorAll('a');
				for (let n = cList.length; n > 0; ) {
					--n;
					if (cList[n].href.match(rogueRegExp)) {
						rList[i].remove();
						break;
					}
				}
			}
		}
	};

	// 检查并执行已启用的功能
	const runScript = async () => {
		let loopNum = 3; // 执行次数
		let timeout = 5; // 间隔时间
		while (loopNum--) {
			Object.keys(scriptCfgs).forEach((key) => {
				GM_setValue(key, scriptCfgs[key]); // 保存脚本配置
				if (scriptCfgs[key]) Steps[key](); // 执行脚本功能
			});
			for (let i = timeout; i-- > 0; ) await Sleep(1000);
		}
	};
	// 监听页面变化
	document.querySelector('title').addEventListener('DOMNodeInserted', () => runScript(), false);
	// 运行脚本
	runScript();
})();

(async()=>{
    // 识别必应网页
    if (!location.host.endsWith('bing.com')) return;
    // 读取脚本配置
	let scriptCfgs = {
		unRogue: GM_getValue('unRogue', 'hao123.com|2345.com') // 屏蔽流氓网站(默认启用,清空参数表示禁用)
	};
	// 创建sleep方法(用于async/await的延时处理)
	const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
	// 功能模块
	const Steps = {
        unRogue: async()=>{
			let rogueList = scriptCfgs.unRogue;
			let rogueRegExp = new RegExp(rogueList);
            let rList = document.querySelectorAll(`main>ol>li`);
            for (let i = rList.length; i > 0; ) {
				--i;
				let cList = rList[i].querySelectorAll('a');
				for (let n = cList.length; n > 0; ) {
					--n;
					if (cList[n].href.match(rogueRegExp)) {
						rList[i].remove();
						break;
					}
				}
			}
        }
    };
	// 检查并执行已启用的功能
	const runScript = async () => {
		let loopNum = 3; // 执行次数
		let timeout = 5; // 间隔时间
		while (loopNum--) {
			Object.keys(scriptCfgs).forEach((key) => {
				GM_setValue(key, scriptCfgs[key]); // 保存脚本配置
				if (scriptCfgs[key]) Steps[key](); // 执行脚本功能
			});
			for (let i = timeout; i-- > 0; ) await Sleep(1000);
		}
	};
	// 监听页面变化
	document.querySelector('title').addEventListener('DOMNodeInserted', () => runScript(), false);
	// 运行脚本
	runScript();
})();