-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodings.html
380 lines (335 loc) · 15 KB
/
encodings.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Encoding Utilities</title>
<style>
h1 {
font-size: 12pt;
}
textarea {
display: block;
max-width: 97%;
}
fieldset {
display: inline-block;
}
#copy_status {
color: darkgreen;
font-size: 11pt;
font-weight: normal;
opacity: 0;
user-select: none;
}
@keyframes FadeOut {
/* For animation for copy status text */
from { opacity: 1; }
to { opacity: 0; }
}
</style>
<script>
function ClearAll() {
document.getElementById('input_area').value = '';
document.getElementById('output_area').value = '';
}
function ClearOutput() {
document.getElementById('output_area').value = '';
}
function RemoveNewlinesFromInput() {
var originalContent = document.getElementById('input_area').value;
document.getElementById('input_area').value = RemoveNewlines(originalContent);
}
function RemoveNewlinesFromOutput() {
var originalContent = document.getElementById('output_area').value;
document.getElementById('output_area').value = RemoveNewlines(originalContent);
}
function RemoveNewlines(inputString){
// Regex might be slightly more efficient, but this feels more readable
return inputString
.replaceAll("\r","")
.replaceAll("\n", "");
}
function CopyOutput() {
var content = document.getElementById('output_area').value;
ClipboardUtilities.CopyToClipboard(content);
}
ClipboardUtilities = function () {
function CopyToClipboard(textToCopy) {
// Support for non-navigator clipboard copying when not over https:// or file:// based on https://stackoverflow.com/a/65996386/2890086
// navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method'
return navigator
.clipboard
.writeText(textToCopy)
.then(
ShowClipboardCopySuccessMessage,
ShowClipboardCopyFailureMessage
);
} else {
// text area method
console.log("Using text area with exec");
textArea.value = textToCopy;
// make the textarea out of viewport
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise(
(res, rej) => {
// here the magic happens
document.execCommand('copy') ? res() : rej();
textArea.remove();
}).then(
ShowClipboardCopySuccessMessage,
ShowClipboardCopyFailureMessage
);
}
}
const ShowClipboardCopySuccessMessage = function () {
var element = document.getElementById('copy_status');
element.textContent = 'Output copied to clipboard';
element.style.color = 'darkgreen';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 3.14s";
}
const ShowClipboardCopyFailureMessage = function () {
var element = document.getElementById('copy_status');
element.textContent = 'Failed to copy output to clipboard';
element.style.color = 'darkred';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 3.14s";
}
/* Expose functions needed outside module*/
return {
CopyToClipboard:CopyToClipboard,
}
}();
function Base64Encode() {
var input = document.getElementById('input_area').value;
var utf8Array = Base64Utilities.strToUTF8Arr(input);
var encoded = Base64Utilities.base64EncArr(utf8Array);
document.getElementById('output_area').value = encoded;
}
function Base64Decode() {
var input = document.getElementById('input_area').value;
var utf8Array = Base64Utilities.base64DecToArr(input);
var decoded = Base64Utilities.UTF8ArrToStr(utf8Array);
document.getElementById('output_area').value = decoded;
}
function UrlEncode() {
var input = document.getElementById('input_area').value;
var encoded = encodeURIComponent(input);
document.getElementById('output_area').value = encoded;
}
function UrlDecode() {
var input = document.getElementById('input_area').value;
var decoded = decodeURIComponent(input);
document.getElementById('output_area').value = decoded;
}
Base64Utilities = function() {
"use strict";
/*\
|*|
|*| Base64 / binary data / UTF-8 strings utilities
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding
|*|
\*/
/* Array of bytes to Base64 string decoding */
function b64ToUint6 (nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
62
: nChr === 47 ?
63
:
0;
}
function base64DecToArr (sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 6 * (3 - nMod4);
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return taBytes;
}
/* Base64 string to array encoding */
function uint6ToB64 (nUint6) {
return nUint6 < 26 ?
nUint6 + 65
: nUint6 < 52 ?
nUint6 + 71
: nUint6 < 62 ?
nUint6 - 4
: nUint6 === 62 ?
43
: nUint6 === 63 ?
47
:
65;
}
function base64EncArr (aBytes) {
var nMod3 = 2, sB64Enc = "";
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) + (nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==');
}
/* UTF-8 array to DOMString and vice versa */
function UTF8ArrToStr (aBytes) {
var sView = "";
for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCodePoint(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
(nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) + aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
nPart
);
}
return sView;
}
function strToUTF8Arr (sDOMStr) {
var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0;
/* mapping... */
for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
nChr = sDOMStr.codePointAt(nMapIdx);
if (nChr > 65536) {
nMapIdx++;
}
nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
}
aBytes = new Uint8Array(nArrLen);
/* transcription... */
for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
nChr = sDOMStr.codePointAt(nChrIdx);
if (nChr < 128) {
/* one byte */
aBytes[nIdx++] = nChr;
} else if (nChr < 0x800) {
/* two bytes */
aBytes[nIdx++] = 192 + (nChr >>> 6);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x10000) {
/* three bytes */
aBytes[nIdx++] = 224 + (nChr >>> 12);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x200000) {
/* four bytes */
aBytes[nIdx++] = 240 + (nChr >>> 18);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} else if (nChr < 0x4000000) {
/* five bytes */
aBytes[nIdx++] = 248 + (nChr >>> 24);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} else /* if (nChr <= 0x7fffffff) */ {
/* six bytes */
aBytes[nIdx++] = 252 + (nChr >>> 30);
aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
}
}
return aBytes;
}
/* Expose functions needed outside module*/
return{
strToUTF8Arr:strToUTF8Arr,
UTF8ArrToStr:UTF8ArrToStr,
base64EncArr:base64EncArr,
base64DecToArr:base64DecToArr,
// uint6ToB64:uint6ToB64,
// b64ToUint6:b64ToUint6
}
}();
</script>
</head>
<body>
<h1><label for="input_area">Input</label></h1>
<textarea id="input_area" name="input_area" rows="20" cols="80"></textarea>
<br>
<fieldset>
<legend>Actions</legend>
<fieldset>
<legend>Base64</legend>
<button onclick="Base64Encode()">Encode</button>
<button onclick="Base64Decode()">Decode</button>
</fieldset>
<fieldset>
<legend>URL encoding</legend>
<button onclick="UrlEncode()">Encode</button>
<button onclick="UrlDecode()">Decode</button>
</fieldset>
<fieldset>
<legend>Remove newlines</legend>
<button onclick="RemoveNewlinesFromInput()">Input</button>
<button onclick="RemoveNewlinesFromOutput()">Output</button>
</fieldset>
<fieldset>
<legend>Clear</legend>
<button onclick="ClearAll()">All</button>
<button onclick="ClearOutput()">Output</button>
</fieldset>
<button onclick="CopyOutput()" title="Copy output to clipboard.">
<span style="position: relative; font-size: 1.3rem;">
<!-- Clipboard icon, 📋 -->
📋
<span style="position: absolute; top: .4rem; left: .6rem; font-size: 1rem;">
<!-- Leftwards arrow with hook, ↩ -->
↩
</span>
</span>
</button>
</fieldset>
<h1>
<label for="output_area">Output</label>
<span id="copy_status">Output copied to clipboard</span>
</h1>
<textarea disabled id="output_area" name="output_area" rows="20" cols="80"></textarea>
</body>
</html>