Greasy Fork

Greasy Fork is available in English.

自动重定向

替换重定向的链接

当前为 2023-02-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         自动重定向
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  替换重定向的链接
// @author       share121
// @match        *://*/*
// @run-at       document-start
// @license      MIT
// @connect      *
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(() => {
    /*
    function changHref(ele) {
        try {
            if (ele.href) {
                fetch(new URL(ele.href, location.href).toString(), {
                    method: "HEAD",
                })
                    .then((e) => {
                        if (e.url && new URL(e.url).host !== new URL(ele.href, location.href).host) {
                            ele.href = e.url;
                        }
                    })
                    .catch((e) => console.error(e));
            }
        } catch (e) {
            console.error(e);
        }
    }
    */
    function changHref(ele) {
        try {
            if (ele.href) {
                GM.xmlHttpRequest({
                    method: "HEAD",
                    url: new URL(ele.href, location.href).toString(),
                    onload: function (response) {
                        if (
                            response.finalUrl &&
                            new URL(response.finalUrl).host !==
                                new URL(ele.href, location.href).host
                        ) {
                            ele.href = response.finalUrl;
                        }
                    },
                    onerror: function (response) {
                        console.error(response);
                    },
                });
            }
        } catch (e) {
            console.error(e);
        }
    }
    function isA(ele) {
        while (ele) {
            if (ele instanceof HTMLAnchorElement) {
                return ele;
            }
            ele = ele.parentNode;
        }
        return false;
    }
    new MutationObserver((mutationList) => {
        mutationList.forEach((e) => {
            if (e.type === "attributes") {
                if (e.target instanceof HTMLAnchorElement) {
                    changHref(e.target);
                }
            } else {
                e.addedNodes.forEach((ele) => {
                    if (ele instanceof HTMLAnchorElement) {
                        changHref(ele);
                    }
                });
            }
        });
    }).observe(document, {
        subtree: true,
        childList: true,
        attributeFilter: ["href"],
    });
    window.addEventListener("mouseover", (e) => {
        let a = isA(e.target);
        if (a) {
            changHref(a);
        }
    });
})();