-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpage.js
145 lines (137 loc) · 4.09 KB
/
page.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
/**
* Listener that receives a message with a list of image
* URL's to display from popup.
*/
chrome.runtime.onMessage
.addListener((message,sender,sendResponse) => {
addImagesToContainer(message)
sendResponse("OK");
});
/**
* Function that used to display an UI to display a list
* of images
* @param {} urls - Array of image URLs
*/
function addImagesToContainer(urls) {
if (!urls || !urls.length) {
return;
}
const container = document.querySelector(".container");
urls.forEach(url => addImageNode(container, url))
}
/**
* Function dynamically add a DIV with image and checkbox to
* select it to the container DIV
* @param {*} container - DOM node of a container div
* @param {*} url - URL of image
*/
function addImageNode(container, url) {
const div = document.createElement("div");
div.className = "imageDiv";
const img = document.createElement("img");
img.src = url;
div.appendChild(img);
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.setAttribute("url",url);
div.appendChild(checkbox);
container.appendChild(div)
}
/**
* The "Select All" checkbox "onChange" event listener
* Used to check/uncheck all image checkboxes
*/
document.getElementById("selectAll")
.addEventListener("change", (event) => {
const items = document.querySelectorAll(".container input");
for (let item of items) {
item.checked = event.target.checked;
};
});
/**
* The "Download" button "onClick" event listener
* Used to compress all selected images to a ZIP-archive
* and download this ZIP-archive
*/
document.getElementById("downloadBtn")
.addEventListener("click", async() => {
try {
const urls = getSelectedUrls();
const archive = await createArchive(urls);
downloadArchive(archive);
} catch (err) {
alert(err.message)
}
})
/**
* Function used to get URLs of all selected image
* checkboxes
* @returns Array of URL string
*/
function getSelectedUrls() {
const urls =
Array.from(document.querySelectorAll(".container input"))
.filter(item=>item.checked)
.map(item=>item.getAttribute("url"));
if (!urls || !urls.length) {
throw new Error("Please, select at least one image");
}
return urls;
}
/**
* Function used to download all image files, identified
* by `urls`, and compress them to a ZIP
* @param {} urls - list of URLs of files to download
* @returns a BLOB of generated ZIP-archive
*/
async function createArchive(urls) {
const zip = new JSZip();
for (let index in urls) {
try {
const url = urls[index];
const response = await fetch(url);
const blob = await response.blob();
zip.file(checkAndGetFileName(index, blob),blob);
} catch (err) {
console.error(err);
}
};
return zip.generateAsync({
type:'blob',
compression: "DEFLATE",
compressionOptions: {
level: 9
}
});
}
/**
* Function used to return a file name for
* image blob only if it has a correct image type
* and positive size. Otherwise throws an exception.
* @param {} index - An index of URL in an input
* @param {*} blob - BLOB with a file content
* @returns
*/
function checkAndGetFileName(index, blob) {
let name = parseInt(index)+1;
const [type, extension] = blob.type.split("/");
if (type != "image" || blob.size <= 0) {
throw Error("Incorrect content");
}
return name+"."+extension.split("+").shift();
}
/**
* Triggers browser "Download file" action
* using a content of a file, provided by
* "archive" parameter
* @param {} archive - BLOB of file to download
*/
function downloadArchive(archive) {
const link = window.document.createElement('a');
link.href = window.URL.createObjectURL(archive);
link.download = "images.zip";
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(link.href);
document.body.removeChild(link);
}