-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
79 lines (73 loc) · 2.05 KB
/
index.html
File metadata and controls
79 lines (73 loc) · 2.05 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base64 Encoder/Decoder</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
max-width: 800px;
}
textarea {
width: 100%;
padding: 10px;
margin-bottom: 20px;
font-size: 1em;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Base64 Encoder/Decoder</h1>
<p>Paste text in the top textarea. If it is Base64 encoded, it will decode it; if not, it will encode it.</p>
<!-- Input Textarea -->
<textarea id="input" rows="10" placeholder="Paste text here..."></textarea>
<!-- Output Textarea -->
<textarea id="output" rows="10" placeholder="Result..." readonly></textarea>
<script>
/**
* Checks if the given string is valid Base64.
* It does this by removing whitespace, checking the length,
* and trying to decode and re-encode the string.
*/
function isValidBase64(str) {
const sanitized = str.replace(/\s+/g, '');
if (sanitized.length % 4 !== 0) {
return false;
}
try {
const decoded = atob(sanitized);
return btoa(decoded) === sanitized;
} catch (e) {
return false;
}
}
// Listen for input changes in the top textarea.
document.getElementById('input').addEventListener('input', function() {
const inputText = this.value.trim();
const outputArea = document.getElementById('output');
// Clear output if input is empty.
if (inputText === '') {
outputArea.value = '';
return;
}
// If valid Base64, decode it.
if (isValidBase64(inputText)) {
try {
outputArea.value = atob(inputText);
} catch (e) {
outputArea.value = 'Error decoding Base64: ' + e;
}
} else {
// Otherwise, encode the text.
try {
outputArea.value = btoa(inputText);
} catch (e) {
outputArea.value = 'Error encoding to Base64: ' + e;
}
}
});
</script>
</body>
</html>