Desu Image Downloader

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

目前為 2024-07-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
        });
    });

})();