Wykop Image Original Filename Downloader

Downloads images from wykop.pl with their original filenames

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Wykop Image Original Filename Downloader
// @namespace    http://tampermonkey.net/
// @icon         https://i.imgur.com/OtAgc3A.png
// @version      1.3
// @description  Downloads images from wykop.pl with their original filenames
// @author       stopbreathing
// @match        https://wykop.pl/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Set to store already processed links
    const processedLinks = new Set();

    // Function to handle image links
    function processImageLinks() {
        // Select all links that match the CDN pattern
        const imageLinks = document.querySelectorAll('a[href*="wykop.pl/cdn"]');

        imageLinks.forEach(link => {
            // Skip if we've already processed this link
            if (processedLinks.has(link)) return;

            // Add to processed set
            processedLinks.add(link);

            // Get the original URL
            const originalUrl = link.href;

            // Check if this is a "Pobierz" button
            const isPobierzButton = link.textContent.trim() === 'Pobierz';

            // If it's a Pobierz button, get the filename from the source link
            let originalFilename;
            if (isPobierzButton) {
                // Find the source link in the same figcaption
                const figcaption = link.closest('figcaption');
                if (figcaption) {
                    const sourceLink = figcaption.querySelector('p a');
                    if (sourceLink) {
                        originalFilename = sourceLink.textContent.trim();
                    }
                }
            } else {
                originalFilename = link.textContent.trim();
            }

            // Get the file extension from the URL
            // Remove query parameters before getting extension
            const extension = originalUrl.split('?')[0].split('.').pop();

            // Only process if we have both filename and extension
            if (originalFilename && extension) {
                // Create a click event handler
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    e.stopPropagation(); // Stop event bubbling

                    // Create a temporary anchor element
                    const tempLink = document.createElement('a');
                    // Use the URL without query parameters
                    tempLink.href = originalUrl;
                    // Ensure the filename is clean (remove any possible query parameters)
                    const cleanFilename = originalFilename.split('?')[0];
                    tempLink.download = `${cleanFilename}.${extension}`;

                    // Append to body, click, and remove
                    document.body.appendChild(tempLink);
                    tempLink.click();
                    tempLink.remove();
                }, { once: true }); // This ensures the event listener only fires once
            }
        });
    }

    // Initial processing
    processImageLinks();

    // Create a debounced version of processImageLinks
    let debounceTimer;
    const debouncedProcess = () => {
        clearTimeout(debounceTimer);
        debounceTimer = setTimeout(processImageLinks, 500);
    };

    // Create a MutationObserver to handle dynamically loaded content
    const observer = new MutationObserver(debouncedProcess);

    // Start observing the document with the configured parameters
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();