PiccomaDownloader

Manga downloader for piccoma.com

As of 2022-09-23. See the latest version.

// ==UserScript==
// @name         PiccomaDownloader
// @namespace    https://piccoma.com/
// @version      0.1
// @description  Manga downloader for piccoma.com
// @homepage     https://github.com/Timesient/manga-download-scripts
// @author       Timesient
// @license      GPL-3.0
// @match        https://piccoma.com/web/viewer/*/*
// @require      https://unpkg.com/[email protected]/dist/axios.min.js
// @require      https://unpkg.com/[email protected]/dist/jszip.min.js
// @require      https://unpkg.com/[email protected]/dist/FileSaver.min.js
// @require      https://greasyfork.org/scripts/451810-imagedownloaderlib/code/ImageDownloaderLib.js?version=1096733
// @grant        GM_xmlhttpRequest
// @grant        unsafeWindow
// ==/UserScript==

(async function(axios, JSZip, saveAs, ImageDownloader) {
  'use strict';

  // wait for the data and method generate by other scripts
  const [configData, getSeed] = await new Promise(resolve => {
    const timer = setInterval(() => {
      if (unsafeWindow._pdata_ && unsafeWindow.get_seed && unsafeWindow.unscrambleImg) {
        clearInterval(timer);
        resolve([unsafeWindow._pdata_, unsafeWindow.get_seed]);
      }
    }, 500);
  });

  // generate title and data of images
  const title = configData.title;
  const imageData = configData.img.filter(img => img.path !== '').map(config => {
    const url = new URL('https:' + config.path);
    const sum = url.pathname.split('/')[4];
    const expire = url.search.match(/\d{10}/)[0];
    config.url = 'https:' + config.path;
    config.seed = getSeed(sum, expire);
    delete config.path;

    return config;
  });

  // setup ImageDownloader
  ImageDownloader({
    getImagePromises,
    title,
    zipOptions: { base64: true }
  });

  // collect promises of image
  function getImagePromises() {
    return imageData.map(data => getDecodedImageBase64Promise(data));
  }
  
  // get promise of decoded image
  function getDecodedImageBase64Promise(data) {
    return new Promise(async resolve => {
      const imageArraybuffer = await new Promise(resolve => {
        GM_xmlhttpRequest ( {
          method: 'GET',
          url: data.url,
          responseType: 'arraybuffer',
          onload: res => resolve(res.response)
        });
      });

      const imageBase64 = 'data:image/jpg;base64,' + window.btoa(new Uint8Array(imageArraybuffer).reduce((data, byte) => data + String.fromCharCode(byte), ''));
      const image = document.createElement('img');
      image.onload = function () {
        const result = unsafeWindow.unscrambleImg(image, 50, data.seed);
        resolve(result[0].toDataURL().replace('data:image/png;base64,', ''));
      }

      image.src = imageBase64;
    });
  }

})(axios, JSZip, saveAs, ImageDownloader);