-
Notifications
You must be signed in to change notification settings - Fork 45
/
t2m.loader.js
418 lines (331 loc) · 10.2 KB
/
t2m.loader.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
(function () {
"use strict";
// Function for performing actions as soon as possible
var on_ready = (function () {
// Vars
var callbacks = [],
check_interval = null,
check_interval_time = 250;
// Check if ready and run callbacks
var callback_check = function () {
if (
(document.readyState === "interactive" || document.readyState === "complete") &&
callbacks !== null
) {
// Run callbacks
var cbs = callbacks,
cb_count = cbs.length,
i;
// Clear
callbacks = null;
for (i = 0; i < cb_count; ++i) {
cbs[i].call(null);
}
// Clear events and checking interval
window.removeEventListener("load", callback_check, false);
window.removeEventListener("readystatechange", callback_check, false);
if (check_interval !== null) {
clearInterval(check_interval);
check_interval = null;
}
// Okay
return true;
}
// Not executed
return false;
};
// Listen
window.addEventListener("load", callback_check, false);
window.addEventListener("readystatechange", callback_check, false);
// Callback adding function
return function (cb) {
if (callbacks === null) {
// Ready to execute
cb.call(null);
}
else {
// Delay
callbacks.push(cb);
// Set a check interval
if (check_interval === null && callback_check() !== true) {
check_interval = setInterval(callback_check, check_interval_time);
}
}
};
})();
// Functions
var script_add = (function () {
var script_on_load = function (state) {
// Okay
script_remove_event_listeners.call(this, state, true);
};
var script_on_error = function (state) {
// Error
script_remove_event_listeners.call(this, state, false);
};
var script_on_readystatechange = function (state) {
if (this.readyState === "loaded" || this.readyState === "complete") {
// Okay
script_remove_event_listeners.call(this, state, true);
}
};
var script_remove_event_listeners = function (state, okay) {
// Remove event listeners
this.addEventListener("load", state.on_load, false);
this.addEventListener("error", state.on_error, false);
this.addEventListener("readystatechange", state.on_readystatechange, false);
state.on_load = null;
state.on_error = null;
state.on_readystatechange = null;
// Trigger
if (state.callback) state.callback.call(null, okay, this);
// Remove
var par = this.parentNode;
if (par) par.removeChild(this);
};
return function (url, callback) {
var head = document.head,
script, state;
if (!head) {
// Callback and done
callback.call(null, false, null);
return false;
}
// Load state
state = {
on_load: null,
on_error: null,
on_readystatechange: null,
callback: callback,
};
// New script tag
script = document.createElement("script");
script.async = true;
script.setAttribute("src", url);
// Events
script.addEventListener("load", (state.on_load = script_on_load.bind(script, state)), false);
script.addEventListener("error", (state.on_error = script_on_error.bind(script, state)), false);
script.addEventListener("readystatechange", (state.on_readystatechange = script_on_readystatechange.bind(script, state)), false);
// Add
head.appendChild(script);
// Done
return true;
};
})();
var on_generic_stop_propagation = function (event) {
event.stopPropagation();
};
var on_exclusive_mode_change = function (flag_node) {
exclusive_mode_update.call(this, flag_node, false);
};
var exclusive_mode_update = (function () {
var previous_fragment = "";
return function (flag_node, check_fragment) {
var hash_is_exclusive = (window.location.hash == "#converter.exclusive");
if (check_fragment) {
this.checked = hash_is_exclusive;
}
else {
if (this.checked ^ (!hash_is_exclusive)) {
previous_fragment = window.location.hash;
}
window.history.replaceState({}, "", window.location.pathname + (this.checked ? "#converter.exclusive" : previous_fragment));
}
if (this.checked) {
flag_node.classList.add("exclusive_enabled");
}
else {
flag_node.classList.remove("exclusive_enabled");
}
};
})();
var on_converter_click = function (converter_files_input, event) {
if (event.which != 2 && event.which != 3) {
converter_files_input.click();
}
};
var on_converter_files_change = function (converter) {
// Read
on_converter_test_files.call(converter, this.files);
// Nullify
this.value = null;
};
var on_file_dragover = function (converter, event) {
if (Array.prototype.indexOf.call(event.dataTransfer.types, "Files") < 0) return;
converter.classList.add("converter_files_active");
if (this === converter) converter.classList.add("converter_files_hover");
event.dataTransfer.dropEffect = "copy";
event.preventDefault();
event.stopPropagation();
return false;
};
var on_file_dragleave = function (converter, event) {
if (Array.prototype.indexOf.call(event.dataTransfer.types, "Files") < 0) return;
converter.classList.remove("converter_files_hover");
if (this !== converter) converter.classList.remove("converter_files_active");
event.preventDefault();
event.stopPropagation();
return false;
};
var on_file_drop = function (converter, event) {
// Reset style
converter.classList.remove("converter_files_active");
converter.classList.remove("converter_files_hover");
event.preventDefault();
event.stopPropagation();
// Not over the converter
if (this !== converter) return false;
// Read files
on_converter_test_files.call(converter, event.dataTransfer.files);
// Done
return false;
};
var on_converter_test_files = function (files) {
// Read
var re_ext = /(\.[^\.]*|)$/,
read_files = [],
ext, i;
for (i = 0; i < files.length; ++i) {
ext = re_ext.exec(files[i].name)[1].toLowerCase();
if (ext == ".torrent") {
read_files.push(files[i]);
}
}
// Nothing to do
if (read_files.length === 0) return;
// Load scripts if necessary
load_requirements(function (errors) {
if (errors === 0) {
// Load
var T2M_obj;
try {
T2M_obj = T2M;
}
catch(e) {
return; // not found
}
T2M_obj.queue_torrent_files(read_files);
}
});
};
var load_requirements = (function () {
// Script requirements
var requirements = [
"src/sha1.js",
"src/bencode.js",
"src/base32.js",
"src/t2m.js",
];
var on_all_scripts_loaded = function () {
var T2M_obj;
try {
T2M_obj = T2M;
}
catch(e) {
return; // not found
}
T2M_obj.setup(rice_checkboxes);
};
var on_script_load = function (state, callback, okay) {
if (okay) ++state.okay;
if (++state.count >= state.total) {
// All loaded/errored
if (state.total - state.okay === 0) on_all_scripts_loaded();
callback.call(null, state.total - state.okay);
}
};
// Return the loading function
return function (callback) {
// Already loaded?
if (requirements === null) {
// Yes
callback.call(null, 0);
return;
}
var head = document.head,
on_load, i;
if (!head) return false;
// Load
on_load = on_script_load.bind(null, { okay: 0, count: 0, total: requirements.length, }, callback);
for (i = 0; i < requirements.length; ++i) {
script_add(requirements[i], on_load);
}
// Done
requirements = null;
return true;
};
})();
var restyle_noscript = function () {
// Script
var nodes = document.querySelectorAll(".script_disabled"),
i;
for (i = 0; i < nodes.length; ++i) {
nodes[i].classList.remove("script_visible");
}
nodes = document.querySelectorAll(".script_enabled");
for (i = 0; i < nodes.length; ++i) {
nodes[i].classList.add("script_visible");
}
};
var rice_checkboxes = function (nodes) {
var svgns = "http://www.w3.org/2000/svg",
i, par, sib, node, n1, n2, n3;
nodes = nodes || document.querySelectorAll("input[type=checkbox].checkbox");
for (i = 0; i < nodes.length; ++i) {
node = nodes[i];
par = node.parentNode;
sib = node.nextSibling;
// Create new checkbox
n1 = document.createElement("label");
n1.className = node.className;
n2 = document.createElementNS(svgns, "svg");
n2.setAttribute("svgns", svgns);
n2.setAttribute("viewBox", "0 0 16 16");
n3 = document.createElementNS(svgns, "polygon");
n3.setAttribute("points", "13,0 16,2 8,16 5,16 0,11 2,8 6,11.5");
// Re-add
n2.appendChild(n3);
n1.appendChild(n2);
par.insertBefore(n1, node);
n1.insertBefore(node, n2);
}
};
// Execute
on_ready(function () {
// Noscript
var nodes, i;
// Rice
restyle_noscript();
rice_checkboxes();
// Stop propagation links
nodes = document.querySelectorAll(".link_stop_propagation");
for (i = 0; i < nodes.length; ++i) {
nodes[i].addEventListener("click", on_generic_stop_propagation, false);
}
// Setup converter
var converter = document.querySelector(".converter"),
converter_files = document.querySelector(".converter_files_input"),
exclusive_mode = document.querySelector("input.converter_exclusive_mode_check"),
non_exclusive_body = document.querySelector(".non_exclusive"),
body = document.body;
if (converter !== null) {
// File browser
if (converter_files !== null) {
converter.addEventListener("click", on_converter_click.bind(converter, converter_files), false);
converter_files.addEventListener("change", on_converter_files_change.bind(converter_files, converter), false);
}
// File drag/drop events
converter.addEventListener("dragover", on_file_dragover.bind(converter, converter), false);
converter.addEventListener("dragleave", on_file_dragleave.bind(converter, converter), false);
converter.addEventListener("drop", on_file_drop.bind(converter, converter), false);
body.addEventListener("dragover", on_file_dragover.bind(body, converter), false);
body.addEventListener("dragleave", on_file_dragleave.bind(body, converter), false);
body.addEventListener("drop", on_file_drop.bind(body, converter), false);
// Exclusive
if (exclusive_mode !== null) {
exclusive_mode_update.call(exclusive_mode, non_exclusive_body, true);
exclusive_mode.addEventListener("change", on_exclusive_mode_change.bind(exclusive_mode, non_exclusive_body), false);
}
}
});
})();