cmoa.jp downloader

Download comic from cmoa.jp

// ==UserScript==
// @name        cmoa.jp downloader
// @namespace   https://blog.bgme.me
// @match       https://www.cmoa.jp/bib/speedreader/*
// @grant       none
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js
// @run-at      document-end
// @version     1.0
// @author      bgme
// @description Download comic from cmoa.jp
// @supportURL  https://github.com/yingziwu/Greasemonkey/issues
// @icon        https://www.cmoa.jp/favicon.ico
// @license     AGPL-3.0-or-later
// ==/UserScript==

"use strict";

/*
// 在 Console 中使用时请去除此段注释
['https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js'].forEach(item => {
    let script = document.createElement('script');
    script.src = item;
    document.body.append(script);
});
*/

function addButton() {
  let button = document.createElement("button");
  button.className = "icon_pc";
  button.style.cssText = `position: fixed;
                          top: 15%;
                          right: 5%;
                          z-index: 99;
                          background-color: #0000;
                          border-style: none;
                          text-align:center;
                          vertical-align:baseline;`;

  let img = document.createElement("img");
  img.src =
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAFSQAABUkBt3pUAAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAbTSURBVHic7Z1ZqFZVFMd/V69zaY4lIagNoqXVbU4boEkbtCSDSMKSxEJfywahxyIrfMmMoIEyQhBMshIq8yGnBoqKZkyTMknKofR6r7eH3YVPu373nL33d/aw1g/2g9xvn7XO3n/3sM4emvBLD2AmMAu4GDgZ6OvZhi86gF3Ab8DPwHpgHfB1QJ+SpgX4AlOwKadtwCJgiNfSyZwbgQOErzyf6QCwFBjosZyyZCKwj/AV1qi0HZjqrbQyZAPhK6mKtBQzxlFqmEz4iqkyrSGzLsFV0TO8eJEONwEbgdNCO+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uAjwFrMFu+2gnn+8mB3DeawxiI0AWOBm4E+HmxHzwaqO71zVkXv5IPLgMNUUy5Om1dT6QJ2ACtDO1GCjzAnjEZPKgLoj7mgOSWqvHnEmlQEMBRzeHMqTAMmhXaiCqocA+wnjeNaBmMOl66qXESMAQAGAK8BvUI70g3PAaNCO1GUlAQAcAGRXZt2DHOA20M7USVVdgGd6TAe7sppAGMwcYeqy0NMF9BJM6YrCHpVyjH0AF4kkZF/LSkKAEy0bUloJ2pYBFwZ2okQfEj1TV5tiuE2j/MwJ5GFKgNxXUAtLwCnBLTfF3iF8JdLWZO6AIZj+t5QB1YuAc4OZNsLqQsAYCphooTXAvcHsOuVHAQA8DQwrkJ7wzC3fsd+VG635CKA/lQbJXwWGFmRrYaSiwAAzgcercDO3aS1NqGhhJ4GdhUlvLSB7xsq2hftNLDDMb9vmoFXaUyUMNloXz1y6gI6GYsZFPrmIYRG++qxnvBN4PHSbR7fs4Ww0b5ou4CYWYafKGHy0b565CyAYcBLuM/VnwLOcvYmUnIWAMD1wHyH/NcB93nyJUpyFwCYeL1NlNBXCxI1uU0Du6I/sILyUcJson31kNACgBnFLy7x+7lotK8QHxB+GlQ0tQNXFHinMcDeCPzVaaBnemD69HqRvM7fxLTesKFIEgCY/93P1Pn7IxRrJZT/SKkLqE1d9e8tmKtmQ/uWVBfQ4Zg/FMuAiTX/HoXZfRz7riPvuB4QkSpDga2YW8UPYTZziun3a5EqADAneMwI7URopA0ClWNQAQhHBSAcFYBwpE4Dc6LokXldoi1A+uxyyewqAB8HMSpufOmS2VUAPznmV9x50yWzqwDWOuZX3FgPbA7pQBMmpBr6g4jEdIBIziIcT3zbpXJPB4GZRSqnKs4FfiB8wUhI3wFTilVL9/hc8dobmA3cijk1Y5Cn5/Yks/14JWnDTPU+A1ZhtsG3+nr4v9GhBc6CW0iCAAAAAElFTkSuQmCC";
  img.style.cssText = "height: 2em;";

  button.onclick = function () {
    console.log("Start download……");
    window.zip = new JSZip();
    window.fileNameList = new Array();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MUSetZCGgPMESRYj1cAGARMIqv1kMlgK8pNQq39TARBB8VhCgyW59S414y6frjxDYeUYTCNnnKHw4WeUxl1/wtGjwk97LToyBan6irmRrPfSHj/K+ZuSJ3TImCav3zaqvlvTN57T9W6+ozJAqa9fH9/gLS3kja/wr69+PUKMhGXUxVie0mVVMXZAUSwONys4ztvHn5kQcgttubJ+tEkde7G2MEiExUyD3VVExE4AAPS40qTlaNSw8QgI+dnlxVx8ncnp0PCsD5WJnaI23Z2Lk3wP1igx83jA2L998V4G8E5WrVy0kRfIeXMLkMm1TIN8GWYXIQTVbKVa+cjLCO4r2+fFnq+X17MZ7N6GmlxRJXt1pWnjh3Q1yX09Vi8tXTl/zb8eeB5GCgkFe9cOAgTutPBcD1stbGEYXVtvYCaA4BwHU9W8smXzt7DesMBMB1NFQMVS8eOgK3hQM4zut6ezZdPbt9/ZWhKQsArpu5OrJ83dwNW0dGAFzHwyVe6iXDx8zt2AEc522jI8etntOx8cbRjAcA1+18/Vf5+vlbvk6cALhORkr/qZeOnHC34QdwnPfNTvzjLtC5+S7QRAQg8eNuYcEW6yAIIF4GQD8A9W5IZX3eFQW6tqI61KNbXf9vy4K/T/2Wd90X+hqFnfHG1K8oUq1nuqpVZD3zjal865nvjBVY74pC/qpg/nYkNQqb6+uZrChYrFcQhcBnOwlR2KIfLoUGlIj1EgaUsPVCUcixnjcKmdZzRCHdeqYo1FgPjUKl9YAolFtPjMIf672i8NP6DFH4br2pH1d/AAm28mJJn9pPAAAAAElFTkSuQmCC";
    button.onclick = function () {
      alert(`Downloaded ${window.fileNameList.length / 3} files.`);
      if (confirm("Save to disk?")) {
        saveZip();
      }
    };
  };

  button.appendChild(img);
  document.body.appendChild(button);
  console.log("Start Load……");
}

function setEvent() {
  let MutationObserver =
    window.MutationObserver ||
    window.WebKitMutationObserver ||
    window.MozMutationObserver;
  const element = document.getElementById("content");
  let observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (mutation.type == "attributes") {
        catchPages();
      }
    });
  });

  observer.observe(element, {
    attributes: true, //configure it to listen to attribute changes
    attributeFilter: ["style"],
  });
}

function catchPages() {
  const pt_imgs = document.querySelectorAll(".pt-img");
  pt_imgs.forEach((pt_img) => {
    catchPage(pt_img);
  });
}

async function catchPage(pt_img) {
  let pageNum = pt_img.parentElement.getAttribute("id").match(/\d+$/);
  let imgs = pt_img.querySelectorAll("img");
  let imgNum = 1;

  for (let img of imgs) {
    let blobUri = img.getAttribute("src");
    let blobName = `p${pageNum}i${imgNum}.jpg`;
    if (window.fileNameList.includes(blobName)) {
      continue;
    }
    window.fileNameList.push(blobName);
    await fetch(blobUri)
      .then((response) => response.blob())
      .then((blob) => window.zip.file(blobName, blob))
      .catch((err) => console.log("catchPage: " + err));
    console.log("Downloading " + blobName);
    imgNum++;
  }
}

function saveZip() {
  console.log("Save to disk……");
  //  console.log(zip);
  let title = document.querySelector("title").text;
  window.zip
    .generateAsync({ type: "blob" })
    .then((blob) => {
      saveAs(blob, `${title}.zip`);
    })
    .catch((err) => console.log("saveZip: " + err));
}

// Run script
window.addEventListener("load", function () {
  addButton();
});