Reddit Base64 Decoder

Userscript that automatically detects and decodes Base64 strings on Reddit, replacing them with readable content.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Reddit Base64 Decoder
// @version      1.0
// @description  Userscript that automatically detects and decodes Base64 strings on Reddit, replacing them with readable content.
// @match        https://www.reddit.com/*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/1592770
// ==/UserScript==

(function() {
    const base64Regex = /\b[A-Za-z0-9+/]{20,}={0,2}\b/g;

    function decodeBase64(str) {
        try {
            return decodeURIComponent(escape(atob(str)));
        } catch {
            return null;
        }
    }

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const matches = node.nodeValue.match(base64Regex);
            if (!matches) return;

            let parent = node.parentNode;
            let text = node.nodeValue;

            matches.forEach(match => {
                const decoded = decodeBase64(match);
                if (!decoded || decoded.length < 5) return;

                // Vérifie si c'est une URL
                let isURL = /^https?:\/\/[^\s]+$/.test(decoded);

                let replacement;

                if (isURL) {
                    // crée un lien cliquable
                    const a = document.createElement("a");
                    a.href = decoded;
                    a.textContent = decoded;
                    a.target = "_blank";
                    a.style.color = "#4dabf7"; // optionnel
                    replacement = a;
                } else {
                    // sinon texte simple
                    replacement = document.createTextNode(decoded);
                }

                // remplace dans le texte
                const parts = text.split(match);
                const frag = document.createDocumentFragment();

                parts.forEach((part, index) => {
                    frag.appendChild(document.createTextNode(part));
                    if (index < parts.length - 1) {
                        frag.appendChild(replacement.cloneNode(true));
                    }
                });

                parent.replaceChild(frag, node);
            });
        }
    }

    function scan(element) {
        const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
        let node;
        while (node = walker.nextNode()) {
            processNode(node);
        }
    }

    const observer = new MutationObserver(mutations => {
        mutations.forEach(m => {
            m.addedNodes.forEach(node => {
                scan(node);
            });
        });
    });

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

    scan(document.body);
})();