Reddit Video Downloader

A script to that allows you to download videos hosted on Reddit by pressing Ctrl+S or Meta+S on the comments section.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                Reddit Video Downloader
// @author              Berk "SAS41" Alyamach
// @homepage            https://github.com/sas41/
// @homepageURL         https://github.com/sas41/
// @description         A script to that allows you to download videos hosted on Reddit by pressing Ctrl+S or Meta+S on the comments section.

// @icon                https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_32.png?raw=true
// @iconURL             https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_32.png?raw=true
// @icon64URL           https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_64.png?raw=true

// @copyright           2018, Berk (sas41) Alyamach - https://github.com/sas41/
// @license             MIT
// @grant               none

// @contributionAmount  €1.00
// @contributionURL     https://www.paypal.me/sas41/1

// @namespace           reddit
// @include             *://*reddit.com/r/*/*/*/*

// @supportURL          https://github.com/sas41/RedditVideoDownloader/issues
// @version             1.0.5
// ==/UserScript==

// ==OpenUserJS==
// @author              sas41
// @contributionAmount  €1.00
// @contributionURL     https://www.paypal.me/sas41/1
// ==/OpenUserJS==

var jsonLink = document.location.href.split('?')[0] + '.json';
var downloadLink = '';

function getJSON(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function()
    {
        var status = xhr.status;
        if (status === 200)
        {
            var response = xhr.response;
            if (!response[0].data.children[0].data.secure_media.reddit_video.is_gif)
            {
                if(confirm('Unfortunately, Reddit doesn\'t support saving of audio along with the video, Download without audio?'))
                {
                    downloadLink = response[0].data.children[0].data.secure_media.reddit_video.fallback_url;
                    downloadURI(downloadLink,'');
                }
            }
            else
            {
                downloadLink = response[0].data.children[0].data.secure_media.reddit_video.fallback_url;
                    downloadURI(downloadLink,'');
            }
        }
        else
        {
            alert('Sorry, Download Failed');
        }
    };
    xhr.send();
}

function downloadURI(url, n)
{
    var save = document.createElement('a');
    save.href = url;
    save.download = n || url;
    
    var event = document.createEvent("MouseEvents");
    event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    save.dispatchEvent(event);

    try
    {
        document.body.removeChild(save);
    }
    catch(err){}
}

document.addEventListener('keydown', (event) => {
    if (event.ctrlKey || event.metaKey)
    {
        if (event.key === 's' || event.key === 'S' )
        {
            event.preventDefault();
            getJSON(jsonLink);
        }
    }
});