GM_archiveImages

打包下载图片

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/565289/1749527/GM_archiveImages.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

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