Reddit Image Downloader

Adds download button for image post in reddit

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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();
    }
})();