ImageDownloaderLib

Image downloader for manga download scripts.

当前为 2022-09-22 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/451810/1096733/ImageDownloaderLib.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

function ImageDownloader({
  getImagePromises,
  isOKToDownload = () => true,
  title = `package_${Date.now()}`,
  zipOptions = {},
  imageSuffix = 'jpg',
  cssVerticalDistance = 'top: 80px',
  cssHorizontalDistance = 'left: 80px'
}) {
  // create download button element
  const dlBtn = document.createElement('button');
  dlBtn.textContent = 'Download';
  dlBtn.style = `
    position: fixed;
    ${cssVerticalDistance};
    ${cssHorizontalDistance};
    z-index: 9999999;

    width: 128px;
    height: 48px;

    display: flex;
    justify-content: center;
    align-items: center;

    font-size: 14px;
    font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
    color: #fff;

    background-color: #0984e3;
    border: none;
    border-radius: 4px;
    cursor: pointer;
  `;

  // setup click event callback
  dlBtn.onclick = function () {
    if (!isOKToDownload()) return;

    dlBtn.disabled = true;
    dlBtn.textContent = "Processing";
    dlBtn.style.backgroundColor = '#aaa';
    dlBtn.style.cursor = 'not-allowed';
    download(getImagePromises, title, zipOptions);
  }

  // add button to body
  document.body.appendChild(dlBtn);

  // download
  async function download(getImagePromises, title, zipOptions) {
    const images = await Promise.all(getImagePromises());

    // create zip
    const zip = new JSZip(); // JSZip library should be imported already
    const folder = zip.folder(title);
    images.forEach((image, index) => {
      const filename = `${index + 1}`.padStart(images.length >= 100 ? 3 : 2, '0') + `.${imageSuffix}`;
      folder.file(filename, image, zipOptions);
    });

    // pop up download window
    const content = await zip.generateAsync({ type: "blob" });
    saveAs(content, `${title}.zip`); // FileSaver library should be imported already

    // change the text of download button
    dlBtn.innerText = "Completed";
  }
}