百度搜索过滤器

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

当前为 2021-01-24 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                 Baidu-Filter
// @name:zh-CN           百度搜索过滤器
// @namespace            https://greasyfork.org/zh-CN/users/42351
// @version              1.2
// @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*://www.baidu.com/s*
// @grant                GM_xmlhttpRequest
// @grant                GM_getValue
// @grant                GM_setValue
// @connect              *
// @run-at               document-body
// @compatible           chrome 测试通过
// @compatible           firefox 未测试
// @compatible           opera 未测试
// @compatible           safari 未测试
// ==/UserScript==

(async () => {
	// 读取脚本配置
	let scriptCfgs = {
		unRedirect: GM_getValue('unRedirect', true), // 反重定向(默认启用)
		unExperience: GM_getValue('unExperience', true), // 屏蔽百度经验(默认启用)
		unOtherQuery: GM_getValue('unOtherQuery', true), // 屏蔽其他人还在搜(默认启用)
		unHotQuery: GM_getValue('unHotQuery', false), // 屏蔽百度热搜榜(默认禁用)
		unExpert: GM_getValue('unExpert', true), // 屏蔽百度健康(默认启用)
		unSimilar: GM_getValue('unSimilar', false), // 屏蔽相关搜索(默认禁用)
		adBlock: GM_getValue('adBlock', true) // 屏蔽广告(默认启用)
	};

	// 创建sleep方法(用于async/await的延时处理)
	const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
	// 创建选择器,用于判断element是否已加载
	const isBodyLoading = async (css) => {
		while (!document.querySelector(css)) await Sleep(1000);
	};

	// 功能模块
	const Steps = {
		unRedirect: async () => {
			await isBodyLoading(`[href*="baidu.com/link?"]`);
			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?"]`).forEach((item) => {
				let thisXhr = GM_xmlhttpRequest({
					url: item.href,
					method: 'HEAD',
					onreadystatechange: (result) => {
						if (result.readyState > 2) {
							item.href = result.finalUrl;
							thisXhr.abort();
						}
					}
				});
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unRedirect(), false);
		},
		unExperience: async () => {
			await isBodyLoading(`[href*="jingyan.baidu.com"]`);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`[href*="jingyan.baidu.com"]`)) item.remove();
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unExperience(), false);
		},
		unOtherQuery: async () => {
			await isBodyLoading(`[href^="/s?rsv"]`);
			document.querySelectorAll(`#content_left>div`).forEach((item) => {
				if (item.querySelector(`[href^="/s?rsv"]`)) item.remove();
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unOtherQuery(), false);
		},
		unHotQuery: async () => {
			await isBodyLoading(`[title="百度热榜"]`);
			document.querySelectorAll(`#content_right`).forEach((item) => {
				if (item.querySelector(`[title="百度热榜"]`)) item.remove();
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unHotQuery(), false);
		},
		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();
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unExpert(), false);
		},
		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();
			});
			document.querySelector('title').addEventListener('DOMNodeInserted', () => Steps.unSimilar(), false);
		},
		adBlock: async () => {}
	};

	// 执行已启用的功能
	Object.keys(scriptCfgs).forEach((key) => {
		GM_setValue(key, scriptCfgs[key]); // 保存脚本配置
		if (scriptCfgs[key]) Steps[key](); // 执行脚本功能
	});
})();