Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Simple YouTube MP3 Button
  3. // @namespace https://www.youtubeinmp3.com
  4. // @version 1.2.2
  5. // @description Adds a download button to YouTube videos which allows you to download the MP3 of the video without having to leave the page
  6. // @author Arari
  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. function polymerInject(){
  15.  
  16. /* Create button */
  17. var buttonDiv = document.createElement("div");
  18. buttonDiv.style.width = "100%";
  19. buttonDiv.id = "parentButton";
  20.  
  21. var addButton = document.createElement("button");
  22. addButton.appendChild(document.createTextNode("Download MP3"));
  23.  
  24. if(typeof(document.getElementById("iframeDownloadButton")) != 'undefined' && document.getElementById("iframeDownloadButton") !== null){
  25.  
  26. document.getElementById("iframeDownloadButton").remove();
  27.  
  28. }
  29.  
  30. addButton.style.width = "100%";
  31. addButton.style.backgroundColor = "#181717";
  32. addButton.style.color = "white";
  33. addButton.style.textAlign = "center";
  34. addButton.style.padding = "10px 0";
  35. addButton.style.marginTop = "5px";
  36. addButton.style.fontSize = "14px";
  37. addButton.style.border = "0";
  38. addButton.style.cursor = "pointer";
  39. addButton.style.borderRadius = "2px";
  40. addButton.style.fontFamily = "Roboto, Arial, sans-serif";
  41.  
  42. addButton.onclick = function () {
  43.  
  44. this.remove();
  45.  
  46. /* Add large button on click */
  47. var addIframe = document.createElement("iframe");
  48. addIframe.src = '//www.convertmp3.io/widget/button/?color=ba1717&video=' + window.location.href;
  49.  
  50. addIframe.style.width = "100%";
  51. addIframe.style.border = "none";
  52. addIframe.style.height = "60px";
  53. addIframe.style.marginTop = "10px";
  54. addIframe.style.overflow = "hidden";
  55. addIframe.scrolling = "no";
  56. addIframe.id = "iframeDownloadButton";
  57.  
  58. var targetElement = document.querySelectorAll("[id='meta']");
  59.  
  60. for(var i = 0; i < targetElement.length; i++){
  61.  
  62. if(targetElement[i].className.indexOf("ytd-watch") > -1){
  63.  
  64. targetElement[i].insertBefore(addIframe, targetElement[i].childNodes[0]);
  65.  
  66. }
  67.  
  68. }
  69.  
  70. };
  71.  
  72. buttonDiv.appendChild(addButton);
  73.  
  74. /* Find and add to target */
  75. var targetElement = document.querySelectorAll("[id='subscribe-button']");
  76.  
  77. for(var i = 0; i < targetElement.length; i++){
  78.  
  79. if(targetElement[i].className.indexOf("ytd-video-secondary-info-renderer") > -1){
  80.  
  81. targetElement[i].appendChild(buttonDiv);
  82.  
  83. }
  84.  
  85. }
  86.  
  87. /* Fix hidden description bug */
  88. var descriptionBox = document.querySelectorAll("ytd-video-secondary-info-renderer");
  89. if(descriptionBox[0].className.indexOf("loading") > -1){
  90.  
  91. descriptionBox[0].classList.remove("loading");
  92.  
  93. }
  94.  
  95. }
  96.  
  97. function standardInject() {
  98. var pagecontainer=document.getElementById('page-container');
  99. if (!pagecontainer) return;
  100. if (/^https?:\/\/www\.youtube.com\/watch\?/.test(window.location.href)) run();
  101. var isAjax=/class[\w\s"'-=]+spf\-link/.test(pagecontainer.innerHTML);
  102. var logocontainer=document.getElementById('logo-container');
  103. if (logocontainer && !isAjax) { // fix for blocked videos
  104. isAjax=(' '+logocontainer.className+' ').indexOf(' spf-link ')>=0;
  105. }
  106. var content=document.getElementById('content');
  107. if (isAjax && content) { // Ajax UI
  108. var mo=window.MutationObserver||window.WebKitMutationObserver;
  109. if(typeof mo!=='undefined') {
  110. var observer=new mo(function(mutations) {
  111. mutations.forEach(function(mutation) {
  112. if(mutation.addedNodes!==null) {
  113. for (var i=0; i<mutation.addedNodes.length; i++) {
  114. if (mutation.addedNodes[i].id=='watch7-container' ||
  115. mutation.addedNodes[i].id=='watch7-main-container') { // old value: movie_player
  116. run();
  117. break;
  118. }
  119. }
  120. }
  121. });
  122. });
  123. observer.observe(content, {childList: true, subtree: true}); // old value: pagecontainer
  124. } else { // MutationObserver fallback for old browsers
  125. pagecontainer.addEventListener('DOMNodeInserted', onNodeInserted, false);
  126. }
  127. }
  128. }
  129.  
  130. function onNodeInserted(e) {
  131. if (e && e.target && (e.target.id=='watch7-container' ||
  132. e.target.id=='watch7-main-container')) { // old value: movie_player
  133. run();
  134. }
  135. }
  136.  
  137. function finalButton(){
  138.  
  139. var buttonIframeDownload = document.createElement("iframe");
  140. buttonIframeDownload.src = '//www.convertmp3.io/widget/button/?color=ba1717&amp;video=' + window.location.href;
  141. buttonIframeDownload.id = "buttonIframe";
  142.  
  143. buttonIframeDownload.style.width = "100%";
  144. buttonIframeDownload.style.height = "60px";
  145. buttonIframeDownload.style.paddingTop = "20px";
  146. buttonIframeDownload.style.paddingBottom = "20px";
  147. buttonIframeDownload.style.overflow = "hidden";
  148. buttonIframeDownload.scrolling = "no";
  149.  
  150. document.getElementById("watch-header").appendChild(buttonIframeDownload);
  151.  
  152. }
  153.  
  154. function run(){
  155.  
  156. if(!document.getElementById("parentButton") && window.location.href.substring(0, 25).indexOf("youtube.com") > -1 && window.location.href.indexOf("watch?") > -1){
  157.  
  158. var parentButton = document.createElement("div");
  159.  
  160. parentButton.className = "yt-uix-button yt-uix-button-default";
  161. parentButton.id = "parentButton";
  162.  
  163. parentButton.style.height = "23px";
  164. parentButton.style.marginLeft = "28px";
  165. parentButton.style.paddingBottom = "1px";
  166.  
  167. parentButton.onclick = function () {
  168.  
  169. this.remove();
  170. finalButton();
  171.  
  172. };
  173.  
  174. document.getElementById("watch7-user-header").appendChild(parentButton);
  175.  
  176. var childButton = document.createElement("span");
  177. childButton.appendChild(document.createTextNode("Download MP3"));
  178. childButton.className = "yt-uix-button-content";
  179.  
  180. childButton.style.lineHeight = "25px";
  181. childButton.style.fontSize = "12px";
  182.  
  183. parentButton.appendChild(childButton);
  184.  
  185. }
  186.  
  187. }
  188.  
  189. if(document.getElementById("polymer-app") || document.getElementById("masthead") || window.Polymer){
  190.  
  191. setInterval(function(){
  192.  
  193. if(window.location.href.indexOf("watch?v=") < 0){
  194.  
  195. return false;
  196.  
  197. }
  198.  
  199. if(document.getElementById("count") && document.getElementById("parentButton") === null){
  200.  
  201. polymerInject();
  202.  
  203.  
  204. }
  205.  
  206. }, 100);
  207.  
  208. }
  209.  
  210. else{
  211.  
  212. standardInject();
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement