forked from zamicol/BaseConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
264 lines (188 loc) · 7.79 KB
/
app.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
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
"use strict";
document.addEventListener('DOMContentLoaded', () => {
document.querySelector("#ConvertBaseButton").addEventListener('click', Convert);
document.querySelector("#FlipButton").addEventListener('click', Flip);
document.querySelector("#reverseInButton").addEventListener('click', ReverseIn);
document.querySelector("#reverseOutButton").addEventListener('click', ReverseOut);
document.querySelector("#ShareButton").addEventListener('click', ShareURL);
// TODO this seems wrong
document.querySelectorAll("#ClearButton").forEach(t => {
t.addEventListener('click', Clear);
});
document.querySelectorAll(".copy").forEach(t => {
t.addEventListener('click', Copy);
});
document.querySelectorAll(".source").forEach(t => {
t.addEventListener('click', Source);
});
document.querySelectorAll(".destination").forEach(t => {
t.addEventListener('click', Desintation);
});
//////////////////
// URL parameters
//////////////////
PopulateFromURL();
//////////////////
// Live Update Conversion
//////////////////
// I don't know if change does anything, but input doesn't seem to cover all
// cases of user interaction.
document.querySelector("#inputString").addEventListener('input', Convert);
document.querySelector("#inputAlphabet").addEventListener('input', Convert);
document.querySelector("#outputAlphabet").addEventListener('input', Convert);
//////////////////
// Examples
//////////////////
//document.querySelector("#ExampleButton").addEventListener('click', ExampleConversion);
document.querySelector("#Example64To256Button").addEventListener('click', Example64To256);
document.querySelector("#Example256to32Button").addEventListener('click', Example256to32);
//////////////////
// Live Update Length
//////////////////
document.querySelectorAll(".conversionTextArea").forEach(t => {
t.addEventListener('input', Length);
});
// I don't know if change does anything, but input doesn't seem to cover all
// cases of user interaction.
document.querySelectorAll(".conversionTextArea").forEach(t => {
t.addEventListener('change', Length);
});
console.log(reduce(6, 8));
});
function FullBuckets() {
var inputBase = document.getElementById("inputAlphabet").value.length;
var outputBase = document.getElementById("outputAlphabet").value.length;
console.log(inputBase);
console.log(outputBase);
inBits = bitPerBase(inputBase);
outBits = bitPerBase(outputBase);
console.log(reduce(inBits, outBits));
}
function Convert() {
let inputAlphabet = document.getElementById("inputAlphabet").value;
let inputString = document.getElementById("inputString").value;
let outputAlphabet = document.getElementById("outputAlphabet").value;
if (inputAlphabet == "" || inputString == "" || outputAlphabet == "") {
console.log("Empty input.");
return null;
}
let outputString = baseConvert(inputString, inputAlphabet, outputAlphabet);
console.log(outputString);
document.getElementById("outputString").value = outputString;
Update();
}
function Flip() {
let inputAlphabet = document.getElementById("inputAlphabet").value;
let inputString = document.getElementById("inputString").value;
let outputAlphabet = document.getElementById("outputAlphabet").value;
let outputString = document.getElementById("outputString").value;
document.getElementById("inputAlphabet").value = outputAlphabet;
document.getElementById("inputString").value = outputString;
document.getElementById("outputAlphabet").value = inputAlphabet;
document.getElementById("outputString").value = inputString;
Update();
}
function ReverseIn() {
var string = document.getElementById("inputString").value;
string = string.split("").reverse().join("");
document.getElementById("inputString").value = string;
}
function ReverseOut() {
var string = document.getElementById("outputString").value;
string = string.split("").reverse().join("");
document.getElementById("outputString").value = string;
}
function Copy() {
//console.log("Triggered Copy");
// Alternative selector:
//console.log(this.parentNode.previousElementSibling.firstElementChild.value);
let input = this.parentNode.previousElementSibling.firstElementChild;
input.select();
input.setSelectionRange(0, 99999);
document.execCommand("copy");
}
function Source() {
console.log("Entered Source");
let input = this.parentNode.parentNode.firstElementChild.firstElementChild.value;
document.getElementById("inputAlphabet").value = input;
Update();
}
function Desintation() {
console.log("Entered Desintation");
let input = this.parentNode.parentNode.firstElementChild.firstElementChild.value;
document.getElementById("outputAlphabet").value = input;
Update();
}
function Clear() {
//console.log("Triggered Clear");
document.getElementById("inputAlphabet").value = "";
document.getElementById("inputString").value = "";
document.getElementById("outputAlphabet").value = "";
document.getElementById("outputString").value = "";
Update();
}
function Length() {
//console.log("Triggered Length");
//console.log(this);
this.parentNode.querySelector(".lengthDisplay").textContent = this.value.length;
}
function LengthAll() {
//console.log("Triggered LengthAll");
document.getElementById("inputAlphabetLength").textContent = document.getElementById("inputAlphabet").value.length;
document.getElementById("inputStringLength").textContent = document.getElementById("inputString").value.length;
document.getElementById("outputAlphabetLength").textContent = document.getElementById("outputAlphabet").value.length;
document.getElementById("outputStringLength").textContent = document.getElementById("outputString").value.length;
}
// Update updates all information presented on the screen.
function Update() {
LengthAll();
}
// Populate fields from URL parameters.
function PopulateFromURL() {
console.log("populating from URL");
var url = new URL(window.location.href);
console.log(url);
var input = url.searchParams.get("in");
var inAlpha = url.searchParams.get("inAlpha");
var outAlpha = url.searchParams.get("outAlpha");
console.log(input, inAlpha, outAlpha);
if (input != "" && inAlpha != "" && outAlpha != "") {
document.getElementById("inputString").value = input;
document.getElementById("inputAlphabet").value = inAlpha;
document.getElementById("outputAlphabet").value = outAlpha;
Convert();
}
}
// Share
function ShareURL() {
var input = document.getElementById("inputString").value;
var inAlpha = document.getElementById("inputAlphabet").value;
var outAlpha = document.getElementById("outputAlphabet").value;
var url = new URL(window.location.href);
url.searchParams.set("in", input);
url.searchParams.set("inAlpha", inAlpha);
url.searchParams.set("outAlpha", outAlpha);
console.log(url);
document.getElementById("ShareTextArea").value = url.href;
}
function ExampleConversion() {
document.getElementById("inputString").value = "KNBrzb9SPxyILkIHg5XE-z7Lm8x8Y5UjS4kXY3StK14";
document.getElementById("inputAlphabet").value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
document.getElementById("outputAlphabet").value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
// Out should be 12BWR7JBVQZDDNW5M1Z4CF8VYEPZZQNVC-G7KRV-ZOCUX-7P7F
Convert();
}
function Example64To256() {
document.getElementById("inputString").value = "eyJhbGciOiJFUzUxMiJ9";
document.getElementById("inputAlphabet").value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
document.getElementById("outputAlphabet").value = document.getElementById("base256").value;
// Out should be {"alg":"ES512"}
Convert();
}
function Example256to32() {
document.getElementById("inputString").value = "foobar¡¡¡¡";
document.getElementById("inputAlphabet").value = document.getElementById("base256").value;
document.getElementById("outputAlphabet").value = document.getElementById("base32").value;
// Out should be MZXW6YTBOI======
Convert();
}