FUXX V2EX || V2EX REDIRECT

Modify all V2EX links to use global.v2ex.co

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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