-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBatchUploader.js
173 lines (145 loc) · 6.89 KB
/
BatchUploader.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
let BatchUpLLoder = new function()
{
let abort = true;
let uploadRequests = [];
this.uploadFiles = function(e)
{
e.preventDefault();
getFiles(e.target);
e.target.blur();
};
const processUpload = function (target, files) {
if (!files.length) {
return;
}
abort = false;
let abortBttn = document.createElement("button");
abortBttn.id = "foxlinksAbortBttn";
abortBttn.textContent = "Abort Upload";
abortBttn.addEventListener("click", abortUpload, false);
let form = target.parentNode;
form.appendChild(document.createTextNode(" "));
form.appendChild(abortBttn);
let imageBox = document.getElementById("foxlinksUploadImageBox");
if (!imageBox) {
imageBox = document.createElement("textarea");
imageBox.readOnly = true;
imageBox.id = "foxlinksUploadImageBox";
imageBox.cols = 60;
imageBox.rows = 6;
imageBox.style.setProperty("position", "fixed", "");
imageBox.style.setProperty("opacity", "0.8", "");
imageBox.style.setProperty("bottom", "20px", "");
imageBox.style.setProperty("right", "5px", "");
imageBox.style.setProperty("z-index", "21", "");
imageBox.addEventListener("click", function (e) {
e.target.select();
}, false);
document.body.appendChild(imageBox);
let closeBox = document.createElement("a");
closeBox.href = "javascript:void(0)";
closeBox.style.setProperty("display", "block", "");
closeBox.style.setProperty("text-align", "center", "");
closeBox.style.setProperty("position", "fixed", "");
clos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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAADQANAAAILwABBBhIsGAAgQINEkx4UGHDggwHRlxI0WHDiQoxGkRo8aFHiBtDZqzokGPHgwEBADs=)`, "");
closeBox.style.setProperty("border", "black 1px solid", "");
closeBox.style.setProperty("bottom", (imageBox.offsetHeight + 20 - 13 - 1).toString() + "px", "");
closeBox.style.setProperty("right", "5px", "");
closeBox.style.setProperty("z-index", "22", "");
closeBox.addEventListener("click", (e) => {
imageBox.parentNode.removeChild(imageBox);
e.target.parentNode.removeChild(e.target);
abortUpload();
}, false);
document.body.appendChild(closeBox);
}
document.getElementById("foxlinksUploadBttn").disabled = true;
const filesArray = Array.from(files);
imageBox.value = filesArray.map(() => "UPLOADING...\n").join("");
let uploadPromises = filesArray.map((f, i) => new Promise((resolve, reject) => {
doUpload(resolve, reject, f, i);
}));
Promise.all(uploadPromises).then(abortUpload);
};
function getFiles(target)
{
const filepicker = document.createElement("input");
filepicker.type = "file";
filepicker.multiple = true;
filepicker.accept = "image/*";
filepicker.onchange = () => { processUpload(target, filepicker.files); };
filepicker.click();
}
function abortUpload()
{
abort = true;
const offset = 0;
let abortBttn = document.getElementById("foxlinksAbortBttn");
if (abortBttn)
{
abortBttn.parentNode.removeChild(abortBttn);
}
document.getElementById("foxlinksUploadBttn").disabled = false;
while (uploadRequests.length > 0)
{
if (uploadRequests[0].readyState !== XMLHttpRequest.DONE)
{
uploadRequests[0].abort();
}
uploadRequests.shift();
}
}
function doUpload(resolve, reject, file, index)
{
let form = new FormData();
form.append("file", file);
let request = new XMLHttpRequest();
request.onreadystatechange = () => {
if (request.readyState !== XMLHttpRequest.DONE)
{
if (abort)
{
request.abort();
reject();
}
return;
}
let img = request.responseText.match(/<input value="<img src="(.*?)" \/>"/);
let imageBox = document.getElementById("foxlinksUploadImageBox");
if (imageBox)
{
let values = imageBox.value.split("\n");
if (img && request.responseText.indexOf("Errors were encountered processing one or more of your uploads") === -1)
{
values[index] = `<img src="${img[1]}" />`;
}
else
{
values[index] = `[FILE UPLOAD "${file.name}" FAILED!]`;
}
imageBox.value = values.join("\n");
}
let uploadRequestsIndex = uploadRequests.indexOf(request);
if (uploadRequestsIndex >= 0)
{
uploadRequests.splice(uploadRequestsIndex, 1);
}
resolve();
};
request.open("POST", "http://u.endoftheinter.net/u.php", true);
request.send(form);
uploadRequests.push(request);
}
};
let imageBttn = document.querySelector('input[value="Upload Image"]');
if (imageBttn)
{
let batchBttn = document.createElement("button");
batchBttn.id = "foxlinksUploadBttn";
batchBttn.textContent = "Batch Uploader";
batchBttn.addEventListener("click", BatchUpLLoder.uploadFiles, false);
imageBttn.parentNode.insertBefore(batchBttn, imageBttn.nextSibling);
imageBttn.parentNode.insertBefore(document.createTextNode(" "), imageBttn.nextSibling);
}