GM_archiveImages

打包下载图片

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/565289/1749527/GM_archiveImages.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

(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;
})();