Greasy Fork

Greasy Fork is available in English.

Desu Image Downloader

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

当前为 2024-07-30 提交的版本,查看 最新版本

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

(function() {
    'use strict';

    function downloadImage(imageUrl, originalFilename) {
        if (imageUrl && originalFilename) {
            GM_download({
                url: imageUrl,
                name: originalFilename,
                onload: () => console.log('Image downloaded successfully!'),
                onerror: (error) => console.error('Download error:', error)
            });
        } else {
            console.error('Could not download image. Unable to find URL or filename.');
        }
    }

    const downloadButtons = document.querySelectorAll('a[href*="//desu-usergeneratedcontent.xyz/"] i.icon-download-alt');
    downloadButtons.forEach(button => {
        button.closest('a').addEventListener('click', (event) => {
            event.preventDefault();
            const imageLink = event.target.closest('a');
            const imageUrl = imageLink.href;

            let filenameElement = imageLink.closest('div.post_file')?.querySelector('a.post_file_filename');
            if (!filenameElement) {
                filenameElement = imageLink.closest('article.thread, article.post')?.querySelector('a.post_file_filename');
            }

            const originalFilename = filenameElement ? filenameElement.textContent.trim() : null;
            downloadImage(imageUrl, originalFilename);
        });
    });

    const filenameLinks = document.querySelectorAll('a.post_file_filename');
    filenameLinks.forEach(link => {
        link.addEventListener('click', (event) => {
            event.preventDefault();

            const originalFilename = link.textContent.trim();
            const imageLink = link.closest('div.post_file, article.thread, article.post')
                                 .querySelector('a[href*="//desu-usergeneratedcontent.xyz/"]');
            const imageUrl = imageLink ? imageLink.href : null;

            downloadImage(imageUrl, originalFilename);
        });
    });

})();