V2EX Imgur Proxy

Replace Imgur links with DuckDuckGo proxy on V2EX

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