FUXX V2EX || V2EX REDIRECT

Modify all V2EX links to use global.v2ex.co

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         FUXX V2EX || V2EX REDIRECT
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Modify all V2EX links to use global.v2ex.co
// @author       systemoutprintlnhelloworld 
// @match        *://*/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const DEBUG_MODE = true;

    // 检查并修改链接
    function modifyLinks() {
        // 获取所有链接
        let links = document.querySelectorAll('a[href*="v2ex.com"]');

        // 遍历所有链接
        links.forEach(function(link) {
            // 如果链接包含 .v2ex.com 或 v2ex.com,则替换为 global.v2ex.co
            if (/v2ex\.com/.test(link.href)) {
                let newHref = link.href.replace(/:\/\/(.*\.)?v2ex\.com/, '://global.v2ex.co');
                if (DEBUG_MODE) {
                    console.log(`Modifying link: ${link.href} to ${newHref}`);
                }
                link.href = newHref;
            }
        });
    }

    // 初始修改
    modifyLinks();

    // 监听DOM变化,动态修改新增的链接
    let observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                modifyLinks();
            }
        });
    });

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