FUXX V2EX || V2EX REDIRECT

Modify all V2EX links to use global.v2ex.co

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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 });
})();