Skip to content

Instantly share code, notes, and snippets.

@vsubhash
Last active September 10, 2018 06:42
Show Gist options
  • Save vsubhash/cc950f855714b2e6bbef to your computer and use it in GitHub Desktop.
Save vsubhash/cc950f855714b2e6bbef to your computer and use it in GitHub Desktop.
"Download any YouTube video from anywhere" script is a User Scrip for YouTube that will allow you to download videos as offline files from YouTube.com. This script will work best in Firefox 42. It can be installed in Firefox using the Greasemonkey add-on. In "about:config", media source streaming options should also be disabled.
// ==UserScript==
// @name YouTube Downloader JavaScript
// @namespace com.vsubhash.js.youtube-downloader
// @description Downloads YouTube videos
// @include https://www.youtube.com/*
// @version 2018
// @grant none
// ==/UserScript==
/*
* Moral Volcano's YouTub DL JavaScript
* Licensed under GPL v3 as provided at
* https://www.gnu.org/licenses/gpl.html
*
*/
document.addEventListener("DOMContentLoaded", addYouTubeDetectingButton, false);
function parseYTPlayer() {
console.error("Inside parser" );
console.error("Title" + ytplayer.config.args.title);
console.error("Format" + unescape(ytplayer.config.args.adaptive_fmts));
try {
if (document.getElementsByTagName("video")[0] && (document.getElementsByTagName("video")[0].pause)) {
document.getElementsByTagName("video")[0].pause();
}
} catch (e) {
window.alert(e);
}
var arFormatParams, arFormats, i, j, sURL, sQuality, sMimeType, sExtension;
var oSelect, oOption;
oSelect = document.getElementById("mvyJsVideoDownloadList");
if (oSelect == null) {
return;
} else {
// Reset download list
oSelect.length = 0;
}
arFormats = ytplayer.config.args.url_encoded_fmt_stream_map.split(",");
for (i = 0; i < arFormats.length; i++) {
//console.error("Format " + i + ". " + arFormats[i]);
arFormatParams = arFormats[i].split("&")
sURL = "";
for (j = 0; j < arFormatParams.length; j++) {
// console.error("\tFormat parameter " + arFormatParams[j]);
if (arFormatParams[j].indexOf("url=") > -1) {
sURL = unescape(arFormatParams[j].substring("url=".length));
} else {
if (arFormatParams[j].indexOf("type=") > -1) {
sMimeType = unescape(arFormatParams[j].substring("type=".length));
}
if (arFormatParams[j].indexOf("quality=") > -1) {
sQuality = arFormatParams[j].substring("quality=".length);
}
}
}
if ((sURL.length > 0) && (sQuality.length > 0) && (sMimeType.length > 0)) {
oOption = document.createElement("option");
oOption.text = sQuality + " - " + sMimeType;
sURL = sURL +
"&ptk=youtube_none&pltype=contentugc" +
"&title=" +
escape(ytplayer.config.args.title);
//console.error("Final URL " + sURL);
oOption.setAttribute("onclick", "(function() { location.href = '" + sURL + "'; })();");
oSelect.add(oOption);
}
}
if (oSelect.length > 0) {
oSelect.style.display = "inline";
} else {
oSelect.style.display = "none";
window.alert("Some error had occurred");
}
}
function handle_DownloadButtonClick() {
if (document.getElementsByTagName("video").length > 0) {
if (document.getElementsByTagName("video")[0].src.length > 10) {
document.getElementById("mvJsLink").setAttribute("href",document.getElementsByTagName("video")[0].src + "&title=" + escape(document.title));
document.getElementById("mvJsLink").innerHTML = document.getElementsByTagName("video")[0].src.substring(0,35) + "...";
document.getElementById("mvJsLink").click();
} else {
window.alert("First play the video a bit and pause. Then, we will get the video link.");
}
} else {
window.alert("No video has loaded yet.");
}
}
function addYouTubeDetectingButton() {
console.log("Executing YouTube detector");
var i, n, oDlButtonEl, oDlDiv, oVideosList;
if ((location.href.indexOf("youtube.com/embed") != -1) ||
(location.href.indexOf("youtube.com/watch") != -1)) {
console.log("Inside YouTube Iframe");
oDlDiv = document.createElement("div");
oDlDiv.setAttribute("id", "mvyJsDiv");
oDlDiv.setAttribute("style", "color: black; padding-left: 1em; background-color: orange!important; width: 100%!important; height: 1.2cm!important; font-size: 0.34cm!important; line-height: 0.4cm!important; font-family: sans-serif!important; ");
if (location.href.indexOf("youtube.com/embed") > -1) {
oDlButtonEl = document.createElement("input");
oDlDiv.setAttribute("id", "mvJsButton");
oDlButtonEl.setAttribute("type", "button");
oDlButtonEl.setAttribute("value", "Grab Video");
oDlButtonEl.addEventListener("click", handle_DownloadButtonClick, false);
oDlButtonEl.setAttribute("style", "color: white!important; border: 1px solid orange; box-shadow: 2px 2px 5px black; background-color: red!important; height: 0.9cm!important; width: 5cm!important; font-size: 0.4cm!important; line-height: 0.5cm!important; ; font-family: sans-serif!important;");
oDlDiv.appendChild(oDlButtonEl);
oVidLink = document.createElement("a");
oVidLink.setAttribute("id", "mvJsLink");
oVidLink.setAttribute("style", "color: darkblue!important; text-decoration: underline!important; font-size: 0.3cm!important; margin: 0.2cm 1cm 0.2cm 1cm!important;");
oVidLink.innerHTML="Video URL";
oVidLink.setAttribute("href","javascript:window.alert('First play the video a bit and pause. Then, we will get the video link.');");
oDlDiv.appendChild(oVidLink);
document.getElementById("player").parentNode.insertBefore(oDlDiv, document.getElementById("player"));
} else {
oDlDiv.innerHTML = "Download video from:";
oVideosList = document.createElement("select");
oVideosList.setAttribute("id", "mvyJsVideoDownloadList");
oVideosList.setAttribute("style", "display: none; font-size: 0.3cm!important; margin: 0.2cm 1cm 0.2cm 1em!important;");
oDlDiv.appendChild(oVideosList);
document.getElementById("player").parentNode.insertBefore(oDlDiv, document.getElementById("player"));
parseYTPlayer();
}
document.getElementById("player").setAttribute("style", document.getElementById("player").getAttribute("style") + "height: 90%!important; ");
document.getElementsByTagName("body")[0].style="background-color: white!important; line-height: 0.5cm;";
document.getElementById("player").parentNode.style="background-color: white!important; ";
}
}
@vsubhash
Copy link
Author

vsubhash commented May 17, 2016

On YouTube pages:
On YouTube pages, the video in different formats can be selected from a list

On other pages:
On other pages, a button to download the current video will be displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment