-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download Large Images as ZIP.user.js
100 lines (89 loc) · 3.54 KB
/
Download Large Images as ZIP.user.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
// ==UserScript==
// @name Download Large Images as ZIP
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Save large images to a ZIP and download on button click
// @author You
// @match *://*/*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_download
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js
// ==/UserScript==
(function() {
'use strict';
let imageLinks = new Set();
// Monitor for all images on the page and store their URLs
document.querySelectorAll('img').forEach(img => {
const src = img.src;
if (src && src.startsWith('http')) {
imageLinks.add(src);
}
});
console.log(`[Image ZIP Downloader] Found ${imageLinks.size} images.`);
// Create a button to trigger the download
const downloadButton = document.createElement('button');
downloadButton.innerText = 'Download Large Images as ZIP';
downloadButton.style.position = 'fixed';
downloadButton.style.bottom = '50px';
downloadButton.style.right = '10px';
downloadButton.style.zIndex = '9999';
GM_addStyle(`
button {
padding: 5px 15px;
font-size: 14px;
cursor: pointer;
background-color: #007BFF;
color: #FFF;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
`);
downloadButton.onclick = async () => {
const zip = new JSZip();
for (let link of imageLinks) {
await new Promise(resolve => {
let img = new Image();
img.onload = function() {
if (img.width > 300) {
GM_xmlhttpRequest({
method: "GET",
url: link,
responseType: "arraybuffer",
onload: (response) => {
if (response.status === 200) {
zip.file(link.split('/').pop(), response.response);
console.log(`[Image ZIP Downloader] Added ${link} to ZIP.`);
} else {
console.error(`[Image ZIP Downloader] Failed to fetch ${link}. Status: ${response.status}`);
}
resolve();
},
onerror: (error) => {
console.error(`[Image ZIP Downloader] Error fetching ${link}.`, error);
resolve();
}
});
} else {
console.log(`[Image ZIP Downloader] Skipped ${link} as it's less than 500px wide.`);
resolve();
}
};
img.onerror = function() {
console.error(`[Image ZIP Downloader] Error loading ${link} for size check.`);
resolve();
};
img.src = link;
});
}
const content = await zip.generateAsync({ type: "blob" });
const blobURL = URL.createObjectURL(content);
console.log(`[Image ZIP Downloader] Triggering ZIP download.`);
GM_download(blobURL, "large_images.zip");
};
document.body.appendChild(downloadButton);
})();