Greasy Fork

森空岛图片下载器

在文章详情页右上添加一个下载按钮,用于下载所有swiper-item类下的图片(除首图),若无swiper-item则下载特定来源的webp图片

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

// ==UserScript==
// @name         森空岛图片下载器
// @namespace    https://greasyfork.org/zh-CN/users/1002415-%E5%B0%8F%E6%97%A6
// @version      1.0.1
// @description  在文章详情页右上添加一个下载按钮,用于下载所有swiper-item类下的图片(除首图),若无swiper-item则下载特定来源的webp图片
// @author       小旦
// @match        https://www.skland.com/article?id=*
// @grant        GM_download
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const downloadButton = document.createElement('button');
    downloadButton.textContent = '下载原图';
    downloadButton.style.position = 'fixed';
    downloadButton.style.right = '50px';
    downloadButton.style.top = '11vh';
    downloadButton.style.zIndex = 9999;
    downloadButton.style.padding = '25px 10px';
    downloadButton.style.backgroundColor = 'rgb(55, 55, 55)';
    downloadButton.style.border = '5px solid rgb(200, 235, 33)';
    downloadButton.style.borderRadius = '37px';
    downloadButton.style.marginTop = '-2px';
    downloadButton.style.color = 'rgb(255, 255, 255)';
    downloadButton.style.fontSize = '12px';
    downloadButton.style.lineHeight = '16px';
    downloadButton.style.fontWeight = '700';
    downloadButton.style.fontFamily = 'akrobat';
    downloadButton.style.cursor = 'pointer';
    document.body.appendChild(downloadButton);

    downloadButton.addEventListener('click', function() {
        downloadImages();
    });

    function downloadImages() {
        const containers = document.querySelectorAll('.swiper-item');

        if (!containers.length) {
            const imgs = document.querySelectorAll('img[src^="https://bbs.hycdn.cn/image/"][src$=".webp"]');
            if (!imgs.length) {
                console.error('未找到包含指定来源的 webp 图片');
                return;
            }

            const now = new Date();
            const timestamp = now.toISOString().slice(0, 19).replace(/[-:T]/g, '');
            let count = 1;

            imgs.forEach(img => {
                handleImageDownload(img, timestamp, count++);
            });
        } else {
            const now = new Date();
            const timestamp = now.toISOString().slice(0, 19).replace(/[-:T]/g, '');
            let count = 1;

            containers.forEach((container, index) => {
                if (index === 0) {
                    const firstImage = container.querySelector('img');
                    if (firstImage) {
                        const images = Array.from(container.querySelectorAll('img')).slice(1);
                        images.forEach(img => {
                            handleImageDownload(img, timestamp, count++);
                        });
                    } else {
                        console.error('未找到首张图片');
                    }
                } else {
                    container.querySelectorAll('img').forEach(img => {
                        handleImageDownload(img, timestamp, count++);
                    });
                }
            });
        }
    }

    function handleImageDownload(img, timestamp, count) {
        const src = img.src;
        const filename = `${timestamp}_${count}.webp`;
        console.log(`Downloading image: ${filename}`);
        GM_download({
            url: src,
            name: filename,
            onload: function(response) {
                console.log(`Downloaded: ${filename}`);
            },
            onerror: function(error) {
                console.error(`Failed to download: ${filename}, error: ${error}`);
            }
        });
    }
})();