Greasy Fork

Greasy Fork is available in English.

图片下载

网页图片下载功能

// ==UserScript==
// @name         图片下载
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  网页图片下载功能
// @author       Negronis
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        unsafeWindow
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    let pa = () => {
        var Yuan = document.querySelectorAll("img")
        var arr = []
        Yuan.forEach(e => {
            arr.push({
                name: e.getAttribute("src"),
                url: e.getAttribute("src"),
            })
        })
        var o = { 
            title: (document.getElementsByTagName("title")[0].innerHTML),
            list: arr
        }
        console.log(o)
        return o
    }
    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

    // 主下载函数
    async function downloadImages(pa) {
        let list = pa['list'];  
        const t = pa['title']
        const downloadDelay = 1000;  

        for (const [index, item] of list.entries()) {
            try { 
                const response = await fetch(item.url);
                if (!response.ok) throw new Error(`HTTP ${response.status}`); 
                const blob = await response.blob(); 
                let extArray = [
                    ".jpg",
                    ".png",
                    ".jpeg",
                    ".gif",
                    ".bmp",
                    ".webp",
                ]
                const ext = extArray.filter(e=>item.name.slice(item.name.lastIndexOf(".")).indexOf(e) > -1)[0] || ".jpg";
                const n = t + index
                const filename = n + `${ext}`; 
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = filename;
                link.style.display = 'none'; 
                document.body.appendChild(link);
                link.click(); 
                setTimeout(() => {
                    document.body.removeChild(link);
                    URL.revokeObjectURL(link.href);
                }, 100); 
                console.log(`[${index + 1}/${list.length}] 下载成功: ${filename}`); 
                if (index < list.length - 1) await delay(downloadDelay);

            } catch (error) {
                console.error(`[${index + 1}] 下载失败:`, error.message);
            }
        }
    }
    let download = ()=>{
        let result = pa()
        downloadImages(result)
    }
    let createBtn = ()=>{
        var btn = document.createElement("button")
        btn.setAttribute("style" , `
            position:fixed;
            z-index:909999;
            right:50px;
            top:50px;
            width:100px;
            height:100px;
            border-radius:50%;
            opacity:0.8;
            background:#2d8cf0;
            color:#fff;
            font-size:18px;
        `)
        btn.innerText = "点击我"
        btn.setAttribute('id' , 'cli')
        document.body.appendChild(btn);
    }
    createBtn()
    document.querySelector('#cli').onclick = download;
})();