打包下载wnacg的图片

由于wnacg的直接下载通道无法使用,所以我想通过脚本请求每一张图片,之后打包保存。

Version vom 09.04.2021. Aktuellste Version

// ==UserScript==
// @name:en         Pack to download-wnacg
// @name         打包下载wnacg的图片
// @namespace    raincore
// @version      1.0
// @description 由于wnacg的直接下载通道无法使用,所以我想通过脚本请求每一张图片,之后打包保存。
// @description:en  Try downloading the wnacg image as a package
// @author       raincore
// @icon http://www.wnacg.com/favicon.ico
// @match       *://*.wnacg.com/photos-index-*
// @match       *://*.wnacg.wtf/photos-index-*
// @match       *://*.wnacg.org/photos-index-*
// @require https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js
// @require https://cdn.staticfile.org/FileSaver.js/1.3.8/FileSaver.min.js
// @require https://cdn.staticfile.org/jszip/3.3.0/jszip.min.js
// @connect *
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// ==/UserScript==
let a;
let maxSize;
let tab;
let zip;
let file_name;

(function () {
    'use strict';
    a = document.createElement('a');
    maxSize = 0;
    tab = $('.uwthumb')[0];
    zip = new JSZip();
    file_name = $('#bodywrap h2').text() + ".zip";
    a.className = "btn";
    a.style = "width:130px;";
    a.text = "打包下载";
    a.onclick = main;
    tab.append(a);
})();

function main() {
    a.onclick = null;
    let curWwwPath = window.document.location.href;
    let aid = curWwwPath.substring(curWwwPath.indexOf("aid") + 4, curWwwPath.lastIndexOf(".html"));
    aid = parseInt(aid);
    getImgList(aid, download);
}

function download(imglist) {
    let array = [];
    array = imglist.map((i) => "https:" + i.url)
    a.text = "开始下载...";
    maxSize = imglist.length - 1;
    getImage(array, 0, compress);
}

function compress(data, i) {
    zip.file(PrefixInteger(i, 4) + ".png", data);
    if (i == maxSize - 1) {
        a.text = "正在压缩...";
        zip.generateAsync({type: "blob"}, function updateCallback(metadata) {
            a.text = "正在压缩:" + metadata.percent.toFixed(2) + " %";
        })
            .then(function (content) {
                saveAs(content, file_name);
                a.text = "下载完成";
                a.onclick = main;
                a.onmouseover = function () {
                    this.text = "重新下载";
                };
            });
    }
}

function getImage(images, index, callback) {
    GM_xmlhttpRequest({
        "method": "GET",
        "url": images[index],
        "responseType": "arraybuffer",
        "onload": function (result) {
            a.text = "已下载" + (index + 1) + "/" + maxSize;
            var arrayBuffer = result.response;
            callback(arrayBuffer, index);
            if (index < maxSize) {
                getImage(images, index + 1, callback)
            }
        }
    });
}

function getImgList(aid, callback) {
    a.text = "加载资源列表...";
    GM_xmlhttpRequest({
        "method": "GET",
        "url": "/photos-gallery-aid-" + aid + ".html",
        "onload": function (result) {
            let rawText = result.response;
            rawText = rawText.split('\n');
            let raw = "";
            for (let i = 2; i < rawText.length - 5; i++) {
                rawText[i] = rawText[i].substring(18);
                rawText[i] = rawText[i].substring(0, rawText[i].indexOf('");'));
                rawText[i] = rawText[i].replace(/\\/g, "");
                raw += rawText[i];
            }
            eval(raw);
            a.text = "加载成功!";
            callback(imglist);
        }
    });
}

function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}