GanmaDownloader

Manga downloader for ganma.jp

// ==UserScript==
// @name         GanmaDownloader
// @namespace    https://github.com/Timesient/manga-download-scripts
// @version      0.6
// @license      GPL-3.0
// @author       Timesient
// @description  Manga downloader for ganma.jp
// @icon         https://ganma.jp/web/favicon.ico
// @homepageURL  https://greasyfork.org/scripts/451869-ganmadownloader
// @supportURL   https://github.com/Timesient/manga-download-scripts/issues
// @match        https://ganma.jp/*
// @require      https://unpkg.com/axios@0.27.2/dist/axios.min.js
// @require      https://unpkg.com/jszip@3.7.1/dist/jszip.min.js
// @require      https://unpkg.com/file-saver@2.0.5/dist/FileSaver.min.js
// @require      https://update.greasyfork.org/scripts/451810/1398192/ImageDownloaderLib.js
// @grant        GM_info
// @grant        GM_xmlhttpRequest
// ==/UserScript==

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

  // reload page when enter or leave chapter
  const re = /https:\/\/ganma\.jp\/web\/reader\/(?<alias>.*)\/(?<episodeID>[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})\/\d+/;
  const oldHref = window.location.href;
  const timer = setInterval(() => {
    const newHref = window.location.href;
    if (re.exec(newHref) && re.exec(oldHref) && re.exec(newHref).groups.episodeID === re.exec(oldHref).groups.episodeID) return;
    if (re.test(newHref) || re.test(oldHref)) {
      clearInterval(timer);
      window.location.reload();
    }
  }, 200);

  // return if not reading chapter now
  if (!re.test(oldHref)) return;
  
  // get data of current episode
  const { alias, episodeID } = re.exec(oldHref).groups;
  const episodeData = await axios({
    method: 'GET',
    url: `https://ganma.jp/api/1.0/magazines/web/${alias}`,
    headers: { 'x-from': `https://ganma.jp/${alias}/${episodeID}/0` }
  }).then(res => res.data.root.items.filter(episodeData => episodeData.id === episodeID)[0]);

  // get title of current episode
  const title = `${episodeData.title} ${episodeData.subtitle || ''}`.trim();

  // get url of images
  const { token, baseUrl } = episodeData.page;
  const imageURLs = episodeData.page.files.map(file => `${baseUrl}${file}?${token}`);

  // append url of afterword image
  if (episodeData.afterwordImage && episodeData.afterwordImage.url) imageURLs.push(episodeData.afterwordImage.url);

  // setup ImageDownloader
  ImageDownloader.init({
    maxImageAmount: imageURLs.length,
    getImagePromises,
    title
  });

  // collect promises of image
  function getImagePromises(startNum, endNum) {
    return imageURLs
      .slice(startNum - 1, endNum)
      .map(url => getImage(url)
        .then(ImageDownloader.fulfillHandler)
        .catch(ImageDownloader.rejectHandler)
      );
  }

  // get promise of image
  function getImage(url) {
    return new Promise(resolve => {
      GM_xmlhttpRequest({
        method: 'GET',
        url,
        responseType: 'arraybuffer',
        onload: res => resolve(res.response)
      });
    });
  }

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