-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwget.js
More file actions
128 lines (118 loc) · 4.37 KB
/
wget.js
File metadata and controls
128 lines (118 loc) · 4.37 KB
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
(() => {
const contentType = "application/x-www-form-urlencoded;charset=UTF-8"
if (!has(perms, "write"))
return
ebi("opa_cfg").insertAdjacentHTML("afterend", '<a id="opa_wget" href="#v=wget" data-dest="wget">📤</a>')
QS("head").insertAdjacentHTML("beforeend", '<style>' +
'#op_wget h2 {margin: 0}' +
'#op_wget label {display: block}' +
'#wget_err {display: block; background: var(--bg-d1); border-radius: .3rem; padding: .2rem; height: 8rem; overflow-y: scroll}</style>'
)
ebi("op_cfg").insertAdjacentHTML("afterend", '<div id="op_wget" class="opview opbox opwide"></div>')
let wrapper = ebi("op_wget")
ebi("opa_wget").onclick = opclick
function setContent(msg=null, showLinks=false) {
wrapper.innerHTML = "<h2>wget downloader</h2>";
if (msg)
wrapper.innerHTML += "<p>" + msg + "</p>";
else {
wrapper.innerHTML += ('<p>Download a file straight to this folder using wget.</p>' +
'<form id="wget_form">' +
'<label>Download URL: <input type="text" name="url" placeholder="(required)" autocorrect="off" autocomplete="off" autocapitalize="off" size="32"></label>' +
'<label>Filename: <input type="text" name="filename" placeholder="(blank for default)" autocorrect="off" autocomplete="off" autocapitalize="off" size="32"></label>' +
'<input type="submit" value="Download">' +
'</form>');
ebi("wget_form").onsubmit = (e) => {
ev(e);
let formdata = new FormData(e.target);
downloadUrl = formdata.get("url");
downloadName = formdata.get("filename");
if (!downloadUrl.trim())
return;
beginDownload();
};
}
if (showLinks) {
wrapper.innerHTML += '<p><a href="#" id="wget_retry">Retry</a> / <a href="#" id="wget_new">Download a different file</a></p>'
ebi("wget_retry").onclick = (e) => {
ev(e);
beginDownload();
}
ebi("wget_new").onclick = (e) => {
ev(e);
setContent();
}
}
}
let downloadId = null,
downloadUrl = null,
downloadName = null;
function updateStatus() {
let intId = setInterval(() => {
let req = new XHR();
req.onload = req.onerror = () => {
try {
let data = JSON.parse(req.responseText.trim());
switch (data.status) {
case "running":
setContent('Downloading file... (' + esc("" + data.progress) + '%)');
break;
case "success":
setContent('The file has been successfully downloaded! :)');
location.reload();
break;
case "error":
if (data.log.length == 0) {
setContent("The file couldn't be downloaded :(<br>Please check the server logs for details or contact the server administrator");
break;
}
setContent(
"The file couldn't be downloaded :(<br>Please check the server logs or the crash log below for details.<br>" +
'<p>Wget log:<br><code id="wget_err">' + esc(data.log.join("\n")).replace("\n", "<br>") + '</code></p>',
true
);
break;
default:
if (data.error)
setContent("Something went wrong :(", true);
break;
}
if (data.status != "running")
clearInterval(intId);
} catch (e) {
console.error(req.responseText.trim(), e);
setContent('The server sent an invalid response.' + req.responseText, true);
clearInterval(intId);
}
}
req.open("POST", get_evpath());
req.setRequestHeader("Content-Type", contentType);
let payload = JSON.stringify({"action": "wget.status", "id": downloadId});
req.send('msg=' + encodeURIComponent(payload));
}, 500);
}
function beginDownload() {
let req = new XHR();
req.onload = () => {
downloadId = parseInt(req.responseText.trim());
if (isNaN(downloadId)) {
console.error("Got invalid download ID from server", req.responseText);
setContent('The server sent an invalid response :(<br>', true);
return;
}
if (downloadId == -1) {
setContent("<code>wget</code> is not installed on the server. Please make sure it is installed or contact the server administrator.");
return;
}
updateStatus();
};
req.open("POST", get_evpath());
req.setRequestHeader("Content-Type", contentType);
let payload = JSON.stringify({"action": "wget.download", "url": downloadUrl, "filename": downloadName});
req.send('msg=' + encodeURIComponent(payload));
setContent("Initializing download...");
}
setContent();
if (sread("opmode") == "wget")
goto("wget");
})()