levelKro / YouTube Direct Downloader

// ==UserScript==
// @name         YouTube Direct Downloader
// @namespace    https://levelkro.com
// @version      0.5.3
// @description  Download direct files of Youtube video (no recode).
// @author       levelKro (https://levelkro.com)
// @match        https://*.youtube.com/*
// @match        https://*.youtube.fr/*
// @match        https://*.youtube.ca/*
// @match        https://*.youtube.us/*
// @match        https://*.youtube.it/*
// @match        https://*.youtu.be/*
// @match        https://youtube.com/*
// @match        https://youtube.fr/*
// @match        https://youtube.ca/*
// @match        https://youtube.us/*
// @match        https://youtube.it/*
// @match        https://youtu.be/*
// @grant        none
// @noframes
// @run-at document-idle
// @license     MIT
// @copyright 2018, levelKro (https://levelkro.com) (https://openuserjs.org/users/levelKro)
// ==/UserScript==
console.log("YTDD : Script started, check YouTube URL each 500ms");
var v_url = "",
  v_type = "",
  v_title = "",
  output = "",
  url = "",
  put = !1;
setInterval(function () {
  if ("-1" != window.location.href.indexOf("/watch")) {
    var chk = document.getElementById("messages");
    if (chk)
      if ((put = document.getElementById("ytdownload")) || (console.log("YTDD : Installing code for rendering"), chk.innerHTML = '<div id="ytdownload" style="background:#040404; color:#fefefe; padding:10px; border-radius:5px; border:1px solid #242424; font-size:1.2em;"></div>', url = ""), put = document.getElementById("ytdownload"))
        if (url != window.location.href || "" == put.innerHTML) url = window.location.href, document.getElementById("ytdownload").innerHTML = "<i>Contacting Youtube API ...</i>", console.log("YTDD : New page detected, Watching now; " + url), v_url = "", v_type = "", output = "", ("YTDD&" + url.split("?")[1]).split("&").forEach(function (entry) {
          if ("-1" != entry.indexOf("v=")) {
            var v = entry.split("=")[1];
            if (console.log("YTDD : Found video ID; " + v), v) {
              console.log("YTDD : Call Youtube API for video details");
              var url_api = "//www.youtube.com/get_video_info?video_id=" + v + "&el=embedded&ps=default&eurl=&gl=US&hl=en";
              if (window.XMLHttpRequest)
                // code for IE7+, Firefox, Chrome, Opera, Safari
                var xmlhttp = new XMLHttpRequest;
              else
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              xmlhttp.onreadystatechange = function () {
                if (4 == xmlhttp.readyState && 200 == xmlhttp.status) {
                  console.log("YTDD : Youtube API answer");
                  for (var response = xmlhttp.responseText, request = {}, out = !1, pairs = response.split('&'), i = 0; i < pairs.length; i++)
                    if (pairs[i]) {
                      var pair = pairs[i].split('=');
                      request[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1])
                    }
                  "" != request.title && (v_title = decodeURIComponent(request.title).replace(/\+/g, " "));
                  var streams_array = request.url_encoded_fmt_stream_map.split(",");
                  output += '<h2>YouTube Direct Downloader</h2><h3>for <u style="color:#ff9900">' + v_title + '</u></h3><p>Right click and "save target as..." for save file. <u>Don\'t forget to rename filename</u> with the name of video.</p><ul>', streams_array.forEach(function (entry) {
                    entry.split("&").forEach(function (t) {
                      var s = t.split("=");
                      "type" == s[0] ? v_type = decodeURIComponent(s[1]) : "url" == s[0] && (v_url = decodeURIComponent(s[1]))
                    }), "" != v_type && (v_type = (v_type = (v_type = v_type.replace(';+codecs="', " (") + ")").replace('")', ")")).replace(',+', " / "), output += '<li><a href="' + v_url + '" target="_blank" style="font-size:1.2em; color:#0099ff;">Download</a> <i>' + v_type + '</i></li>', out = !0)
                  }), output += '</ul>', 0 == out && (output = '<h2>YouTube Direct Downloader</h2><h3>for <u style="color:#ff9900">' + v + '</u></h3><p>Sorry, no link available for this video.</p>', console.log("YTDD : Sorry, no video link found. This video is private, region locked or protected. Try with GenYouTube."))
                }
              }, xmlhttp.open("GET", url_api, !1), xmlhttp.send()
            }
            output += '<p>Need audio or video only ? Need more formats ? Script got error ? Try <a href="https://video.genyoutube.net/' + v + '" target="_blank" style="color:#0099ff;">GenYoutube</a> (no affiliation, many ads but work).</p>', output += '<p><b>DISCLAIMER</b> Do not use this script to download protected content without the permission of the author. Buy the product instead of downloading it, whenever possible. The use of this script is at your own risk and the author is not responsible for its use.</p>'
          }
          console.log("YTDD : Display links for the video (if available)"), document.getElementById("ytdownload").innerHTML = output
        })
  }
}, 500);