Greasy Fork

Desu Image Downloader

Download images with original filenames on desuarchive.org (all boards)

目前为 2024-08-06 提交的版本。查看 最新版本

// ==UserScript==
// @name         Desu Image Downloader
// @version      1.15
// @description  Download images with original filenames on desuarchive.org (all boards)
// @author       Anonimas
// @match        https://desuarchive.org/*
// @grant        GM_download
// @namespace https://greasyfork.org/users/1342214
// ==/UserScript==

(function() {
    'use strict';

    function getFullFilename(element) {
        return element.getAttribute('title') || element.textContent.trim();
    }

    function downloadImage(imageUrl, originalFilename) {
        if (imageUrl && originalFilename) {
            GM_download({
                url: imageUrl,
                name: originalFilename,
                onload: () => {},
                onerror: (error) => console.error('Download error:', error)
            });
        }
    }

    function handleDownload(event) {
        event.preventDefault();
        const imageLink = event.target.closest('a[href*="//desu-usergeneratedcontent.xyz/"]');
        if (!imageLink) return;

        const imageUrl = imageLink.href;
        let filenameElement = imageLink.closest('div.post_file, article.thread, article.post')?.querySelector('a.post_file_filename');
        if (!filenameElement) return;

        const originalFilename = getFullFilename(filenameElement);
        downloadImage(imageUrl, originalFilename);
    }

    document.querySelectorAll('a[href*="//desu-usergeneratedcontent.xyz/"] i.icon-download-alt').forEach(button => {
        button.closest('a').addEventListener('click', handleDownload);
    });

    document.querySelectorAll('a.post_file_filename').forEach(link => {
        link.addEventListener('click', handleDownload);
    });
})();