Advertisement
Guest User

Javascript to download YouTube videos - Moral Volcano

a guest
May 9th, 2015
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Download any YouTube video from anywhere
  3. // @namespace   com.vsubhash.js.youtube.download
  4. // @description Adds a download button to YouTube videos including those embedded elsewhere
  5. // @version     1
  6. // @grant       none
  7. // ==/UserScript==
  8.  
  9. /*
  10.  * Moral Volcano's YouTub DL JavaScript
  11.  * https://moralvolcano.wordpress.com/2015/05/09/how-to-download-any-youtube-video-using-just-javascript-no-need-for-any-special-add-on-or-extension-in-opera-12x-or-older/
  12.  * Licensed under GPL v3 as provided at
  13.  * https://www.gnu.org/licenses/gpl.html
  14.  *
  15.  */
  16.  
  17. document.addEventListener("DOMContentLoaded", addYouTubeDetectingButton(), false);
  18.  
  19. function addYouTubeDetectingButton() {
  20.   console.log("User JS called");
  21.   var i, n, oDlButtonEl, oDlDiv, oVidLink;
  22.  
  23.   if ((location.href.indexOf("youtube.com/embed") != -1) ||
  24.       (location.href.indexOf("youtube.com/watch") != -1)) {
  25.     console.log("Inside YouTube Iframe");
  26.    
  27.     oDlDiv = document.createElement("div");
  28.     oDlDiv.setAttribute("id", "mvJsDiv");
  29.     oDlDiv.setAttribute("style", "color: black; 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;  ");
  30.    
  31.     oDlButtonEl = document.createElement("input");
  32.     oDlDiv.setAttribute("id", "mvJsButton");    
  33.     oDlButtonEl.setAttribute("type", "button");
  34.     oDlButtonEl.setAttribute("value", "Grab Video");    
  35.    
  36.     oDlButtonEl.addEventListener("click", handle_DownloadButtonClick, false);  
  37.    
  38.     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;");
  39.     oDlDiv.appendChild(oDlButtonEl);
  40.    
  41.     oVidLink = document.createElement("a");
  42.     oVidLink.setAttribute("id", "mvJsLink");        
  43.     oVidLink.setAttribute("style", "color: darkblue!important; text-decoration: underline!important; font-size: 0.3cm!important; margin: 0.2cm 1cm 0.2cm 1cm!important;");
  44.     oVidLink.innerHTML="Video URL";
  45.     oVidLink.setAttribute("href","javascript:window.alert('First play the video a bit and pause. Then, we will get the video link.');");
  46.     oDlDiv.appendChild(oVidLink);
  47.    
  48.    
  49.     document.getElementById("player").setAttribute("style", document.getElementById("player").getAttribute("style") + "height: 90%!important; ");
  50.    
  51.     document.getElementById("player").parentNode.insertBefore(oDlDiv, document.getElementById("player"));
  52.    
  53.     document.getElementsByTagName("body")[0].style="background-color: white!important; line-height: 0.5cm;";
  54.     document.getElementById("player").parentNode.style="background-color: white!important; ";
  55.    
  56.    
  57.   }
  58. }
  59.  
  60. function handle_DownloadButtonClick() {
  61.   if (document.getElementsByTagName("video").length > 0) {
  62.    if (document.getElementsByTagName("video")[0].src.length > 10) {
  63.      document.getElementById("mvJsLink").setAttribute("href",document.getElementsByTagName("video")[0].src + "&title=" + document.title+".flv");
  64.      
  65.      document.getElementById("mvJsLink").innerHTML = document.getElementsByTagName("video")[0].src.substring(0,35) + "...";
  66.      
  67.      location.href=document.getElementsByTagName("video")[0].src + "&title=" + document.title+".flv";
  68.  
  69.    } else {
  70.      window.alert("First play the video a bit and pause. Then, we will get the video link.");
  71.    }
  72.   } else {
  73.     window.alert("No video has loaded yet.");
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement