This script is designed to take a list of image URL's and download them all, as fast as it can. The issue is that it misses some, and I'm unsure why. Is this related to Chrome's limitation of the number of parallel downloads? I've tried to account for that with the use of the onload function of GM_download.
It downloads about half of the files from the list. The success rate goes up if I increase the sleep() timer from 200 to something like 2000, but I don't know why given I'm relying on the onload function to correctly state when the download is finished (and thus when Chrome can accept a new download)
// ==UserScript==
// @name Image Save Test
// @namespace ist
// @include *testimages.juliaimages.org/
// @version 1.0.0
// @run-at document-end
// @noframes true
// @grant GM_download
// ==/UserScript==
var currentDownloads = 0;
var queue = [ 'https://testimages.juliaimages.org/thumbnails/autumn_leaves.png',
'https://testimages.juliaimages.org/thumbnails/blobs.png',
'https://testimages.juliaimages.org/thumbnails/cameraman.png',
'https://testimages.juliaimages.org/thumbnails/fabio_color_512.png',
'https://testimages.juliaimages.org/thumbnails/earth_apollo17.png',
'https://testimages.juliaimages.org/thumbnails/fabio_gray_256.png',
'https://testimages.juliaimages.org/thumbnails/hela-cells.png',
'https://testimages.juliaimages.org/thumbnails/lake_gray.png',
'https://testimages.juliaimages.org/thumbnails/house.png',
'https://testimages.juliaimages.org/thumbnails/jetplane.png',
'https://testimages.juliaimages.org/thumbnails/lake_color.png',
'https://testimages.juliaimages.org/thumbnails/lena_gray_16bit.png',
'https://testimages.juliaimages.org/thumbnails/lighthouse.png',
'https://testimages.juliaimages.org/thumbnails/mandril_color.png',
'https://testimages.juliaimages.org/thumbnails/mandril_gray.png',
'https://testimages.juliaimages.org/thumbnails/mountainstream.png',
'https://testimages.juliaimages.org/thumbnails/peppers_color.png',
'https://testimages.juliaimages.org/thumbnails/moonsurface.png',
'https://testimages.juliaimages.org/thumbnails/peppers_gray.png',
'https://testimages.juliaimages.org/thumbnails/toucan.png',
'https://testimages.juliaimages.org/thumbnails/pirate.png'];
(async function() {
while (queue.length > 0) {
if (currentDownloads > 5) {
await sleep(200);
continue;
}
var item = queue.shift();
(function(_item) {
GM_download({
url: _item,
name: _item.match(/([0-9A-Za-z _-]+)(?=\.png)/)[0],
saveAs: false,
onerror: function(error) {
queue.unshift(_item);
currentDownloads--;
},
onload: function() {
currentDownloads--;
}
});
currentDownloads++;
})(item);
}
})();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}