V2EX Imgur Proxy

Replace Imgur links with DuckDuckGo proxy on V2EX

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         V2EX Imgur Proxy
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replace Imgur links with DuckDuckGo proxy on V2EX
// @author       You
// @match        https://www.v2ex.com/t/*
// @icon         https://www.google.com/s2/favicons?bb=1&domain=v2ex.com
// @grant        none
// @license      MIT License
// ==/UserScript==

(function() {
    'use strict';

    const proxyPrefix = "https://external-content.duckduckgo.com/iu/?u=";

    function replaceImgur() {
        // 1. Handle actual <img> tags
        const images = document.querySelectorAll('img[src*="i.imgur.com"]');
        images.forEach(img => {
            if (!img.src.includes(proxyPrefix)) {
                img.src = proxyPrefix + img.src;
            }
        });

        // 2. Handle <a> links
        const links = document.querySelectorAll('a[href*="i.imgur.com"]');
        links.forEach(link => {
            if (!link.href.includes(proxyPrefix)) {
                link.href = proxyPrefix + link.href;
            }
        });

        // 3. Handle text-only links in posts (V2EX often auto-links these)
        // This looks for elements containing the specific string
        const walker = document.createTreeWalker(document.getElementById('Wrapper'), NodeFilter.SHOW_TEXT, null, false);
        let node;
        while(node = walker.nextNode()) {
            if (node.nodeValue.includes('https://i.imgur.com/') && !node.parentElement.closest('a')) {
                 // If there's raw text not inside a link, this part is harder to replace without
                 // breaking the DOM, but V2EX usually converts these to <a> tags anyway.
            }
        }
    }

    // Run immediately
    replaceImgur();

    // Also run when the content changes (for AJAX loaded replies or previews)
    const observer = new MutationObserver(replaceImgur);
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();