-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
362 lines (247 loc) · 10.1 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
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
var links = [];
var xhrList = [];
var downloading = false;
$(function () {
var downloadButton = document.getElementById('download');
var progressbar = $('#progressbar');
//$('#stopdownload-icon').hide();
$('#stopdownload-icon').css('opacity', '0');
$("#extension_select, #delay").on('change', loseFocus);
function loseFocus() {
this.blur();
};
$('#download').button();
$(document).tooltip({
position: {
my: "center bottom-10",
at: "center top",
using: function (position, feedback) {
$(this).css(position);
$("<div>")
.addClass("arrow")
.addClass(feedback.vertical)
.addClass(feedback.horizontal)
.appendTo(this);
}
}
,
show: {
effect: "fade",
delay: 750,
duration: 250,
easing: "swing"
}
});
//minimum value on min/max/delay inputs
$('#min_size').on("change", function () {
if ($("#min_size").val() < 0 || document.getElementById('min_size').value == "" || isNaN($('#min_size').val())) {
$("#min_size").val("0");
}
});
$('#max_size').on("change", function () {
if ($("#max_size").val() < 0 || document.getElementById('max_size').value == "" || isNaN($('#max_size').val())) {
$("#max_size").val("1");
}
});
$("#delay").on("change", function () {
if ($("#delay").val() < 0 || document.getElementById('delay').value == "" || isNaN($('#delay').val())) {
$("#delay").val("0.5");
}
});
//kill tooltip on form click
$("input").on("click", function () {
$('.ui-tooltip').hide();
});
//cancel button
$('#download').click(function () {
if (downloading) {
window.location.reload();
}
});
//empty input text on click so the select list will show
$('input[name=extension_select]').click(function () {
$('input[name=extension_select]').val('');
});
function startMessage() {
$('#progressbar, #status_message').finish();
$('#progressbar, #status_message').animate({
opacity: 1
}, 250);
}
function endMessage() {
$('#progressbar, #status_message').finish();
$('#progressbar, #status_message').animate({
opacity: 0
}, 2500, 'easeInOutQuart', function () {
$('#status_message').innerHTML = "";
});
}
//prepares interface for downloading
function startDownloads() {
downloading = true;
//$('#download-icon').hide();
//$('#stopdownload-icon').show();
$('#download-icon').animate({
opacity: 0
}, 25, function () {
$(this).css('z-index', '-1');
});
$('#stopdownload-icon').animate({
opacity: 0.75
}, 25, function () {
$(this).css('z-index', '1');
});
$('#download').addClass('progress-bar progress-bar-striped active');
$('body').addClass('download-background');
}
//resets interface once downloads finish
function endDownloads() {
downloading = false;
$('#download-icon').animate({
opacity: 0.75
}, 25, function () {
$(this).css('z-index', '1');
});
$('#stopdownload-icon').animate({
opacity: 0
}, 25, function () {
$(this).css('z-index', '-1');
});
$('#download').removeClass('progress-bar progress-bar-striped active');
$('body').removeClass('download-background');
}
function buildDownloadList(links, urlList, keyword, fileType) {
//build list of URLs for valid downloads
for (i = 0; i < links.length; i++) {
var link = links[i];
//if no keyword is specified or if it contains keyword
if (keyword == "" || link.toUpperCase().indexOf(keyword.toUpperCase()) >= 0) {
//if it matches the desired extension
if (link.substring(link.length - (fileType.length + 1), link.length) == "." + fileType) {
urlList.push(link);
}
//no extension specified
else if (fileType == "" || fileType == "any") {
var extension = link.substring(link.length - (4), link.length);
if (extension == ".wav" || extension == ".mp3" || extension == ".rx2" || extension == ".aac" || extension == ".ogg") {
urlList.push(link);
}
}
}
}
}
function processDownloads(urlList,includeUrl,fileType,min_size,max_size,downloaded,delay){
//error handling
if (urlList.length == 0) {
document.getElementById('status_message').innerHTML = "No links match the given parameters";
endDownloads();
endMessage();
}
//download process
else {
startDownloads();
document.getElementById('status_message').innerHTML = 1 + "/" + urlList.length;
progressbar.progressbar({
max: urlList.length,
value: 0
});
//download from each URL in the list
urlList.forEach(function (url, index) {
xhrList[index] = new XMLHttpRequest();
xhrList[index].open('GET', url, true);
xhrList[index].responseType = "blob";
xhrList[index].onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
setTimeout(function () {
var filename;
if (includeUrl) {
filename = url;
}
else {
//BUG: if extension has a slash, it will only include the text after the slash
filename = url.split("/").pop();
}
var blob;
if (fileType == "wav") {
blob = new Blob([xhrList[index].response], { type: "audio/wav" });
}
else if (fileType == "rx2") {
blob = new Blob([xhrList[index].response], { type: "audio" });
}
else if (fileType == "mp3") {
blob = new Blob([xhrList[index].response], { type: "audio/mpeg" });
}
else {
blob = new Blob([xhrList[index].response], { type: "audio" });
}
try {
//if the size is valid, or if size hasn't been set
if ((blob.size > +min_size && blob.size <= max_size) ||
(min_size == 0 && max_size == 0) ||
(blob.size > +min_size && max_size == 0) ||
(min_size == 0 && blob.size <= max_size)) {
saveAs(blob, filename);
}
}
catch (ex) {
document.getElementById('status_message').innerHTML = ex;
endDownloads();
endMessage();
return;
}
downloaded++;
document.getElementById('status_message').innerHTML = downloaded + "/" + urlList.length;
progressbar.progressbar("value", downloaded);
if (downloaded == urlList.length) {
document.getElementById('status_message').innerHTML = "All files have finished downloading";
setTimeout(function () {
endDownloads();
endMessage();
}, 1000);
}
}, (delay * index)); //wait between each download
}
};
xhrList[index].send();
});
}
}
downloadButton.addEventListener('click', function () {
if (!downloading) {
startMessage();
var includeUrl = document.getElementById("include_url").checked;
var fileSelect = document.getElementById("extension_select");
var fileType = fileSelect.value;
var delaySelect = document.getElementById("delay");
var keyword = document.getElementById("keyword").value;
var min_size = document.getElementById("min_size").value * 1000000; //convert to bytes
var max_size = document.getElementById("max_size").value * 1000000;
var urlList = [];
var downloaded = 0;
var delay;
if (delaySelect.value == "") { //if no delay time is input, the default is one second
delay = 1000;
}
else {
delay = document.getElementById("delay").value * 1000;
}
buildDownloadList(links, urlList, keyword, fileType);
processDownloads(urlList,includeUrl,fileType,min_size,max_size,downloaded,delay);
let b = baffle('#status_message').start();
b.reveal(250);
}
}, false);
});
chrome.extension.onRequest.addListener(function (links_list) {
links = links_list;
});
function onWindowLoad() {
chrome.tabs.executeScript(null, {
file: "get_links.js"
}, function () {
if (chrome.runtime.lastError) {
//window.alert('There was an error injecting script : \n' + chrome.runtime.lastError.message);
}
});
}
window.onload = onWindowLoad;