Simple YouTube MP3/VIDEO Buttons

Add two download buttons to YouTube videos that allow you to download MP3 or Video without leaving the page with the option to choose the quality.

  1. // ==UserScript==
  2. // @name Simple YouTube MP3/VIDEO Buttons
  3. // @namespace https://youtubemp3api.com/
  4. // @version 0.8
  5. // @description Add two download buttons to YouTube videos that allow you to download MP3 or Video without leaving the page with the option to choose the quality.
  6. // @author Arari, dLehr, Rene (edited)
  7. // @include http*://*.youtube.com/*
  8. // @include http*://youtube.com/*
  9. // @include http*://*.youtu.be/*
  10. // @include http*://youtu.be/*
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. var lastId = ""
  15. function getYTId(url){
  16. var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  17. var match = url.match(regExp);
  18. return (match&&match[7].length==11)? match[7] : false;
  19. }
  20.  
  21. function audioMp3(){
  22.  
  23. /* Create button */
  24. var buttonDiv = document.createElement("div");
  25. buttonDiv.style.width = "100%";
  26. buttonDiv.id = "parentButton";
  27. var addButton = document.createElement("button");
  28. addButton.appendChild(document.createTextNode("Download MP3"));
  29. if(typeof(document.getElementById("iframeDownloadButton")) != 'undefined' && document.getElementById("iframeDownloadButton") !== null)
  30. {
  31.  
  32. document.getElementById("iframeDownloadButton").remove();
  33.  
  34. }
  35.  
  36. addButton.style.width = "100%";
  37. addButton.style.backgroundColor = "#181717";
  38. addButton.style.color = "white";
  39. addButton.style.textAlign = "center";
  40. addButton.style.padding = "10px";
  41. addButton.style.marginTop = "5px";
  42. addButton.style.fontSize = "14px";
  43. addButton.style.border = "0";
  44. addButton.style.cursor = "pointer";
  45. addButton.style.borderRadius = "2px";
  46. addButton.style.fontFamily = "Roboto, Arial, sans-serif";
  47.  
  48. addButton.onclick = function () {
  49. if(document.getElementById("iframeDownloadButton") !== null)
  50. {
  51.  
  52. document.getElementById("iframeDownloadButton").remove();
  53.  
  54. }
  55. /* Add large button on click */
  56. var ytId = getYTId(location.href);
  57. if(!ytId)return;
  58. var addIframe = document.createElement("iframe");
  59. addIframe.src = '//www.recordmp3.co/#/watch?v='+ytId + '&layout=button';
  60. addIframe.style.width = "300px";
  61. addIframe.style.height = "60px";
  62. addIframe.style.marginTop = "0px";
  63. addIframe.border = "0px";
  64. addIframe.overflow = "hidden";
  65. addIframe.id = "iframeDownloadButton";
  66. var targetElement = document.querySelectorAll("[id='meta']");
  67.  
  68. for(var i = 0; i < targetElement.length; i++){
  69.  
  70. if(targetElement[i].className.indexOf("ytd-watch") > -1)
  71. {
  72.  
  73. targetElement[i].insertBefore(addIframe, targetElement[i].childNodes[0]);
  74. }
  75. }
  76.  
  77. };
  78.  
  79. buttonDiv.appendChild(addButton);
  80.  
  81. /* Find and add to target */
  82. var targetElement = document.querySelectorAll("[id='subscribe-button']");
  83.  
  84. for(var i = 0; i < targetElement.length; i++){
  85.  
  86. if(targetElement[i].className.indexOf("ytd-video-secondary-info-renderer") > -1){
  87.  
  88. targetElement[i].appendChild(buttonDiv);
  89.  
  90. }
  91.  
  92. }
  93.  
  94. }
  95.  
  96.  
  97. function videoMp4(){
  98.  
  99. /* Create button */
  100. var buttonDiv = document.createElement("div");
  101. buttonDiv.style.width = "100%";
  102. buttonDiv.id = "parentButton";
  103. var addButton = document.createElement("button");
  104. addButton.appendChild(document.createTextNode("Download VIDEO"));
  105. if(typeof(document.getElementById("iframeDownloadButton")) != 'undefined' && document.getElementById("iframeDownloadButton") !== null){
  106.  
  107. document.getElementById("iframeDownloadButton").remove();
  108.  
  109. }
  110.  
  111. addButton.style.width = "100%";
  112. addButton.style.backgroundColor = "#181717";
  113. addButton.style.color = "white";
  114. addButton.style.textAlign = "center";
  115. addButton.style.padding = "10px";
  116. addButton.style.marginTop = "5px";
  117. addButton.style.fontSize = "14px";
  118. addButton.style.border = "0";
  119. addButton.style.cursor = "pointer";
  120. addButton.style.borderRadius = "2px";
  121. addButton.style.fontFamily = "Roboto, Arial, sans-serif";
  122.  
  123. addButton.onclick = function () {
  124.  
  125. if(document.getElementById("iframeDownloadButton") !== null)
  126. {
  127.  
  128. document.getElementById("iframeDownloadButton").remove();
  129. }
  130. /* Add large button on click */
  131. var ytId = getYTId(location.href);
  132. if(!ytId)return;
  133. var addIframe = document.createElement("iframe");
  134. addIframe.src = '//www.recordmp3.co/#/watch?v=' + ytId + '&layout=button&format=video&t_press_to_start=Click%20to%20Convert';
  135. addIframe.style.width = "300px";
  136. addIframe.style.height = "40px";
  137. addIframe.style.marginTop = "0px";
  138. addIframe.border = "0px";
  139. addIframe.id = "iframeDownloadButton";
  140. var targetElement = document.querySelectorAll("[id='meta']");
  141.  
  142. for(var i = 0; i < targetElement.length; i++){
  143.  
  144. if(targetElement[i].className.indexOf("ytd-watch") > -1){
  145.  
  146. targetElement[i].insertBefore(addIframe, targetElement[i].childNodes[0]);
  147.  
  148. }
  149. }
  150.  
  151. };
  152.  
  153. buttonDiv.appendChild(addButton);
  154.  
  155. /* Find and add to target */
  156. var targetElement = document.querySelectorAll("[id='subscribe-button']");
  157.  
  158. for(var i = 0; i < targetElement.length; i++){
  159.  
  160. if(targetElement[i].className.indexOf("ytd-video-secondary-info-renderer") > -1){
  161.  
  162. targetElement[i].appendChild(buttonDiv);
  163.  
  164. }
  165.  
  166. }
  167. }
  168.  
  169. if(document.getElementById("polymer-app") || document.getElementById("masthead") || window.Polymer){
  170.  
  171. setInterval(function(){
  172.  
  173. if(window.location.href.indexOf("watch?v=") < 0){
  174.  
  175. return false;
  176.  
  177. }
  178.  
  179. if(document.getElementById("count") && document.getElementById("parentButton") === null){
  180.  
  181. audioMp3(), videoMp4();
  182.  
  183. }
  184.  
  185. }, 100);
  186.  
  187. }
  188.  
  189. else{
  190.  
  191. standardInject();
  192.  
  193. }