Greasy Fork

Greasy Fork is available in English.

劲到六七之图片打包下载工具

A tool to help you quickly package and download images on the web

当前为 2024-04-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         劲到六七之图片打包下载工具
// @namespace    http://tampermonkey.net/
// @version      2024-04-16
// @description  A tool to help you quickly package and download images on the web
// @description:zh-CN  一个帮你快速打包下载网页上图片的工具
// @author       <[email protected]>
// @license      GPLv3
// @match        https://www.cnblogs.com/
// @icon         https://s21.ax1x.com/2024/04/16/pFxNgjH.jpg
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @grant        GM_xmlhttpRequest
// @include     *://medium.com/*
// @include     *://twitter.com/*
// @include     *://*.twitter.com/*
// @include     *://weibo.com/*
// @include     *://*.weibo.com/*
// @include     *://*.vmgirls.com/*
// @include     *://wallpaperhub.app/*
// @include     *://*.bing.com/*
// @include     *://*.msn.cn/*
// @include     *://instagram.com/*
// @include     *://*.instagram.com/*
// @include     *://instagram.com/*
// @include     *://*.instagram.com/*
// @include     *://tiktok.com/*
// @include     *://*.tiktok.com/*
// @include     *://*.douyin.com/*
// @include     *://*.kuaishou.com/*
// @include     *://*.xiaohongshu.com/*

// ==/UserScript==

(function() {
    let imgs = document.querySelectorAll('img')
    let urls = []
    imgs.forEach(item=>{
        if(!item.src)return
        urls.push(item.src)
    })
    const imageUrls = urls
    const zip = new JSZip();
    Promise.all(imageUrls.map((url, index) =>{
        return new Promise((resolve, reject) =>{
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                responseType: "blob",
                onload: function(response) {
                    if (response.status === 200) {
                        let blob = response.response
                        const filename = `image${index + 1}.jpg`;
                        zip.file(filename, blob, { binary: true });

                    } else {
                        console.error("Request failed with status " + response.status);
                    }
                    resolve()
                },
                onerror: function(e) {
                    console.error("Request failed: " + e.message);
                    resolve()
                }
            });
        })

    })).then(() => {
        zip.generateAsync({ type: 'blob' }).then(blob => {
            saveAs(blob, 'images.zip');
        });
    }).catch(error => {
        console.error('Error downloading images:', error);
    });
})();