Kemono 下载工具

仅支持Files内图片下载

< Feedback on Kemono 下载工具

Review: OK - script works, but has bugs

§
Posted: 2025/03/02

替换下面两个函数解决无法保存对应拓展名的问题

function createFileName(artist, titles, index, pages, url) {
  let extension = url.split('.').pop().split('?')[0]; // 提取URL中的扩展名
  if (!['jpg', 'png', 'gif', 'jpeg', 'webp'].includes(extension)) {
    extension = 'jpg'; // 如果无法获取有效扩展名,默认使用jpg
  }

  if (nameRule === 1) {
    return `${index}.${extension}`; // 1.jpg
  } else if (nameRule === 2) {
    return `${index}_${pages}.${extension}`; // 1_20.jpg
  } else if (nameRule === 3) {
    return `${titles[0]} ${index}_${pages}.${extension}`; // title 1_20.jpg
  } else if (nameRule === 4) {
    return `${titles[0]} ${titles[1]} ${index}_${pages}.${extension}`; // title (fanbox) 1_20.jpg
  } else if (nameRule === 5) {
    return `[${artist}] ${titles[0]} ${titles[1]} [${index}_${pages}].${extension}`; // [artist] title (fanbox) [1_20].jpg
  }
}
function downloadingFiles(btn, artist, titles, fileList) {
  let totalQuest = fileList.length, success = 0, faild = 0;

  fileList.forEach((url, index) => {
    let fileName = createFileName(artist, titles, index + 1, fileList.length, url); // 传递 URL 以获取扩展名

    GM_download({
      url: url,
      name: fileName,
      onload: function() {
        console.log(`下载 ${index + 1}/${fileList.length} 完成`);
        totalQuest -= 1;
        success += 1;
        downloadingFilesFinished(btn, totalQuest, fileList.length, success, faild);
      },
      onerror: function(err) {
        console.error(`下载 ${index + 1}/${fileList.length} 失败,准备二次尝试`, err);

        GM_download({
          url: url,
          name: fileName,
          onload: function() {
            console.log(`下载 ${index + 1}/${fileList.length} 完成 (二次尝试)`);
            totalQuest -= 1;
            success += 1;
            downloadingFilesFinished(btn, totalQuest, fileList.length, success, faild);
          },
          onerror: function(err) {
            console.log(`下载 ${index + 1}/${fileList.length} 失败 (二次尝试),请尝试手动下载`, err, {
              name: fileName,
              url: url
            });
            totalQuest -= 1;
            faild += 1;
            downloadingFilesFinished(btn, totalQuest, fileList.length, success, faild);
          }
        });
      }
    });
  });
}

Post reply

Sign in to post a reply.