forked from dandeto/Base-64-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
320 lines (289 loc) · 8.96 KB
/
popup.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
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
var string_input = document.getElementById("string-input");
var out = document.getElementById("output"), //all global because its UI
// copybtn = document.getElementById("copy"),
to64 = document.getElementById("to"),
from64 = document.getElementById("from"),
// tobtn = document.getElementById("convert"),
// input = document.getElementById("file"),
canvas = document.getElementById("cnv"),
// download = document.getElementById("download"),
// clear = document.getElementById("clear"),
// selector = document.getElementById("fileTypeSelector"),
// label = document.getElementById("label"),
// fileName = document.getElementById("fileName"),
// file_type = document.getElementById("file_type"),
state, string, img, aud, fr, file, type, b64Type, created, fileType;
var setting = 0;
var ctx = canvas.getContext("2d");
to.style.borderColor = "red";
to.addEventListener('click', () => {
// Convert result if pressed while active.
if (setting === 0) {
convertText(out.value);
return;
}
setting = 0;
// tobtn.style.display = "none";
// copybtn.style.display = "inline";
// download.style.display = "none";
// clear.style.display = "none";
// selector.style.display = "none";
// label.style.display = "inline-block";
// fileName.style.display = "inline-block";
// file_type.style.display = "none";
to.style.borderColor = "red";
from64.style.borderColor = "";
});
from64.addEventListener('click', () => {
// Convert result if pressed while active.
if (setting === 1) {
convertText(out.value);
return;
}
setting = 1;
// tobtn.style.display = "inline";
// copybtn.style.display = "none";
// download.style.display = "inline-block";
// clear.style.display = "inline";
// selector.style.display = "inline";
// label.style.display = "none";
// fileName.style.display = "none";
// file_type.style.display = "inline-block";
from64.style.borderColor = "red";
to.style.borderColor = "";
});
// clear.addEventListener('click', () => {
// out.value = null;
// });
// selector.addEventListener('change', () => {
// b64Type = this.value;
// });
// copybtn.addEventListener('click', () => {
// out.select();
// document.execCommand('copy');
// });
// download.addEventListener('click', function() {
// downloadCanvas(this);
// }, false);
// input.addEventListener("change", convert);
// tobtn.addEventListener("click", find);
// document.getElementById("clear-storage").addEventListener('click', clearStorage);
string_input.addEventListener('paste', e => {
// Delay to let paste finish.
setTimeout(() => convertText(e), 100);
});
string_input.addEventListener('keypress', e => {
// Submit on Enter.
if (e.key === 'Enter') {
convertText(e);
}
});
// Clear memory if result is deleted manually.
out.addEventListener('input', e => {
if (e.target.value === '') {
clearStorage();
}
});
function convertText(e) {
// Use event object or string value.
let v;
try {
v = e.target.value;
} catch (err) {
v = e;
}
if (!setting) {
out.value = window.btoa(v);
} else {
out.value = window.atob(v);
}
store(out.value);
out.select();
document.execCommand('copy');
}
function convert() {
clearDisplay();
document.getElementById("fileName").textContent = input.files[0].name;
setting = 0;
file = input.files[0];
fr = new FileReader();
if (file.type.indexOf("audio") == 0 || file.type.indexOf("video") == 0) { //if audio or video
fr.onload = createAudio;
fileType = "audio";
} else if (file.type.indexOf("image") == 0) { // if image
fr.onload = createImage;
fileType = "image";
} else if (file.type.indexOf("text") == 0) { // if txt
fr.onload = createText; // onload fires after reading is complete
fileType = "text";
} else {
alert("File not supported.");
fr.onload = createDefault;
}
fr.readAsDataURL(file);
}
function find() {
clearDisplay();
if (out.value.indexOf("text/") > -1) {//all text
createText();
fileType = "text";
} else if (out.value.indexOf("image/") > -1) { //all image types
createImage();
fileType = "image";
} else if (out.value.indexOf("audio/") > -1 ||
out.value.indexOf("video/ogg") > -1) { //all audio
createAudio();
fileType = "audio";
}
}
function clearDisplay() {
if (created == "audio") {
var el = document.getElementsByTagName("audio")[0];
document.getElementById("cnvContainer").removeChild(el);
} else if (created == "text") {
var el = document.getElementsByTagName("textarea")[0];
document.getElementById("cnvContainer").removeChild(el);
} else {
ctx.clearRect(0,0,canvas.width,canvas.height); //clean up canvas
canvas.width = 0;
canvas.height = 0;
}
created = "";
}
function createImage() {
img = new Image();
img.onload = imageLoaded; //create canvas and put img on it
if (setting == 0) {
img.src = fr.result;
out.value = fr.result;
store(fr.result);
}
if (setting == 1) {
if (state == -1) {
out.value = string + out.value;
img.src = out.value;
} else {
img.src = out.value;
}
store(out.value);
}
}
function imageLoaded() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0);
}
function createAudio() {
aud = document.createElement("audio");
if (setting == 0) {
aud.setAttribute("src",fr.result); //make uploaded file the audio
out.value = aud.src;
store(fr.result);
}
if (setting == 1) {
var contains = out.value.indexOf("data:audio/mp3;base64,"); //Chrome says mp3 instead of mpeg like FF but FF accepts both
if (state == -1 && contains == 0) {
aud.setAttribute("src",out.value);
} else if (state == -1 && contains == -1) {
out.value = string + out.value;
aud.setAttribute("src",out.value);
} else {
aud.setAttribute("src",out.value);
}
store(out.value);
}
aud.setAttribute("controls", "controls");
document.getElementById("cnvContainer").appendChild(aud);
created = "audio";
}
function createText() {
var txt = document.createElement("textarea");
txt.setAttribute("rows", "5");
function detect() {
var contains = out.value.indexOf("data:text");
if (contains == -1) {
txt.value = window.atob(out.value);
} else if (contains == 0) {
var str = out.value.split(",").pop();
txt.value = window.atob(str);
}
store(out.value);
}
if (setting == 0) {
out.value = fr.result;
store(fr.result);
}
detect();
document.getElementById("cnvContainer").appendChild(txt);
created = "text";
}
function createDefault() { //not a supported type. Do the conversion, but leave off header.
out.value = window.btoa(fr.result);
}
function downloadCanvas(link) {
var ext;
file_type = document.getElementById("file_type"); //update the current value
if (file_type.value.indexOf(".") == 0) {
ext = file_type.value;
} else {
ext = "." + file_type.value;
}
if (file_type.value == undefined || file_type.value == "") {
if (b64Type == "image/png") { ext = ".png"}
if (b64Type == "image/jpeg") { ext = ".jpg"}
if (b64Type == "image/bmp") { ext = ".bmp"}
if (b64Type == "image/svg+xml") { ext = ".svg"}
if (b64Type == "image/gif") { ext = ".gif"}
if (b64Type == "image/tiff") { ext = ".tiff"}
if (b64Type == "image/x-icon") { ext = ".ico"}
if (b64Type == "audio/mpeg") { ext = ".mp3"}
if (b64Type == "audio/wav") { ext = ".wav"}
if (b64Type == "video/ogg") { ext = ".ogg"}
if (b64Type == "text/plain") { ext = ".txt"}
if (b64Type == "text/css") { ext = ".css"}
if (b64Type == "text/html") { ext = ".html"}
if (b64Type == "text/javascript") { ext = ".js"}
}
if (ext == undefined || ext == "") { //needs to select download format
alert("Select a file type.");
} else if (!(out.value == undefined || out.value == "")) {
link.download = "converted_file" + ext;
link.href = out.value;
}
}
function store(data) {
chrome.storage.local.set({"key": data});
}
function clearStorage() {
chrome.storage.local.clear();
}
(function () {
// Default to start with converting from b64.
from64.click();
string_input.focus();
chrome.storage.local.get(['key'], function (result) {
if (result.key !== undefined) {
out.value = result.key;
// function load() {
// out.value = result.key;
// setting = 1;
// find();
// setting = 0;
// }
}
});
// function confirmBox(callback) {
// var confirmDialogue = document.querySelector(".confirm");
// confirmDialogue.style.display = "block";
// confirmDialogue.children[1].addEventListener('click', () => {
// confirmDialogue.style.display = "none";
// callback();
// });
// confirmDialogue.children[2].addEventListener('click', () => { confirmDialogue.style.display = "none" });
// }
// //ask to load what's in ls
// if (result.key.length < 500000) load(); //smallish file, don't worry about it.
// else confirmBox(load); //will take awhile to load
// }
// });
}());