百度反重定向

将百度重定向网址解析为直接网址

当前为 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-unRedirect
// @name:zh-CN           百度反重定向
// @namespace            https://greasyfork.org/zh-CN/users/42351
// @version              1.0
// @description          Resolve Baidu 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/*
// @grant                GM_xmlhttpRequest
// @connect              *
// @run-at               document-end
// @compatible           chrome 测试通过
// @compatible           firefox 未测试
// @compatible           opera 未测试
// @compatible           safari 未测试
// ==/UserScript==

(() => {
	// 创建sleep方法(用于async/await的延时处理)
	const Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

	// 解析百度重定向地址
	(async () => {
		while (!document.querySelector(`h3 a`)) await Sleep(1000);
		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();
					}
				}
			});
		});
	})();
})();