Reddit B64 Decoder

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

当前为 2024-08-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Reddit B64 Decoder
// @namespace    https://greasyfork.org/es/scripts/476028
// @version      1.5.4
// @description  Decode Base64-encoded text in Reddit posts and convert URLs to clickable links.
// @author       Shu2Ouma
// @icon         https://www.redditstatic.com/shreddit/assets/favicon/192x192.png
// @license      MIT
// @match        https://*.reddit.com/r/*
// @grant        none
// ==/UserScript==

// Mapa de íconos con nombre de página y URL del ícono
const iconMap = {
  'mediafire.com': { name: 'Mediafire', iconUrl: 'https://img.icons8.com/?size=512&id=IYtT6OXWgtB2&format=png' },
  'mega.nz': { name: 'MEGA', iconUrl: 'https://img.icons8.com/?size=512&id=doAYX1PwPI9p&format=png' },
  '1fichier.com': { name: '1Fichier', iconUrl: 'https://i.imgur.com/rcwgt7W.png' },
  'youtube.com': { name: 'YouTube', iconUrl: 'https://img.icons8.com/?size=512&id=19318&format=png' },
  'youtu.be': { name: 'YouTube', iconUrl: 'https://img.icons8.com/?size=512&id=19318&format=png' },
  'reddit.com': { name: 'Reddit', iconUrl: 'https://img.icons8.com/?size=512&id=FLisMqR76b1i&format=png' },
  'bilibili.com': { name: 'Bilibili', iconUrl: 'https://img.icons8.com/?size=512&id=5E24fZ9ORelo&format=png' },
  'video.twimg.com': { name: 'Twitter', iconUrl: 'https://img.icons8.com/?size=512&id=yoQabS8l0qpr&format=png' },
  'twitter.com': { name: 'Twitter', iconUrl: 'https://img.icons8.com/?size=512&id=yoQabS8l0qpr&format=png' },
  'x.com': { name: 'Twitter', iconUrl: 'https://img.icons8.com/?size=512&id=yoQabS8l0qpr&format=png' },
  'drive.google.com': { name: 'Google Drive', iconUrl: 'https://img.icons8.com/?size=512&id=ya4CrqO7PgnY&format=png' },
  'filecrypt.cc': { name: 'Filecrypt', iconUrl: 'https://img.icons8.com/?size=512w&id=I2lKi8lyTaJD&format=png' },
  'qiwi.gg': { name: 'Qiwi', iconUrl: 'https://img.icons8.com/?size=512w&id=1GrV0yCPr1LJ&format=png' },
  'image': { name: 'Imagen', iconUrl: 'https://img.icons8.com/?size=512&id=42840&format=png' },
  'default': { name: 'Enlace', iconUrl: 'https://img.icons8.com/?size=512w&id=n9d0Hm43JCPK&format=png' }
};

// Función para decodificar texto codificado en Base64 y convertir URLs en íconos clicables
function decodeBase64AndLinkify(text) {
  const base64Regex = /([A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)*/g;

  return text.replace(base64Regex, match => {
    try {
      const decoded = atob(match);
      if (decoded.includes('https://') || decoded.includes('http://')) {
        return decoded.replace(/(https?:\/\/[^\s<]+)/g, url => {
          try {
            const { hostname, pathname } = new URL(url);
            const cleanedHostname = hostname.replace('www.', '');

            // Determinar si es una URL de imagen
            const isImage = /\.(jpg|jpeg|png|gif|webp|svg|bmp|tiff|ico)$/i.test(pathname);

            const icon = isImage ? iconMap['image'] : iconMap[cleanedHostname] || iconMap['default'];
            if (icon) {
              return `<a href="${url}" target="_blank" rel="noreferrer noopener" style="text-decoration: none; display: inline-flex; align-items: center; line-height: 20px;">
                        <span style="position: relative; display: inline-block;">
                          <img src="${icon.iconUrl}" alt="${icon.name}" style="width: 20px; height: 20px; position: absolute; left: 0; top: 50%; transform: translateY(-50%);" />
                          <span style="padding-left: 24px; line-height: 20px;">${icon.name}</span>
                        </span>
                      </a><br>`;
            } else {
              return `<a href="${url}" target="_blank" rel="noreferrer noopener" style="text-decoration: underline; text-underline-offset: 2px;">${url}</a><br>`;
            }
          } catch (urlError) {
            console.error(`Error processing URL: ${url}`, urlError);
            return `${url}<br>`;
          }
        });
      }
    } catch (error) {
      console.error(`Error decoding Base64: ${match}`, error);
    }
    return match;
  });
}

// Function to process Reddit posts on the page
function processRedditPosts(container) {
  const postContents = container.querySelectorAll('p');
  postContents.forEach(post => {
    const decodedText = decodeBase64AndLinkify(post.textContent);
    if (decodedText !== post.textContent) {
      post.innerHTML = decodedText;
    }
  });
}

// Execute the script with a 2.0-second delay after the page has finished loading
function initialProcess() {
  const container = document.querySelector('#main-content');
  if (container) {
    processRedditPosts(container);
  }
}

// Handle click events to check for new elements
function handleClick() {
  setTimeout(() => {
    const container = document.querySelector('#main-content');
    if (container) {
      processRedditPosts(container);
    }
  }, 1000); // Delay to ensure content has loaded
}

// Observe changes in the DOM within #main-content
const observer = new MutationObserver(mutationsList => {
  mutationsList.forEach(mutation => {
    if (mutation.addedNodes.length > 0) {
      mutation.addedNodes.forEach(node => {
        if (node instanceof HTMLElement) {
          const container = node.closest('#main-content');
          if (container) {
            processRedditPosts(container);
          }
        }
      });
    }
  });
});

// Start observing changes in the #main-content container
const mainContentContainer = document.querySelector('#main-content');
if (mainContentContainer) {
  observer.observe(mainContentContainer, { childList: true, subtree: true });
}

// Add event listener for clicks to check for new elements
document.addEventListener('click', handleClick);

// Initial processing after the page loads
window.addEventListener('load', () => {
  setTimeout(initialProcess, 2000);
});