Sleazy Fork is available in English.

MICIDOL|CEVERIA|TELEGRA.PH 写真打包

MIC IDOL等网站写真打包下载,更多站点的支持以后添加。

< Feedback on MICIDOL|CEVERIA|TELEGRA.PH 写真打包

Review: Good - script works

§
Posted: 04.03.2023

在作者的代码上修改了一下,又能下载everia的图片了。

// ==UserScript==
// @name         CEVERIA写真打包
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  网站写真打包下载
// @author       Xie
// @match        http*://everia.club/*/*/*/*
// @run-at      document-end
// @icon         https://www.google.com/s2/favicons?sz=64&domain=everia.club
// @grant        GM_xmlhttpRequest
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.4/jszip.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.2/FileSaver.min.js
// ==/UserScript==

(function() {
    'use strict';

    let is_down = false;
    let site = 'EVERIA';
    let log = console.log;

    /*获取标题和图片的selector*/
    const title_rules ='h1.entry-title';
    const image_rules = 'figure.wp-block-image.size-large img';

    /*获取并输出标题*/
    let titEle = document.querySelector(title_rules);
    if (titEle == null) {
        titEle = document.querySelector('title');
    }
    let title = titEle.innerText.trim();
    log(title);


    /*创建下载按钮*/
    let show = document.createElement('button');
    const btn_style = `.show_btn{
    position:fixed;
    left:50px;
    bottom:50px;
    width: 100px;
    height: 50px;
    border-radius:50%;
    border: none;
    background-color: #f44949;
    border: 1px solid #f44949;
    color:#fff;padding:0;
    }
    .show_btn:active{
    background-color: #ca8e9f;
    }`;
    let t = document.createElement('style');
    t.innerText = btn_style;
    document.querySelector('head').appendChild(t);
    show.innerText = '下载';
    show.setAttribute('class', 'show_btn');
    document.body.appendChild(show);

    /*获取下载图片的url*/
    /*麻了,为了获取img标签上的data-src属性废了老大劲才找到解决方法*/
    //https://cloud.tencent.com/developer/ask/sof/108741
    //https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dataset
    let img_eles = document.querySelectorAll(image_rules);
    let imgs = [];
    img_eles.forEach(x=>{imgs.push(x.dataset.src)});

    let num = imgs.length;
    log('图片总数: ', num);
    log(imgs);
    let downloaded = 0;
    let zip = new JSZip();
    let folder = zip.folder(title);
    let promises = [];


    /*为图片命名的函数*/
    function zeropadding(n){
        const max = 3;
        let s = String(n);
        let len = s.length;
        if(len < max){
            let x = max - len;
            for(let i = 0;i<x;i++){
                s='0'+s;
            }
        }
        return s;
    }

    /*下载图片的函数*/
    function request(url, referrerStr, timeoutInt = 100000) {
        return new Promise((resolve, reject) => {
            //console.log(`发起网址请求:${url}`);
            GM_xmlhttpRequest({
                url,
                method: 'GET',
                overrideMimeType: 'text/plain; charset=x-user-defined',
                headers: {
                    "Cache-Control": "no-cache",
                    referrer: referrerStr
                },
                timeout: timeoutInt,
                onload: response => { //console.log(url + " reqTime:" + (new Date() - time1));
                    response.loadstuts = true;
                    resolve(response);
                },
                onabort: response => {
                    console.log(url + " abort");
                    response.loadstuts = false;
                    resolve(response);
                },
                onerror: response => {
                    console.log(url + " error");
                    //console.log(response);
                    response.loadstuts = false;
                    resolve(response);
                },
                ontimeout: response => {
                    console.log(`${url} ${timeoutInt}ms timeout`);
                    response.loadstuts = false;
                    response.finalUrl = url;
                    resolve(response);
                },
            });
        });
    }


    /*调用request()下载图片并打包为zip压缩包*/
    function zip_img() {
        for (let index = 0;index<num;index++) {
            let img = imgs[index];
            let p = request(img);
            p.then(res => {
                // let url = res.url;
                if (res.loadstuts == false) {
                    throw new Error('下载失败');
                }
                let img_name = zeropadding(index)+'.jpg';
                log('下载完成: ' + img_name);
                var r = res.responseText, data = new Uint8Array(r.length), i = 0;
                while (i < r.length) {
                    data[i] = r.charCodeAt(i);
                    i++;
                }
                var blob = new Blob([data], { type: 'image/jpeg' }); // 转为Blob类型
                folder.file(img_name, blob, { binary: true }); // 压入zip中
                downloaded++;
                show.innerText = `${num}`;
                console.log(`waiting ${downloaded}/${num}`);
            }).catch(reason => {
                console.log('download error: ', reason);
            })
            promises.push(p);
        }
        Promise.all(promises).then(() => {
            is_down = true;
            show.enable = true;
            show.innerText = '下载完毕'
            log(`共下载: ${downloaded}/${num}`)
            zip.generateAsync({ type: "blob", base64: true }).then(function (content) {
                saveAs(content, title + ".zip");
            });
        }).catch(reason => {
            log('final error -> ', reason);
        })
    }

    /*点击按钮进行下载同时避免重复下载*/
    show.onclick = function () {
        if (!is_down) {
            show.enable = false;
            show.innerText = '等待下载';
            zip_img();
        } else {
            log('已下载,请勿重复点击!');
        }
    }
})();
§
Posted: 16.10.2023

好哥哥,everia又空包了,能解决一下吗,感谢!

Post reply

Sign in to post a reply.