Base64 Decoding Userscripts

// ==UserScript==
// @name            Base64 Decoder Passive
// @description     Right Click, Tampermonkey to Decode Base64
// @version         0.1
// @author          Sidd065
// @match           *://*/*
// @grant           none
// @run-at          context-menu
// ==/UserScript==

function decode(str) {
    try{return decodeURIComponent(escape(window.atob(str)));}
    catch{return str;}
}
(function() {
    'use strict';
    let body = document.body.innerText.split('\n'), words = [];
    for (let i = 0; i < body.length; i++){
        words = words.concat(body[i].split(' '));}
    for (let i = 0; i < words.length; i++){
        if (words[i].endsWith("=")){
            document.body.innerHTML=document.body.innerHTML.replace(words[i],decode(words[i]));}}
})();

Aggressive Base64 Decoder

Because some base64 links don't end with =, the following script is more aggressive, but it can break some UI stuff.

// ==UserScript==
// @name            Base64 Decoder Aggressive
// @description     Right Click, Tampermonkey to Decode Base64
// @version         0.2
// @author          Sidd065
// @match           *://*/*
// @grant           none
// @run-at          context-menu
// ==/UserScript==

function decode(str) {
    try{return decodeURIComponent(escape(window.atob(str)));}
    catch{return false;}
}
(function() {
    'use strict';
    let body = document.body.innerText.split('\n'), words = [];
    for (let i = 0; i < body.length; i++){
        words = words.concat(body[i].split(' '));}
    for (let i = 0; i < words.length; i++){
        let temp = decode(words[i]);
        if (temp){
            document.body.innerHTML=document.body.innerHTML.replace(words[i],temp);}}
})();

Automatic Base64 Decoder

Every 500 ms, this userscript will auto check for, find, and decode all base64 links. This might be redundant for those who don't often view these kinds of links on a regular basis, but this script is more faster in the sense the process is automated entirely (no right clicking to decode).

// ==UserScript==
// @name         Auto Decoder for all sites
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Decode Base64 encoded links for all sites
// @author       Coder
// @match        *://**/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';
    const base64Regex = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{20,})$/g

    // Must set an interval, otherwise the content that is lazy loaded (e.g. loaded on scrolling) will not get decoded
    setInterval(() => {
        const pTags = document.querySelectorAll('p')

        pTags.forEach(pTag => {
            // Split the string into an array and check each element for a base64 encoded string
            const pTagText = pTag.innerText.split(/\s+/);

            pTagText.forEach(text => {
                if (base64Regex.test(text)) {
                    // If the string is a base64 encoded string, decode it and replace the p tag with the decoded string
                    pTag.innerText = pTag.innerText.replace(text, atob(text));
                    const txt = pTag.innerText.split("\n");
                    const links = [];
                    txt.forEach(link => {
                        links.push("<a href='" + link + "'>" + link + "</a>");
                    });
                    pTag.outerHTML = links.join("\n");
                }
            });
        })
    }, 500)
})();

Base64 "Extensions"

These decode and encode one link at a time, and auto send you to the link that is decoded. I would only recommend them for people using sites where links are not easily auto-decoded (Discord), or they only encounter a few Base64 links regularly (not a whole website worth or anything like that).

Base64 Decode Extension

javascript: (function () {var e64Str=prompt("Enter encoded base64 string here.","");null==e64Str||""==e64Str?alert("No string to decode."):confirm("Do you want to go to this link?\n"+atob(e64Str))&&window.open(atob(e64Str));}())

Base64 Encode Extension

javascript: (function () {var d64Str=prompt("Enter string to encode to base64.","");null==d64Str||""==d64Str?alert("No string to encode."):prompt("Copy and share your encoded message!",btoa(d64Str.trim()));}())

To add these, you must right click your favorites (Bookmarks Toolbar if you have Firefox) bar (underneath the URL Bar). Click "Add Page" ("Add Bookmark" if you have Firefox). Change the name to "Base64 Encode", then put the code pasted above into the "URL" form. Add another page using "Add page" or "Add Bookmark", but change the name to "Base64 Decode" and add the code pasted above into the "URL" form.

Edit
Pub: 17 May 2022 12:27 UTC
Edit: 17 May 2022 12:40 UTC
Views: 1524