Baidu-Filter

Filter search page spam, And resolve redirect URL into direct

目前為 2021-01-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                 Baidu-Filter
// @name:zh-CN           百度搜索过滤器
// @namespace            https://greasyfork.org/zh-CN/users/42351
// @version              1.1
// @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), // 屏蔽百度健康(默认启用)
		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);
		},
		adBlock: async () => {}
	};

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