feat: 新增异环角色图片下载脚本

This commit is contained in:
奇趣保罗 2026-05-30 01:50:44 +08:00
parent 0e42d20199
commit 0922035a4c
1 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,83 @@
//
// 批量下载异环角色图
//
// URL: https://neverness-to-everness.fandom.com/wiki/Character
// 清洗链接
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(".hlist.mw-collapsible.navbox-inner .card-image-container img")).map((item) => [
cleanImageUrl(item.src),
generateImageName(item.src)
]);
// https://static.wikia.nocookie.net/neverness-to-everness/images/2/2d/Aurelia_Icon.png/revision/latest/scale-to-width-down/74?cb=20260505052443
// 自动下载图片的函数
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("-icon", "")}.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);