Greasy Fork

Greasy Fork is available in English.

自动重定向

替换重定向的链接

当前为 2023-01-30 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(() => {
    function changHref(ele) {
        try {
            if (ele.href) {
                fetch(new URL(ele.href, location.href).toString(), {
                    method: "HEAD",
                })
                    .then((e) => {
                        if (e.url && e.url !== ele.href) {
                            ele.href = e.url;
                        }
                    })
                    .catch((e) => console.error(e));
            }
        } catch (e) {
            console.error(e);
        }
    }
    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) => {
        if (e.target instanceof HTMLAnchorElement) {
            changHref(e.target);
        }
    });
})();