84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
//
|
|
// 批量下载鸣潮角色图
|
|
//
|
|
|
|
// URL: https://wutheringwaves.fandom.com/wiki/Wuthering_Waves_Wiki
|
|
|
|
// 清洗链接
|
|
function cleanImageUrl(url) {
|
|
// 使用正则表达式匹配并提取需要的部分
|
|
const cleanedUrl = url.replace(/(\/scale-to-width-down\/\d+\?.*)$/, '');
|
|
return cleanedUrl;
|
|
}
|
|
|
|
// 生成图片名称
|
|
function generateImageName(url) {
|
|
// 提取文件名部分
|
|
const fileNameMatch = url.match(/\/([^\/]+)\.png/);
|
|
if (fileNameMatch && fileNameMatch[1]) {
|
|
// 转换为小写并替换下划线为短横线
|
|
return fileNameMatch[1].toLowerCase().replace(/_/g, '-');
|
|
}
|
|
return null;
|
|
}
|
|
|
|
const images = Array.from(document.querySelectorAll(".article-table .card-image-container img")).map((item) => [
|
|
cleanImageUrl(item.src),
|
|
generateImageName(item.src)
|
|
]);
|
|
|
|
// https://static.wikia.nocookie.net/wutheringwaves/images/d/df/Resonator_Brant.png/revision/latest/scale-to-width-down/74?cb=20250213184622
|
|
|
|
// 自动下载图片的函数
|
|
function downloadImages(imageArray) {
|
|
let index = 0;
|
|
|
|
const downloadImage = async () => {
|
|
if (index >= imageArray.length) {
|
|
console.log("所有图片下载完成!");
|
|
return;
|
|
}
|
|
|
|
const [url, filename] = imageArray[index];
|
|
console.log(`正在下载第 ${index + 1} 张图片: ${filename}.png`);
|
|
|
|
try {
|
|
// 使用 fetch 下载图片
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
"accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
|
|
"accept-encoding": "gzip, deflate, br, zstd",
|
|
}
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`无法下载图片: ${url}`);
|
|
}
|
|
|
|
// 将图片数据转换为 Blob
|
|
const blob = await response.blob();
|
|
|
|
// 创建一个隐藏的 <a> 元素用于下载
|
|
const a = document.createElement("a");
|
|
const objectUrl = URL.createObjectURL(blob); // 创建 Blob 的临时 URL
|
|
a.href = objectUrl;
|
|
a.download = `${filename.replace("resonator-", "")}.webp`; // 指定下载的文件名
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
|
|
// 释放 Blob 的临时 URL
|
|
URL.revokeObjectURL(objectUrl);
|
|
} catch (error) {
|
|
console.error(`下载图片失败: ${error.message}`);
|
|
}
|
|
|
|
index++;
|
|
setTimeout(downloadImage, 2000); // 每隔 2 秒下载下一张图片
|
|
};
|
|
|
|
downloadImage();
|
|
}
|
|
|
|
// 调用函数开始下载
|
|
downloadImages(images);
|