Reddit Video Downloader

Downloads video files from reddit

This script is marked as "external"
This script was commisioned by someone else, and I will not check if it works. If it breaks, contact me
Click here to install Browse More Scripts
// ==UserScript==
// @name         Reddit Video Downloader
// @namespace    0096e29e26832b043838ac5d3f7c74850406038a
// @version      0.2
// @description  Downloads video files from reddit
// @author       /u/AyrA_ch
// @match        https://*.reddit.com/*
// @match        https://reddit.com/*
// @grant        none
// @run-at       document-idle
// @external     true
// ==/UserScript==

// Changelog
// 0.1 - Initial version
// 0.2 - Added some comments

(function (e, titleLink) {
	var url = e && e.dataset.mpdUrl;

	var getBinary = function (url) {
		var req = new XMLHttpRequest();
		req.open("GET", url);
		//This line is the secret to obtaining a video file and not some crappy html
		//Imgur does something similar with their gifv pages
		req.setRequestHeader("Accept", "video\/*, *\/*;q=0.1");
		//request as binary and not text
		req.responseType = "blob";
		req.addEventListener("load", function () {
			if (req.response) {
				//Get temporary URL
				var obj = URL.createObjectURL(req.response);
				console.log("Download size for", obj, req.response.size);

				//Download via temporary link
				var a = document.createElement("a");
				a.href = obj;
				//This extracts the short 6 letter code from the URL (/r/Subreddit/comments/code)
				a.download = location.href.match(/\/r\/[^/]+\/[^/]+\/([^/]+)/)[1] + ".mp4";
				a.textContent = "Download Video File";
				a.click();
				console.log("Video download ready at temporary element", a);
				//This frees up memory again
				setTimeout(function () {
					URL.revokeObjectURL(obj);
					console.log("URL revoked", obj);
				}, 5000);
			}
		});
		req.send(null);
	};

	//Maps video properties in an XML to a JS object
	var map = function (v) {
		return {
			width: +v.getAttribute("width"),
			height: +v.getAttribute("height"),
			bandwidth: +v.getAttribute("bandwidth"),
			url: (new URL(v.querySelector("BaseURL").textContent, url)).href
		};
	};

	//Converts array like object to array
	var toArray = function (a) {
		return Array.prototype.slice.call(a);
	};

	//Sorts by height descending, making the highest quality the top element
	var sort = function (a, b) {
		return b.height - a.height;
	};

	//Starts the video download process by obtaining and parsing the mpd file
	var download = function (url) {
		var req = new XMLHttpRequest();
		req.open("GET", url);
		req.addEventListener("load", function () {
			//Get all video qualities and map sorted to a plain JS object
			var formats = toArray(req.responseXML.querySelectorAll("Representation")).map(map).sort(sort);
			//Best format is first in list
			var best = formats[0];
			console.log("MPD download complete, best quality", best);
			//Download video file itself
			getBinary(best.url);
		});
		req.send(null);
	};

	if (url && titleLink) {
		var a = document.createElement("a");
		a.href = "#";
		a.textContent = decodeURIComponent("%F0%9F%92%BE"); //Floppy disk Icon
		a.addEventListener("click", function (e) {
			e.preventDefault();
			e.stopPropagation();
			download(url);
		});
		titleLink.parentNode.appendChild(a);
		console.debug("Downloader ready. mpd file URL:", url);
	}
})(document.querySelector("div[data-mpd-url]"), document.querySelector("p> a.title"));

/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.

DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/

Copyright © 2018 by Kevin Gut 📧 | More services | Generated for 98.80.143.34