Reddit Image Downloader

Adds download button for image post in reddit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit Image Downloader
// @namespace    http://tampermonkey.net/
// @version      2024-09-12
// @description  Adds download button for image post in reddit
// @author       FlinCode
// @match        https://www.reddit.com/r/*/comments/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const img = document.getElementById("post-image")
    if (img) {
        const items = img.srcset.split(",").map(item => item.trim())
        const imgSrc = items[items.length - 1]
        console.log(imgSrc)
        addUrls([imgSrc])
    } else {
        const imgs = document.getElementsByClassName("media-lightbox-img")

        let urls = []
        for (let i = 0; i < imgs.length; i++) {
            const img = imgs[i]
            const items = img.srcset.split(",").map(item => item.trim())
            const imgSrc = items[items.length - 1]
            urls.push(imgSrc)
            console.log("Image: " + i, imgSrc)
        }
        addUrls(urls)
    }

    function addUrls(urls) {
        const box = querySelectorAllShadows("shreddit-post")[0]
        // add url buttons to download
        urls.forEach((url, idx) => {
            url = convertUrl(url)
            const a = document.createElement("a")
            a.innerHTML = "Download " + (idx + 1)
            a.href = url
            a.download = url.split("/").pop()
            a.target = "_blank"
            a.className = "button-bordered button-small button join-btn leading-none min-w-[100px]"
            a.style.padding = "9px";
            a.style.textDecoration = "none"
            a.style.marginLeft = "5px"
            box.appendChild(a)
        });
    }

    // https://i.redd.it/file-name.jpeg

    function convertUrl(url) {
        let base = "https://i.redd.it/";
        const u = new URL(url)
        const stem = u.pathname.split("v0-").pop()
        return base + stem
    }

    function querySelectorAllShadows(selector, el = document.body) {
        const childShadows = Array.from(el.querySelectorAll('*')).
        map(el => el.shadowRoot).filter(Boolean);

        const childResults = childShadows.map(child => querySelectorAllShadows(selector, child));

        const result = Array.from(el.querySelectorAll(selector));
        return result.concat(childResults).flat();
    }
})();