-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.js
More file actions
66 lines (61 loc) · 2.27 KB
/
test.js
File metadata and controls
66 lines (61 loc) · 2.27 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
downloadBlob = function (data, fileName, mimeType) {
var blob, url;
blob = new Blob([data], {
type: mimeType
});
url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function () {
return window.URL.revokeObjectURL(url);
}, 1000);
};
downloadURL = function (data, fileName) {
var a;
a = document.createElement('a');
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = 'display: none';
a.click();
a.remove();
};
function fn() {
document.querySelector('input').addEventListener('change', function () {
var files = [];
var self = this;
var readFileIntoFiles = function () {
if (this.result) {
var arrayBuffer = this.result;
array = new Uint8Array(arrayBuffer);
console.log(files.push(array));
reader.onload = readFileIntoFiles;
if (files.length < self.files.length)
reader.readAsArrayBuffer(self.files[files.length]);
else {
var worker = new Worker('worker.js');
worker.addEventListener('message', function (e) {
console.log('Worker said: ', e);
if (e.data.type == "log") {
let div = document.createElement("div");
div.textContent = e.data.message;
document.querySelector("body").appendChild(div);
} else if (e.data.type == "result") {
alert(`TOOK: ${e.data.time}`)
downloadBlob(e.data.result, "smaller.pdf", "application/pdf");
}
}, false);
let action = files.length > 1 ? "merge" : "compress";
let r =
worker.postMessage({
array: files,
action
});
}
}
}
var reader = new FileReader();
reader.onload = readFileIntoFiles;
reader.readAsArrayBuffer(this.files[0]);
}, false);
}
document.addEventListener('DOMContentLoaded', fn, false);