CryonicWraith / Eroshare Downloader

// ==UserScript==
// @name         Eroshare Downloader
// @namespace    cryonicwraith.erosharedownloader
// @description  A script to add a nifty little button to download Eroshare albums.
// @version      1.3
// @author       CryonicWraith
// @copyright    2017, CryonicWraith
// @license      Creative Commons Attribution-ShareAlike 4.0 International License
// @updateURL    https://openuserjs.org/meta/CryonicWraith/Eroshare_Downloader.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/CryonicWraith/Eroshare_Downloader.user.js
// @match        *://eroshare.com/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    "use strict";
    var debug = false;

    var Promise = window.Promise;
    if (!Promise) {
        Promise = JSZip.external.Promise;
    }

    function AddZip(url) {
        return new Promise(function(resolve, reject) {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                responseType: "arraybuffer",
                onload: function(response) {
                    resolve(response.response);
                },
                onerror: function(err){
                    reject(err);
                }
            });
        });
    }

    var zip = new JSZip();

    $("div.album-info").append($("<a>").addClass("btn grey-cog-link download").css("width","125px").append($("<i>").addClass("fa fa-download fa-fw fa-lg")).append($("<span>").addClass("download-label").text("Download")));

    $("a.download").click(function (){
        $("div.item-container").each(function() {
            switch ($(this).attr("data-type")) {
                case "Image":
                    zip.file(($(this).attr("id") + ".jpg"), AddZip(("https:" + $(this).find("img").attr("src"))));
                    if(debug === true) {console.log("DL - Added IMAGE - URL: " + "http:" +($(this).find("img").attr("src")) + "FN: " + ($(this).attr("id") + ".jpg"));}
                    break;
                case "Video":
                    zip.file(($(this).attr("id") + ".mp4"), AddZip(($(this).find("video>source[data-quality='HD']").attr("src"))));
                    if(debug === true) {console.log("DL - Added VIDEO - URL: " + ($(this).find("video>source[data-quality='HD']").attr("src")) + " - FN: " + ($(this).attr("id") + ".jpg"));}
                    break;
            }
        });

        zip.generateAsync({type:"blob"}, function updateCallback(metadata) {
            if ($(".download-label").text() != (metadata.percent.toFixed(0) + "%")){
                $(".download-label").text(metadata.percent.toFixed(0) + "%");
            }
        }).then(function (content) {
            // see FileSaver.js
            saveAs(content, "Eroshare - " + ($("#album-data").attr("data-album-slug")) + ".zip");
        });
    }).fail(function (err) {
        console.log("ERROR: " + err);
    });
})();