Reddit Video/Image/GIF Downloader [Redditsave]

Adds a download button to Reddit posts that contain videos, images, or GIFs. The button will open a new tab to redditsave.com with the video/image/GIF URL pre-filled.

// ==UserScript==
// @name         Reddit Video/Image/GIF Downloader [Redditsave]
// @version      1.0
// @license      MIT
// @description  Adds a download button to Reddit posts that contain videos, images, or GIFs. The button will open a new tab to redditsave.com with the video/image/GIF URL pre-filled.
// @icon         https://www.reddit.com/favicon.ico
// @author       MahdeenSky
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @match        https://www.reddit.com/*
// @exclude      https://www.reddit.com/message/compose/*
// @noframes
// @namespace https://greasyfork.org/users/1279521
// ==/UserScript==

(function() {
    'use strict';

    $(function(){
        AddButtons();
        setInterval(AddButtons, 200);
    });

    function AddButtons() {
        if (window.location.href.includes("/comments/")) {
            addDownloadButtonToCommentsPage();
        } else {
            addDownloadButtonToBrowsePage();
        }
    }

    function addDownloadButtonToCommentsPage() {
        let targets = $("shreddit-post[post-type='video'], shreddit-post[post-type='image']");
        targets.each(function(){
            let post = $(this);
            if(post.find(".downloadVid").length == 0) {
                let dlUrl = post.attr('permalink');
                let buttonBar = post.find("div[slot='credit-bar']");

                // Create the download button
                let downloadButton = createDownloadButton(dlUrl);

                buttonBar.append(downloadButton);
            }
        });
    }

    function addDownloadButtonToBrowsePage() {
        let targets = $("shreddit-post[post-type='video'], shreddit-post[post-type='image']");
        targets.each(function(){
            let post = $(this);
            if(post.find(".downloadVid").length == 0) {
                let dlUrl = post.attr('permalink');
                let buttonBar = post.find("span[slot='credit-bar']");

                // Create the download button
                let downloadButton = createDownloadButton(dlUrl);

                buttonBar.append(downloadButton);
            }
        });
    }

    // Helper function to create the download button element
    function createDownloadButton(dlUrl) {
        return $(`
            <span class="flex items-center pl-xs">
                <shreddit-async-loader class="relative min-w-[32px] -mr-[7px]" bundlename="shreddit_post_overflow_menu">
                    <div class="downloadVid">
                        <button class="rounded-full p-xs flex items-center justify-center hover:bg-neutral-background-hover">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-download" viewBox="0 0 16 16" style="color: rgb(215, 218, 220);">
                                <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
                                <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
                            </svg>
                            <span class="sr-only" style="margin-left: 5px;">Download</span>
                            <span style="margin-left: 5px;">Download</span>
                        </button>
                    </div>
                </shreddit-async-loader>
            </span>
        `).click(function(e) {
            e.preventDefault();
            window.open("https://redditsave.com/info?url=" + encodeURIComponent(encodeURI("https://www.reddit.com" + dlUrl)), "_blank");
        });
    }

})();