Desu Image Downloader

Download images with original filenames on desuarchive.org and add download button to direct image pages

目前為 2024-08-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.19
// @description  Download images with original filenames on desuarchive.org and add download button to direct image pages
// @author       Anonimas
// @match        https://desuarchive.org/*
// @match        https://desu-usergeneratedcontent.xyz/*
// @grant        GM_download
// @grant        GM_addStyle
// @namespace https://greasyfork.org/users/1342214
// ==/UserScript==

(function() {
    'use strict';

    GM_addStyle(`
        #download-button {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px 20px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
            text-decoration: none;
            font-family: Arial, sans-serif;
        }
        #download-button:hover {
            background-color: rgba(0, 0, 0, 0.7);
        }
    `);

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

    function appendFilenameToUrl(url, filename) {
        return `${url}?filename=${encodeURIComponent(filename)}`;
    }

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

    function handleImageClick(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);

        const newUrl = appendFilenameToUrl(imageUrl, originalFilename);
        window.open(newUrl, '_blank');
    }

    function addDownloadButtonToImagePage() {
        if (window.location.hostname === 'desu-usergeneratedcontent.xyz') {
            const button = document.createElement('a');
            button.id = 'download-button';
            button.textContent = 'Download';

            const imageUrl = window.location.href;

            const urlParams = new URLSearchParams(window.location.search);
            const originalFilename = urlParams.get('filename') || extractFilenameFromUrl(imageUrl);

            button.href = imageUrl;
            button.download = originalFilename;

            document.body.appendChild(button);
        }
    }

    function extractFilenameFromUrl(url) {
        return url.substring(url.lastIndexOf('/') + 1);
    }

    if (window.location.hostname === 'desuarchive.org') {
        document.querySelectorAll('a.post_file_filename').forEach(link => {
            link.addEventListener('click', event => {
                event.preventDefault();
                const imageUrl = link.closest('a').href;
                const originalFilename = getFullFilename(link);
                downloadImage(imageUrl, originalFilename);
            });
        });

        document.querySelectorAll('a[href*="//desu-usergeneratedcontent.xyz/"] i.icon-download-alt').forEach(button => {
            button.closest('a').addEventListener('click', event => {
                event.preventDefault();
                const imageUrl = button.closest('a').href;
                let filenameElement = button.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/"] img').forEach(image => {
            image.closest('a').addEventListener('click', handleImageClick);
        });
    }

    addDownloadButtonToImagePage();
})();