sanson222 / ZScript for TMO

// ==UserScript==
// @name         ZScript for TMO
// @namespace    http://tampermonkey.net/
// @version      0.3.3
// @description  Download Manga from this page
// @author       Zearch
// @match        https://tmofans.com/viewer/*/cascade
// @grant        none
// @license         MIT
// @updateURL   https://openuserjs.org/meta/sanson222/ZScript_for_TMO.meta.js
// @copyright 2019, sanson222 (https://openuserjs.org/users/sanson222)
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js
// ==/UserScript==

(function() {
    'use strict';

    var zip = new JSZip();
    var dirManga = 'images/';

    var canvasCollection = document.getElementById('viewer-container').childNodes;
    var canvasCollectionArr = [].slice.call(canvasCollection);

    var filteredCollection = canvasCollectionArr.filter(function(i) {
        if (i.nodeName === '#text') {
            return false;
        }
        return true;
    });



    var mappedCollection = filteredCollection.map(function(el){ return el.childNodes[1];});

    var counter = 0;

    var botonFlotador = document.createElement('button');
    botonFlotador.innerText = "DESCARGAR MANGA";
    botonFlotador.style.width = "100%";
    botonFlotador.style.borderRadius = "5px";
    classAllow();
    addMagic();
    document.body.insertBefore(botonFlotador, document.body.firstChild);

    function eventMagic(e) {
        e.preventDefault();
        removeMagic();
        classNotAllow();
        MAGIC();
    }

    function removeMagic() {
      botonFlotador.removeEventListener('click', eventMagic);
    }

    function addMagic() {
       botonFlotador.addEventListener('click', eventMagic);
    }

    function classAllow() {
        botonFlotador.disabled = false;
        botonFlotador.style.color = "blue";
        botonFlotador.style.cursor = "pointer";
        botonFlotador.style.backgroundColor = "white";
    }

    function classNotAllow() {
        botonFlotador.disabled = true;
        botonFlotador.style.color = "gray";
        botonFlotador.style.backgroundColor = "white";
        botonFlotador.style.cursor = "progress";
    }

    function MAGIC() {
        
        try {
             console.log(filteredCollection);
        mappedCollection.forEach(function(zImg, numberImg) {

    zImg.crossOrigin = "";
    zImg.src = zImg.src;

    zImg.onload = function() {
        var _temporalCanvas = document.createElement('canvas');
    var _ctx = _temporalCanvas.getContext('2d');

    _temporalCanvas.width = zImg.naturalWidth;
    _temporalCanvas.height = zImg.naturalHeight;

    _ctx.drawImage(zImg,0,0);
    var imgName = numberImg > 9 ? '0'+numberImg : '00'+numberImg;

    _temporalCanvas.toBlob(function(blob) {
        zip.file(imgName+'.png', blob);
        counter++;
        if (counter === mappedCollection.length) {
            zip.generateAsync({type: 'blob'}).then(function(blob1) {
                // download code
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.style = "display: none";
                var url = window.URL.createObjectURL(blob1);
                a.href = url;
                a.download = document.title+'.zip';
                a.click();
                window.URL.revokeObjectURL(url);
                classAllow();
                addMagic();
            });
        }
    });
    };

            zImg.onerror = function() { throw new Error("Error"); }


});
    } catch (e) {
        console.log(e);
        alert("Error: you forgot activate the CORS plugin?");
    }
    }
})();