Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Base64 Link Decoder v5
- // @namespace http://tampermonkey.net/
- // @version 2.1
- // @description Automatically decodes and replaces base64-encoded links
- // @author You
- // @match *://*/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Function to check if a string is valid base64
- function isBase64(str) {
- if (!str || str.length < 4) return false;
- // Base64 pattern
- if (!/^[A-Za-z0-9+/]+={0,2}$/.test(str)) return false;
- try {
- // Try to decode it - if it works, it's valid base64
- const decoded = atob(str);
- // Verify by encoding it back (with proper padding)
- const reencoded = btoa(decoded);
- // The reencoded version might have different padding, so compare without padding
- return str.replace(/=+$/, '') === reencoded.replace(/=+$/, '');
- } catch (err) {
- return false;
- }
- }
- // Function to decode base64 and check if it's a URL
- function decodeIfUrl(str) {
- try {
- const decoded = atob(str);
- // Check if decoded string looks like a URL (more permissive check)
- if (decoded.match(/^https?:\/\//i) ||
- decoded.match(/^www\./i) ||
- decoded.match(/^ftp:\/\//i) ||
- decoded.includes('://')) {
- return decoded;
- }
- } catch (err) {
- // Not valid base64
- }
- return null;
- }
- // Function to process a single link element
- function processLink(link) {
- const href = link.href;
- // Check if the entire href is base64 encoded
- if (isBase64(href)) {
- const decoded = decodeIfUrl(href);
- if (decoded) {
- link.href = decoded;
- link.title = `Decoded from: ${href.substring(0, 30)}...`;
- return;
- }
- }
- // Check for base64 in URL parameters
- try {
- const url = new URL(href);
- let modified = false;
- url.searchParams.forEach((value, key) => {
- if (isBase64(value)) {
- const decoded = decodeIfUrl(value);
- if (decoded) {
- url.searchParams.set(key, decoded);
- modified = true;
- }
- }
- });
- if (modified) {
- link.href = url.toString();
- link.title = `Decoded parameter from original URL`;
- }
- } catch (err) {
- // Not a valid URL, skip
- }
- }
- // Function to process text nodes and convert base64 strings to links
- function processTextNode(node) {
- const text = node.textContent;
- // Look for base64-like patterns (at least 16 chars to avoid false positives)
- const base64Pattern = /[A-Za-z0-9+/]{16,}={0,2}/g;
- const matches = text.match(base64Pattern);
- if (!matches) return;
- console.log('Found potential base64 strings:', matches);
- let modified = false;
- let newHTML = text;
- const replacements = [];
- matches.forEach(match => {
- console.log('Testing if valid base64:', match, 'Result:', isBase64(match));
- if (isBase64(match)) {
- const decoded = decodeIfUrl(match);
- console.log('Decoded result:', decoded);
- if (decoded) {
- console.log('✓ Successfully decoded base64:', match, '->', decoded);
- replacements.push({ original: match, decoded: decoded });
- }
- }
- });
- // Apply replacements (do this separately to avoid replacing parts of already replaced text)
- replacements.forEach(({ original, decoded }) => {
- const link = `<a href="${decoded}" title="Decoded from base64" style="color: #4a9eff; text-decoration: underline;">${decoded}</a>`;
- newHTML = newHTML.replace(original, link);
- modified = true;
- });
- if (modified) {
- const parent = node.parentNode;
- const wrapper = document.createElement('span');
- wrapper.innerHTML = newHTML;
- parent.replaceChild(wrapper, node);
- }
- }
- // Walk through all text nodes in an element
- function walkTextNodes(element) {
- const walker = document.createTreeWalker(
- element,
- NodeFilter.SHOW_TEXT,
- null,
- false
- );
- const textNodes = [];
- let currentNode;
- while (currentNode = walker.nextNode()) {
- // Skip script and style tags, but INCLUDE code tags
- const parent = currentNode.parentNode;
- if (parent && parent.tagName !== 'SCRIPT' && parent.tagName !== 'STYLE' && parent.tagName !== 'A') {
- textNodes.push(currentNode);
- }
- }
- console.log('Processing', textNodes.length, 'text nodes');
- textNodes.forEach(processTextNode);
- }
- // Also process code elements specifically
- function processCodeElements() {
- const codeElements = document.querySelectorAll('code');
- console.log('Found', codeElements.length, 'code elements');
- codeElements.forEach(code => {
- const text = code.textContent.trim();
- console.log('Checking code element:', text);
- if (isBase64(text)) {
- const decoded = decodeIfUrl(text);
- console.log('Code element base64 decoded to:', decoded);
- if (decoded) {
- const link = document.createElement('a');
- link.href = decoded;
- link.textContent = decoded;
- link.title = 'Decoded from base64';
- link.style.color = '#4a9eff';
- link.style.textDecoration = 'underline';
- code.replaceWith(link);
- }
- }
- });
- }
- // Process all existing links
- function processAllLinks() {
- const links = document.querySelectorAll('a[href]');
- links.forEach(processLink);
- }
- // Initial processing
- processAllLinks();
- walkTextNodes(document.body);
- processCodeElements();
- // Watch for dynamically added content
- const observer = new MutationObserver((mutations) => {
- mutations.forEach((mutation) => {
- mutation.addedNodes.forEach((node) => {
- if (node.nodeType === 1) { // Element node
- if (node.tagName === 'A' && node.href) {
- processLink(node);
- }
- // Check children for links
- const links = node.querySelectorAll ? node.querySelectorAll('a[href]') : [];
- links.forEach(processLink);
- // Check for code elements
- if (node.tagName === 'CODE') {
- const text = node.textContent.trim();
- if (isBase64(text)) {
- const decoded = decodeIfUrl(text);
- if (decoded) {
- const link = document.createElement('a');
- link.href = decoded;
- link.textContent = decoded;
- link.title = 'Decoded from base64';
- link.style.color = '#4a9eff';
- link.style.textDecoration = 'underline';
- node.replaceWith(link);
- }
- }
- }
- // Check for text nodes with base64
- if (node.querySelectorAll) {
- walkTextNodes(node);
- const codes = node.querySelectorAll('code');
- codes.forEach(code => {
- const text = code.textContent.trim();
- if (isBase64(text)) {
- const decoded = decodeIfUrl(text);
- if (decoded) {
- const link = document.createElement('a');
- link.href = decoded;
- link.textContent = decoded;
- link.title = 'Decoded from base64';
- link.style.color = '#4a9eff';
- link.style.textDecoration = 'underline';
- code.replaceWith(link);
- }
- }
- });
- }
- } else if (node.nodeType === 3) { // Text node
- processTextNode(node);
- }
- });
- });
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- console.log('Base64 Link Decoder: Active - Processing links and text');
- })();
Add Comment
Please, Sign In to add comment