GM_archiveImages

打包下载图片

Ezt a szkriptet nem ajánlott közvetlenül telepíteni. Ez egy könyvtár más szkriptek számára, amik tartalmazzák a // @require https://update.greasyfork.org/scripts/565289/1749527/GM_archiveImages.js hivatkozást.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

(function () {
  /**
   * 打包下载图片
   * @param images Array<{ url: string; name: string }> - 要下载的图片列表
   * @param zip string - 要保存的压缩包名
   * @param concurrency number - 并发数
   * @param maxRetries number - 失败重试最大次数
   * @param onload ({ loaded, total }) => void - 图片下载进度
   * @param onretry ({ url, currentRetry, maxRetries }) => void - 图片下载失败重试
   * @param onerror (image, err) => void - 图片(重试后依旧)下载失败
   * @param onloaded (images) => void - 所有图片下载完成
   * @param onarchive (percent) => void - 打包进度
   * @param oncomplete () => void - 完成
   */
  async function GM_archiveImages({
    images,
    zip,
    concurrency = 10,
    maxRetries = 5,
    onload,
    onretry,
    onerror,
    onloaded,
    onarchive,
    oncomplete,
  }) {
    let loaded = 0;
    const limit = GM_pLimit(concurrency);
    const input = images.map((image) =>
      limit(() =>
        // 获取图片数据
        GM_request({
          url: image.url,
          method: "get",
          responseType: "blob",
          maxRetries,
          onretry,
        })
          .then((res) => {
            image.blob = res.response;
          })
          .catch((err) => {
            onerror?.(image, err);
          })
          .finally(() => {
            loaded++;
            onload?.({ loaded, total: images.length });
          }),
      ),
    );

    await Promise.all(input); // 并发获取图片数据
    onloaded?.(images);

    const jsZip = new JSZip(); // 创建 ZIP 实例
    const loadedImages = images.filter((image) => image.blob);
    loadedImages.forEach((image) => {
      jsZip.file(image.name, image.blob); // 添加图片到 ZIP 文件夹
    });

    // 生成 ZIP 文件并触发下载
    const content = await jsZip.generateAsync({ type: "blob" }, (metadata) => {
      onarchive?.(metadata.percent);
    });
    saveAs(content, zip); // 使用 FileSaver.js 下载 ZIP 文件
    oncomplete?.({ images, jsZip });
  }

  window.GM_archiveImages = GM_archiveImages;
})();