Guest User

Untitled

a guest
Jan 23rd, 2026
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Base64 Link Decoder v5
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.1
  5. // @description Automatically decodes and replaces base64-encoded links
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Function to check if a string is valid base64
  15. function isBase64(str) {
  16. if (!str || str.length < 4) return false;
  17. // Base64 pattern
  18. if (!/^[A-Za-z0-9+/]+={0,2}$/.test(str)) return false;
  19. try {
  20. // Try to decode it - if it works, it's valid base64
  21. const decoded = atob(str);
  22. // Verify by encoding it back (with proper padding)
  23. const reencoded = btoa(decoded);
  24. // The reencoded version might have different padding, so compare without padding
  25. return str.replace(/=+$/, '') === reencoded.replace(/=+$/, '');
  26. } catch (err) {
  27. return false;
  28. }
  29. }
  30.  
  31. // Function to decode base64 and check if it's a URL
  32. function decodeIfUrl(str) {
  33. try {
  34. const decoded = atob(str);
  35. // Check if decoded string looks like a URL (more permissive check)
  36. if (decoded.match(/^https?:\/\//i) ||
  37. decoded.match(/^www\./i) ||
  38. decoded.match(/^ftp:\/\//i) ||
  39. decoded.includes('://')) {
  40. return decoded;
  41. }
  42. } catch (err) {
  43. // Not valid base64
  44. }
  45. return null;
  46. }
  47.  
  48. // Function to process a single link element
  49. function processLink(link) {
  50. const href = link.href;
  51.  
  52. // Check if the entire href is base64 encoded
  53. if (isBase64(href)) {
  54. const decoded = decodeIfUrl(href);
  55. if (decoded) {
  56. link.href = decoded;
  57. link.title = `Decoded from: ${href.substring(0, 30)}...`;
  58. return;
  59. }
  60. }
  61.  
  62. // Check for base64 in URL parameters
  63. try {
  64. const url = new URL(href);
  65. let modified = false;
  66.  
  67. url.searchParams.forEach((value, key) => {
  68. if (isBase64(value)) {
  69. const decoded = decodeIfUrl(value);
  70. if (decoded) {
  71. url.searchParams.set(key, decoded);
  72. modified = true;
  73. }
  74. }
  75. });
  76.  
  77. if (modified) {
  78. link.href = url.toString();
  79. link.title = `Decoded parameter from original URL`;
  80. }
  81. } catch (err) {
  82. // Not a valid URL, skip
  83. }
  84. }
  85.  
  86. // Function to process text nodes and convert base64 strings to links
  87. function processTextNode(node) {
  88. const text = node.textContent;
  89.  
  90. // Look for base64-like patterns (at least 16 chars to avoid false positives)
  91. const base64Pattern = /[A-Za-z0-9+/]{16,}={0,2}/g;
  92. const matches = text.match(base64Pattern);
  93.  
  94. if (!matches) return;
  95.  
  96. console.log('Found potential base64 strings:', matches);
  97.  
  98. let modified = false;
  99. let newHTML = text;
  100. const replacements = [];
  101.  
  102. matches.forEach(match => {
  103. console.log('Testing if valid base64:', match, 'Result:', isBase64(match));
  104. if (isBase64(match)) {
  105. const decoded = decodeIfUrl(match);
  106. console.log('Decoded result:', decoded);
  107. if (decoded) {
  108. console.log('✓ Successfully decoded base64:', match, '->', decoded);
  109. replacements.push({ original: match, decoded: decoded });
  110. }
  111. }
  112. });
  113.  
  114. // Apply replacements (do this separately to avoid replacing parts of already replaced text)
  115. replacements.forEach(({ original, decoded }) => {
  116. const link = `<a href="${decoded}" title="Decoded from base64" style="color: #4a9eff; text-decoration: underline;">${decoded}</a>`;
  117. newHTML = newHTML.replace(original, link);
  118. modified = true;
  119. });
  120.  
  121. if (modified) {
  122. const parent = node.parentNode;
  123. const wrapper = document.createElement('span');
  124. wrapper.innerHTML = newHTML;
  125. parent.replaceChild(wrapper, node);
  126. }
  127. }
  128.  
  129. // Walk through all text nodes in an element
  130. function walkTextNodes(element) {
  131. const walker = document.createTreeWalker(
  132. element,
  133. NodeFilter.SHOW_TEXT,
  134. null,
  135. false
  136. );
  137.  
  138. const textNodes = [];
  139. let currentNode;
  140. while (currentNode = walker.nextNode()) {
  141. // Skip script and style tags, but INCLUDE code tags
  142. const parent = currentNode.parentNode;
  143. if (parent && parent.tagName !== 'SCRIPT' && parent.tagName !== 'STYLE' && parent.tagName !== 'A') {
  144. textNodes.push(currentNode);
  145. }
  146. }
  147.  
  148. console.log('Processing', textNodes.length, 'text nodes');
  149. textNodes.forEach(processTextNode);
  150. }
  151.  
  152. // Also process code elements specifically
  153. function processCodeElements() {
  154. const codeElements = document.querySelectorAll('code');
  155. console.log('Found', codeElements.length, 'code elements');
  156. codeElements.forEach(code => {
  157. const text = code.textContent.trim();
  158. console.log('Checking code element:', text);
  159. if (isBase64(text)) {
  160. const decoded = decodeIfUrl(text);
  161. console.log('Code element base64 decoded to:', decoded);
  162. if (decoded) {
  163. const link = document.createElement('a');
  164. link.href = decoded;
  165. link.textContent = decoded;
  166. link.title = 'Decoded from base64';
  167. link.style.color = '#4a9eff';
  168. link.style.textDecoration = 'underline';
  169. code.replaceWith(link);
  170. }
  171. }
  172. });
  173. }
  174.  
  175. // Process all existing links
  176. function processAllLinks() {
  177. const links = document.querySelectorAll('a[href]');
  178. links.forEach(processLink);
  179. }
  180.  
  181. // Initial processing
  182. processAllLinks();
  183. walkTextNodes(document.body);
  184. processCodeElements();
  185.  
  186. // Watch for dynamically added content
  187. const observer = new MutationObserver((mutations) => {
  188. mutations.forEach((mutation) => {
  189. mutation.addedNodes.forEach((node) => {
  190. if (node.nodeType === 1) { // Element node
  191. if (node.tagName === 'A' && node.href) {
  192. processLink(node);
  193. }
  194. // Check children for links
  195. const links = node.querySelectorAll ? node.querySelectorAll('a[href]') : [];
  196. links.forEach(processLink);
  197.  
  198. // Check for code elements
  199. if (node.tagName === 'CODE') {
  200. const text = node.textContent.trim();
  201. if (isBase64(text)) {
  202. const decoded = decodeIfUrl(text);
  203. if (decoded) {
  204. const link = document.createElement('a');
  205. link.href = decoded;
  206. link.textContent = decoded;
  207. link.title = 'Decoded from base64';
  208. link.style.color = '#4a9eff';
  209. link.style.textDecoration = 'underline';
  210. node.replaceWith(link);
  211. }
  212. }
  213. }
  214.  
  215. // Check for text nodes with base64
  216. if (node.querySelectorAll) {
  217. walkTextNodes(node);
  218. const codes = node.querySelectorAll('code');
  219. codes.forEach(code => {
  220. const text = code.textContent.trim();
  221. if (isBase64(text)) {
  222. const decoded = decodeIfUrl(text);
  223. if (decoded) {
  224. const link = document.createElement('a');
  225. link.href = decoded;
  226. link.textContent = decoded;
  227. link.title = 'Decoded from base64';
  228. link.style.color = '#4a9eff';
  229. link.style.textDecoration = 'underline';
  230. code.replaceWith(link);
  231. }
  232. }
  233. });
  234. }
  235. } else if (node.nodeType === 3) { // Text node
  236. processTextNode(node);
  237. }
  238. });
  239. });
  240. });
  241.  
  242. observer.observe(document.body, {
  243. childList: true,
  244. subtree: true
  245. });
  246.  
  247. console.log('Base64 Link Decoder: Active - Processing links and text');
  248. })();
  249.  
Add Comment
Please, Sign In to add comment