Reddit B64 Decoder

Decode Base64-encoded text in Reddit posts and convert URLs to clickable links.

Ekde 2023/09/25. Vidu La ĝisdata versio.

// ==UserScript==
// @name         Reddit B64 Decoder
// @namespace    https://greasyfork.org/es/scripts/476028-reddit-b64-decoder
// @version      1.1
// @description  Decode Base64-encoded text in Reddit posts and convert URLs to clickable links.
// @author       Shu2Ouma
// @license      MIT
// @match        https://*.reddit.com/r/*
// @grant        none
// ==/UserScript==

// Function to decode Base64-encoded text and convert URLs to clickable links
// Función para decodificar texto codificado en Base64 y convertir URLs en enlaces clicables
function decodeBase64AndLinkify(domElement) {
  const text = domElement.textContent;

  // Regular expression to detect and decode Base64
  // Expresión regular para detectar y decodificar Base64
  const base64Regex = /([A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)*/g;

  const decodedText = text.replace(base64Regex, match => {
    try {
      const decoded = atob(match);
      if (decoded.includes('https://') || decoded.includes('http://')) {
        return (
          decoded
          .replace(/[\r\n]+/g, '<br /><br />')
          .replace(/(https?:\/\/[^\s<]+)/g, `<a href="$1" target="_blank" style='text-decoration: underline #8989ff dotted; text-underline-offset: 2px;'>$1</a>`)
        );
      }
    } catch (error) {
      console.error(`Error decoding Base64: ${match}`, error);
    }
    return match;
  });

  domElement.innerHTML = decodedText;
}

// Function to process Reddit posts on the page
// Función para procesar las publicaciones de Reddit en la página
function processRedditPosts() {
  const postContents = document.querySelectorAll('p');
  postContents.forEach(decodeBase64AndLinkify);
}

// Execute the script with a 1.5-second delay after the page has finished loading
// Ejecutar el script con un retraso de 1.5 segundos después de que la página haya terminado de cargar
window.addEventListener('load', () => {
  setTimeout(processRedditPosts, 1500); // 1500 milisegundos = 1.5 segundos
});

// Observe changes in the DOM after the page has loaded
// Observar cambios en el DOM después de que la página se haya cargado
const observer = new MutationObserver(mutationsList => {
  mutationsList.forEach(mutation => {
    if (mutation.addedNodes.length > 0) {
      mutation.addedNodes.forEach(node => {
        if (node instanceof HTMLElement) {
          const postContents = node.querySelectorAll('p');
          postContents.forEach(decodeBase64AndLinkify);
        }
      });
    }
  });
});

// Start observing changes in the DOM
// Iniciar la observación de cambios en el DOM
observer.observe(document.body, { childList: true, subtree: true });