-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (109 loc) · 4.1 KB
/
index.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
var aria2Global = {};
var changes = {};
var config = {};
var [setting, ...options] = document.querySelectorAll('#setting, #setting [name]');
var [adduri, ...download]= document.querySelectorAll('#adduri, #adduri [name]');
var [entry, uploader] = adduri.querySelectorAll('#entry, #uploader');
document.addEventListener('click', (event) => {
switch (event.target.id) {
case 'proxy_btn':
event.target.previousElementSibling.value = localStorage.aria2Proxy || '';
break;
case 'enter_btn':
downloadSubmit();
break;
case 'commit_btn':
managerOptionsSave();
break;
}
});
downBtn.addEventListener('click', async (event) => {
manager.toggle('adduri');
manager.remove('setting')
});
optionsBtn.addEventListener('click', (event) => {
manager.toggle('setting');
manager.remove('adduri');
});
document.getElementById('commit').addEventListener('click', (event) => {
options.forEach((entry) => { localStorage[entry.name] = config[entry.name] ?? entry.dataset.value; });
if ('interval' in changes) {
clearInterval(aria2Alive);
aria2Interval = config.interval * 1000;
aria2Alive = setInterval(aria2ClientUpdate, aria2Interval);
}
if ('proxy' in changes) {
aria2Proxy = config.proxy;
}
if ('scheme' in changes) {
aria2RPC.method = config.scheme;
}
if ('secret' in changes) {
aria2RPC.secret = config.scheme;
}
if ('jsonrpc' in changes) {
aria2RPC.url = config.jsonrpc;
}
changes = {};
});
async function downloadSubmit() {
var sessions = [];
var urls = entry.value.match(/(https?:\/\/|ftp:\/\/|magnet:\?)[^\s\n]+/g);
if (urls) {
urls.forEach((url) => sessions.push({url, options: aria2Global}));
aria2RPC.call(...sessions);
}
entry.value = '';
manager.remove('adduri');
}
uploader.addEventListener('change', async (event) => {
var sessions = [];
await Promise.all([...event.target.files].map(async (file) => {
var type = file.name.slice(file.name.lastIndexOf('.') + 1);
var b64encode = await getFileData(file);
var download = type === 'torrent' ? {method: 'aria2.addTorrent', params: [b64encode, [], aria2Global]} : {method: 'aria2.addMetalink', params: [b64encode, aria2Global]};
sessions.push(download);
}));
await aria2RPC.call(...sessions);
event.target.value = '';
manager.remove('adduri');
});
function getFileData(file) {
return new Promise((resolve) => {
var reader = new FileReader();
reader.onload = (event) => {
var base64 = reader.result.slice(reader.result.indexOf(',') + 1);
resolve(base64);
};
reader.readAsDataURL(file);
});
}
NodeList.prototype.disposition = function (json) {
return ParseOptions(this, json);
}
setting.addEventListener('change', (event) => {
config[event.target.name] = changes[event.target.name] = event.target.value;
});
options.forEach((entry) => {
config[entry.name] = entry.value = localStorage[entry.name] ?? entry.dataset.value;
});
function ParseOptions(nodes, json) {
var result = {};
nodes.forEach((node) => {
var value = json[node.name];
if (value) {
node.value = result[node.name] = value;
}
});
return result;
}
(async function () {
aria2ClientSetup({jsonrpc: config.scheme + '://' + config.jsonrpc, secret: config.secret, interval: config.interval, proxy: config.proxy});
var [global, version] = await aria2RPC.call({method: 'aria2.getGlobalOption'}, {method: 'aria2.getVersion'});
var options = global.result;
options['min-split-size'] = getFileSize(options['min-split-size']);
options['max-download-limit'] = getFileSize(options['max-download-limit']);
options['max-upload-limit'] = getFileSize(options['max-upload-limit']);
aria2Global = ParseOptions(download, options);
document.querySelector('#aria2_ver').textContent = version.result.version;
})();