-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.html
More file actions
156 lines (135 loc) · 5.43 KB
/
base64.html
File metadata and controls
156 lines (135 loc) · 5.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Base64 Encoder/Decoder</title>
<link rel="stylesheet" href="common.css">
<style>
body {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<a href="index.html" class="back-link">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
All Tools
</a>
<header>
<h1>Base64 Encoder/Decoder</h1>
<p class="subtitle">Real-time bidirectional conversion</p>
</header>
<div class="controls">
<button id="clearBtn" class="secondary">Clear All</button>
</div>
<div class="container">
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
</svg>
Text
</span>
<button id="copyText" class="copy-btn">Copy</button>
</div>
<textarea id="textArea" placeholder="Type or paste text here..." spellcheck="false"></textarea>
</div>
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
</svg>
Base64
<span id="statusIndicator" class="status-indicator"></span>
</span>
<button id="copyBase64" class="copy-btn">Copy</button>
</div>
<textarea id="base64Area" placeholder="Type or paste Base64 here..." spellcheck="false"></textarea>
<div id="errorMessage" class="error-message"></div>
</div>
</div>
<script>
const textArea = document.getElementById('textArea');
const base64Area = document.getElementById('base64Area');
const clearBtn = document.getElementById('clearBtn');
const copyText = document.getElementById('copyText');
const copyBase64 = document.getElementById('copyBase64');
const errorMessage = document.getElementById('errorMessage');
const statusIndicator = document.getElementById('statusIndicator');
let updating = false;
function encodeToBase64() {
if (updating) return;
updating = true;
errorMessage.style.display = 'none';
statusIndicator.className = 'status-indicator';
const text = textArea.value;
if (!text) {
base64Area.value = '';
updating = false;
return;
}
try {
base64Area.value = btoa(unescape(encodeURIComponent(text)));
statusIndicator.className = 'status-indicator valid';
} catch (e) {
statusIndicator.className = 'status-indicator invalid';
}
updating = false;
}
function decodeFromBase64() {
if (updating) return;
updating = true;
errorMessage.style.display = 'none';
statusIndicator.className = 'status-indicator';
const base64 = base64Area.value;
if (!base64) {
textArea.value = '';
updating = false;
return;
}
try {
textArea.value = decodeURIComponent(escape(atob(base64)));
statusIndicator.className = 'status-indicator valid';
} catch (e) {
errorMessage.style.display = 'block';
errorMessage.textContent = 'Invalid Base64 string';
statusIndicator.className = 'status-indicator invalid';
}
updating = false;
}
textArea.addEventListener('input', encodeToBase64);
base64Area.addEventListener('input', decodeFromBase64);
clearBtn.addEventListener('click', () => {
textArea.value = '';
base64Area.value = '';
errorMessage.style.display = 'none';
statusIndicator.className = 'status-indicator';
textArea.focus();
});
function setupCopyButton(btn, textarea) {
btn.addEventListener('click', () => {
if (!textarea.value) return;
navigator.clipboard.writeText(textarea.value).then(() => {
const originalText = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(() => {
btn.textContent = originalText;
btn.classList.remove('copied');
}, 2000);
});
});
}
setupCopyButton(copyText, textArea);
setupCopyButton(copyBase64, base64Area);
</script>
</body>
</html>