Adds button to direct link or download the stupidly hard to save or share directly reddit videos. Designed to work on new Reddit only. Buttons appear when viewing the specific post -- does not work on preview/expand on post listing pages.
Verze ze dne
// ==UserScript==
// @name Reddit Video Downloader
// @namespace https://lawrenzo.com/p/reddit-video-downloader
// @version 0.3.1
// @description Adds button to direct link or download the stupidly hard to save or share directly reddit videos. Designed to work on new Reddit only. Buttons appear when viewing the specific post -- does not work on preview/expand on post listing pages.
// @author Lawrence Sim
// @license WTFPL (http://www.wtfpl.net)
// @grant none
// @match *://*.reddit.com/*
// ==/UserScript==
'use strict';
(function() {
var defaultStyles = {
"margin": "0.4em",
"font-size": "0.8em",
"padding": "0.4em 0.6em",
"border-radius": "4px",
"border": "1px solid #d9d9d9",
"background": "#fff"
};
function createBtn(parent, opts) {
let a = document.createElement("a"),
btn = document.createElement("button");
a.append(btn);
a.href= opts.href;
a.target = "_blank";
btn.innerHTML = opts.html;
for(let key in defaultStyles) {
btn.style[key] = defaultStyles[key];
}
parent.append(a);
}
function addLinks(mutated, observer) {
let videoElem = document.querySelector("[data-test-id='post-content'] video");
if(!videoElem) return;
if(videoElem.getAttribute("vlinked")) return observer && observer.disconnect();
videoElem.setAttribute("vlinked", 1);
fetch(window.location.href.split("?")[0].replace(/\/$/, "")+".json")
.then(res => {
if(!res && !res.ok) throw Error(res.statusText);
return res.json();
})
.then(json => {
let postData, vidData, contentDiv;
try {
postData = json[0].data.children[0].data;
if(!postData.secure_media && postData.crosspost_parent_list && postData.crosspost_parent_list.length) {
postData = postData.crosspost_parent_list[0];
}
vidData = postData.secure_media.reddit_video;
} catch(e) { }
if(!vidData) return console.log(postData || json);
console.log(vidData);
contentDiv = videoElem.closest("[data-test-id='post-content']");
createBtn(contentDiv, {
href: vidData.fallback_url,
html: "Direct Video Link (no sound)"
});
createBtn(contentDiv, {
href: window.location.href.replace(/reddit.com\//, "redditsave.com/info?url="),
html: "Download via RedditSave"
});
createBtn(contentDiv, {
href: window.location.href.replace(/reddit.com/, "reddit.tube"),
html: "Download via Reddit.Tube"
});
})
.catch(err => console.log(err) && null);
if(observer) observer.disconnect();
}
addLinks();
(new MutationObserver(addLinks)).observe(document.body, {childList:true, subtree:true});
})();