imgur Proxy

For all my fellow brits out there, I grant your biggest wish. Maybe not your BIGGEST, but a big one nontheless.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name          imgur Proxy
// @namespace     JD's userscripts
// @description   For all my fellow brits out there, I grant your biggest wish. Maybe not your BIGGEST, but a big one nontheless.
// @author        JD
// @version       1.1
// @match         *://*/*
// @exclude       *://imgur.fsky.io/*
// @grant         none
// @run-at        document-idle
// ==/UserScript==

const rimgoInstance = "imgur.fsky.io";

if (location.hostname.includes("imgur.com")) {
    let newUrl = location.href;
    newUrl = newUrl.replace(/^(https?:\/\/)(www\.|i\.)?imgur\.com/i, `$1${rimgoInstance}`);

    if (newUrl !== location.href) {
        location.replace(newUrl);
    }
}

function replaceImgurLinks(scope) {
    const imgElements = scope.querySelectorAll(`img[src*="imgur.com"]:not([src*="${rimgoInstance}"])`);

    imgElements.forEach(img => {
        img.src = img.src.replace(/^(https?:\/\/)(www\.|i\.)?imgur\.com/i, `$1${rimgoInstance}`);
    });

    const aElements = scope.querySelectorAll(`a[href*="imgur.com"]:not([href*="${rimgoInstance}"])`);

    aElements.forEach(a => {
        a.href = a.href.replace(/^(https?:\/\/)(www\.|i\.)?imgur\.com/i, `$1${rimgoInstance}`);
    });
}

replaceImgurLinks(document);

const observer = new MutationObserver(mutationsList => {
    for (const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1) {
                    replaceImgurLinks(node);
                }
            });
        }
    }
});

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