-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
199 lines (185 loc) · 5.95 KB
/
index.html
File metadata and controls
199 lines (185 loc) · 5.95 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Based(64) - Efficient Encoder/Decoder Workflows</title>
<!-- Alpine Plugins -->
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/persist@3.x.x/dist/cdn.min.js"></script>
<!-- Alpine Core -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" />
<style>
#history table {
table-layout: fixed;
width: 100%;
}
#history th:first-child,
#history td:first-child {
width: 50%;
}
#history th:last-child,
#history td:last-child {
width: 50%;
}
#history {
min-height: 100vh;
}
#history td {
cursor: pointer;
word-break: break-all;
}
#encoder > article input,
#encoder > article fieldset {
margin-bottom: 0;
}
#history header {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
</head>
<body>
<header class="container-fluid">
<nav>
<ul>
<li>
<strong>Based(64)</strong>
</li>
</ul>
<ul>
<li>
<a href="https://github.com/narkeeso/based64">GitHub</a>
</li>
</ul>
</nav>
</header>
<main class="container-fluid" x-data="encoder">
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('encoder', () => ({
decoded: '',
encoded: '',
urlsafeMode: Alpine.$persist(false),
history: Alpine.$persist([]),
submit(encode = true) {
// Prevent empty inputs
if (!this.decoded && !this.encoded) return;
const history = this.history;
const urlEncode = str => {
// Convert to Base64
const base64 = btoa(str);
// Make URL-safe: replace + with -, / with _, and remove padding =
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};
const urlDecode = str => {
// Add padding back
str += '='.repeat((4 - (str.length % 4)) % 4);
// Reverse URL-safe replacements: - with +, _ with /
str = str.replace(/\-/g, '+').replace(/_/g, '/');
// Decode from Base64
return atob(str);
};
if (encode) {
history.unshift({
decoded: this.decoded,
encoded: this.urlsafeMode ? urlEncode(this.decoded) : btoa(this.decoded),
});
} else {
history.unshift({
decoded: this.urlsafeMode ? urlDecode(this.encoded) : atob(this.encoded),
encoded: this.encoded,
});
}
// TODO: Make this configurable
if (history.length > 1000) {
history.pop();
}
// Reset input fields
this.decoded = '';
this.encoded = '';
},
clear() {
this.history = [];
},
}));
});
</script>
<section id="encoder">
<article>
<div class="grid">
<form @submit.prevent="submit(true)">
<input placeholder="Encode..." x-model="decoded" />
</form>
<form @submit.prevent="submit(false)">
<input placeholder="Decode..." x-model="encoded" />
</form>
</div>
<footer>
<fieldset>
<label style="margin-bottom: 0">
<input type="checkbox" role="switch" x-model="urlsafeMode" />
URL Safe
</label>
</fieldset>
</footer>
</article>
</section>
<section id="history">
<header>
<h6>History</h6>
<a @click="clear">Clear History</a>
</header>
<table>
<thead>
<tr>
<th>Plain</th>
<th>Base64</th>
</tr>
</thead>
<tbody>
<template x-for="item in history" x-data="rows">
<tr>
<td @click="highlight">
<span x-text="item.decoded"></span>
</td>
<td @click="highlight">
<span x-text="item.encoded"></span>
</td>
</tr>
</template>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('rows', () => ({
highlight() {
// Create a new Range object
const range = document.createRange();
// Select all the contents of the given element
range.selectNodeContents(this.$el);
// Get the current window's selection object
const selection = window.getSelection();
// Remove any existing selections
selection.removeAllRanges();
// Add the new range to the selection
selection.addRange(range);
},
}));
});
</script>
</tbody>
</table>
</section>
</main>
<hr />
<footer class="container-fluid">
<h6>About</h6>
<p style="max-width: 500px">
<small>
Based64 is an efficient client-side base64 encoder/decoder that stores a history of your conversions locally,
eliminating the need to re-enter text for frequent encoding and decoding tasks.
</small>
</p>
</footer>
</body>
</html>