Desu Image Downloader

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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 https://greasyfork.org/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);
        });
    });

})();