Tampermonkey 例子:
async function batchDownload(imgsUrl, imgsTitles, folderName) {
const data = imgsUrl;
const zip = new JSZip();
const promises = [];
for (let index = 0; index < data.length; index++) {
const item = data[index];
const promise = await getData(item, "blob").then(data => {
const file_name = imgsTitles[index];
zip.file(file_name, data, {
binary: true
})
console.log(`第 ${index+1} 张,文件名:${file_name},大小:${parseInt(data.size / 1024)} Kb,下载完成!等待压缩...`);
}).catch((err) => {
console.log(err);
return -1;
})
promises.push(promise);
}
Promise.all(promises).then((err) => {
for (let i = 0; i < err.length; i++) {
if (err[i] == -1) {
console.log( i + "文件缺失!")
break;
}
}
zip.generateAsync({
type: "blob"
}).then(content => {
saveAs(content, `${folderName} [${data.length}P]`);
})
})
};
function getData(url, type = "document", usermethod = "GET") {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: usermethod,
url: url,
responseType: type,
onload: function (response) {
if (response.status == 200) {
resolve(response.response);
} else {
reject(response);
}
},
onerror: function (error) {
console.log("网络错误");
reject(error);
}
});
});
}
项目完整代码