-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64direct.js
51 lines (43 loc) · 1.4 KB
/
base64direct.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// On click, get base64 string, decode to ASCII, assume result is a url,
// send browser to url.
function go() {
var b64box = document.getElementById("b64box");
if (b64box.reportValidity() === true) {
a = decode(b64box.value);
location.assign(a.pop());
}
return false;
};
// On click, get base64 string, decode to ASCII, show intial string, all
// subsequent strings, and final, linkified ASCII result.
function show() {
var b64box = document.getElementById("b64box");
if (b64box.reportValidity() === true) {
a = decode(b64box.value);
showDiv = document.getElementById("showDiv");
showDiv.innerHTML = "";
for (let i=0; i<(a.length-1); i++) {
showDiv.insertAdjacentHTML("beforeend", `<p>${a[i]}</p>`);
}
url = a.pop();
showDiv.insertAdjacentHTML("beforeend", `<p><a href="${url}">${url}</a></p>`);
}
return false;
};
// Take a string, check if it's base64, decode if so, repeat until b64 check
// fails or 9 attempts, return an array of all decodings.
function decode(str) {
const a = [];
for (let i=0; i<9; i++) {
a.push(str);
try {
window.atob(str);
} catch (e) {
return(a);
}
str = decodeURIComponent(escape(window.atob(str)));
console.log(a[i]);
}
console.log("Error: Too many (10+) base64 decodings.")
return(a);
};