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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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