Twitch URL & Link Cleaner (Full)

Removes query parameters from Twitch URLs and all <a href> links (absolute and relative) on the page (everything after "?").

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitch URL & Link Cleaner (Full)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Removes query parameters from Twitch URLs and all <a href> links (absolute and relative) on the page (everything after "?").
// @author       DiCK
// @match        https://www.twitch.tv/*
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Remove query parameters from the current page URL
    const cleanCurrentUrl = () => {
        const url = window.location.href;
        const clean = url.split('?')[0];
        if (url !== clean) {
            history.replaceState(null, '', clean);
        }
    };

    // Remove query parameters from all <a> hrefs
    const cleanAllLinks = () => {
        const links = document.querySelectorAll('a[href]');
        links.forEach(link => {
            const href = link.getAttribute('href');
            if (!href) return;

            // Check if it's a Twitch-related link (absolute or relative)
            const isTwitchLink =
                href.startsWith('/') || href.startsWith('https://www.twitch.tv/');

            if (isTwitchLink && href.includes('?')) {
                const cleanHref = href.split('?')[0];
                link.setAttribute('href', cleanHref);
            }
        });
    };

    // Initial cleanup
    cleanCurrentUrl();
    cleanAllLinks();

    // Observe the DOM for new/changed <a> tags (Twitch is a dynamic site)
    const observer = new MutationObserver(() => {
        cleanAllLinks();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    // Re-run cleaning after pushState / replaceState
    const patchHistoryMethod = (method) => {
        const original = history[method];
        history[method] = function () {
            original.apply(this, arguments);
            setTimeout(() => {
                cleanCurrentUrl();
                cleanAllLinks();
            }, 0);
        };
    };

    patchHistoryMethod('pushState');
    patchHistoryMethod('replaceState');

    // Back/forward button
    window.addEventListener('popstate', () => {
        cleanCurrentUrl();
        cleanAllLinks();
    });
})();