URL Expander

Expand TinyURL, Reurl.cc, and Bit.ly links to their original URLs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         URL Expander
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Expand TinyURL, Reurl.cc, and Bit.ly links to their original URLs
// @author       ChatGPT
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(async function () {
    'use strict';

    function replaceLink(element, newUrl) {
        element.href = newUrl;
    }

    async function expandTinyUrl(tinyUrl) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: tinyUrl.replace('://tinyurl.com/', '://preview.tinyurl.com/'),
                onload: function (response) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, "text/html");
                    const redirectUrl = doc.querySelector("#redirecturl");
                    if (redirectUrl) {
                        resolve(redirectUrl.href);
                    } else {
                        reject("Original URL not found");
                    }
                },
                onerror: function (error) {
                    reject(error);
                }
            });
        });
    }

    async function expandReurl(reurl) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: reurl + "+",
                onload: function (response) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, "text/html");
                    const redirectUrl = doc.querySelector(".row .col-md-12.text-center a");
                    if (redirectUrl) {
                        resolve(redirectUrl.href);
                    } else {
                        reject("Original URL not found");
                    }
                },
                onerror: function (error) {
                    reject(error);
                }
            });
        });
    }

    async function expandBitly(bitlyUrl) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: bitlyUrl + "+",
                onload: function (response) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, "text/html");
                    const redirectUrl = doc.querySelector(".item-detail--url");
                    if (redirectUrl) {
                        resolve(redirectUrl.textContent.trim());
                    } else {
                        reject("Original URL not found");
                    }
                },
                onerror: function (error) {
                    reject(error);
                }
            });
        });
    }

    const tinyUrls = document.querySelectorAll('a[href^="https://tinyurl.com/"]');
    for (const link of tinyUrls) {
        try {
            const expandedUrl = await expandTinyUrl(link.href);
            replaceLink(link, expandedUrl);
        } catch (error) {
            console.error(`Error expanding TinyURL: ${error}`);
        }
    }

    const reurls = document.querySelectorAll('a[href^="https://reurl.cc/"]');
    for (const link of reurls) {
        try {
            const expandedUrl = await expandReurl(link.href);
            replaceLink(link, expandedUrl);
        } catch (error) {
            console.error(`Error expanding Reurl.cc: ${error}`);
        }
    }

    const bitlyUrls = document.querySelectorAll('a[href^="https://bit.ly/"]');
    for (const link of bitlyUrls) {
        try {
            const expandedUrl = await expandBitly(link.href);
            replaceLink(link, expandedUrl);
        } catch (error) {
            console.error(`Error expanding Bit.ly: ${error}`);
        }
    }
})();