Youtube download button script


SUBMITTED BY: CHKDSK

DATE: March 3, 2016, 9:44 p.m.

UPDATED: March 4, 2016, 1:33 a.m.

FORMAT: Text only

SIZE: 37.4 kB

HITS: 90323

  1. For Tamper/Greasemonkey
  2. // ==UserScript==
  3. // @name Download YouTube Videos as MP4
  4. // @description Adds a button that lets you download YouTube videos.
  5. // @homepageURL https://github.com/gantt/downloadyoutube
  6. // @author Gantt
  7. // @version 1.8.6.1
  8. // @date 2016-01-18
  9. // @namespace http://googlesystem.blogspot.com
  10. // @include http://www.youtube.com/*
  11. // @include https://www.youtube.com/*
  12. // @exclude http://www.youtube.com/embed/*
  13. // @exclude https://www.youtube.com/embed/*
  14. // @match http://www.youtube.com/*
  15. // @match https://www.youtube.com/*
  16. // @match http://s.ytimg.com/yts/jsbin/html5player*
  17. // @match https://s.ytimg.com/yts/jsbin/html5player*
  18. // @match http://manifest.googlevideo.com/*
  19. // @match https://manifest.googlevideo.com/*
  20. // @match http://*.googlevideo.com/videoplayback*
  21. // @match https://*.googlevideo.com/videoplayback*
  22. // @match http://*.youtube.com/videoplayback*
  23. // @match https://*.youtube.com/videoplayback*
  24. // @connect-src googlevideo.com
  25. // @connect-src ytimg.com
  26. // @grant GM_xmlhttpRequest
  27. // @grant GM_getValue
  28. // @grant GM_setValue
  29. // @run-at document-end
  30. // @license MIT License
  31. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAB3RJTUUH2wMOCgIoGUYEAQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAARnQU1BAACxjwv8YQUAAAGSUExURfi/JO/v797e3sbGxq2traWlpZSUlJycnNbW1oyEhIRaWow5OZQhIZwYGKUQEKUICK0ICJQxMYxKSoxzc4x7e4RCQpQYGKUAAK0AALUAAL0AAK0QEIxra5QpKa0YGIxSUsYAAKUhIZR7e87Ozr0ICJRSUr29vYxjY6U5OaUpKa0hIb21tZwAALUICO/Ozu/GxqUxMZSEhLUYGO/W1r0YGKVCQpQQEL0pKffe3vfW1pxra5Q5OcZCQvfn585CQr2trZx7e8ZSUs5SUq05Oc5jY9ZjY84AAKWMjM5zc957e60pKdaMjOelpbWcnLWUlLVCQsYYGMYICNbOzpQICMYhIbV7e5xaWt6cnPfv79bGxt6lpe+9vc5KSs6lpb0xMc6EhM69vbUxMbUhIb1aWs61tcZaWuecnMYxMb1KSsZjY96UlNa1td7W1r17e9a9vZwQEN6trb1jY8YQENZra+fOzr1zc85aWufe3t6MjMY5OdZaWt61tdZ7e+/n5+e9vc6MjMZra+/e3ue1tdalpd7GxrUpKalL4aAAAAABdFJOUwBA5thmAAACxklEQVR42uXX/1/SQBgH8NuAoEQ2ijgbBivJLznBAiUUKiyJSgOVAk0tKZKw75mRRt/7v4MBY8ezjW39Vs8v8rqHz/u1jbvbidC/XL8KmcpOqVT6nSjXjooGw8WfFd+QWGfE4oLbtbr++PdMOy0BDYLjEj/0xevfWIyVAI7b/aIj/9WHsRrA8Yf9bqSexVgD4Lic9kWE/LgPwPGfNfJHDO4P8Iuq+S2M9QD8oUp+nxEAcFCtfgIA/14x/9ElAKDQbNQAwN9VAiYEABy0OgsAWAnB/AcBAtVWawkAfJ4CD0BQADZavYcQgI9h3CCQjpD5PcEgwG+SwLRhIL0vz78SjAPEU3hrHODfyX4I6rUJIP0G3oExoNwFXpoB+HwXmDEFpF9IwKA5YK+Tp9fMAdUOsC6YA553gKcmgdTfAhV1oMQqADndQDmJ0AZLAsFnCIV3VYDHJLAjDkZKciAaFz/lCeBJB1glgXBrNLndBWLJ9uZGAI+keTBLANL8SnWAzWRniAC2pG+6lQF0hfjTqCIBrEvjDwiggFSLuIUoLY0vEwAbUcsnc/LlnO02HGvEz+hXEeJ5Yj+4L2vNkxOJDSnlQzliIq2synr3embiUBjmw0FyU83KX04Ob+9aAK/Ppd5deZloz4HFlCHzt3sX0x2a6LcvQb4ab8r7i+DVdqvnCq/D5ZzqdhfAcr5B9wD0PNwPEu0ZnLwK9oPgNfCQJ2fhhhITJ3E8BjeUOXA+QNQlBh5xLjemVCgKjzgzNIJFjWF4yJoKhafgIWt6VHGmjgR0HvMuTipPdWQJ6AImbBRSE8aY/sC4er5xFx5vHyB4YRRpFWUf0AL4c+dHkHZRFo9TDeB9Aa3Llwjr8FlFwB+wO/rHm0VbPae9mPini/O5h/XGxatw2I6fGHAOuhiGZVxO98lTdgutP94yaIvVdqxZdpvFYTT9X9UfqQQlTXlm8wkAAAAASUVORK5CYII=
  32. // ==/UserScript==
  33. (function () {
  34. var FORMAT_LABEL={'5':'FLV 240p','18':'MP4 360p','22':'MP4 720p','34':'FLV 360p','35':'FLV 480p','37':'MP4 1080p','38':'MP4 2160p','43':'WebM 360p','44':'WebM 480p','45':'WebM 720p','46':'WebM 1080p','135':'MP4 480p - no audio','137':'MP4 1080p - no audio','138':'MP4 2160p - no audio','139':'M4A 48kbps - audio','140':'M4A 128kbps - audio','141':'M4A 256kbps - audio','264':'MP4 1440p - no audio','266':'MP4 2160p - no audio','298':'MP4 720p60 - no audio','299':'MP4 1080p60 - no audio'};
  35. var FORMAT_TYPE={'5':'flv','18':'mp4','22':'mp4','34':'flv','35':'flv','37':'mp4','38':'mp4','43':'webm','44':'webm','45':'webm','46':'webm','135':'mp4','137':'mp4','138':'mp4','139':'m4a','140':'m4a','141':'m4a','264':'mp4','266':'mp4','298':'mp4','299':'mp4'};
  36. var FORMAT_ORDER=['5','18','34','43','35','135','44','22','298','45','37','299','46','264','38','266','139','140','141'];
  37. var FORMAT_RULE={'flv':'max','mp4':'all','webm':'none','m4a':'max'};
  38. // all=display all versions, max=only highest quality version, none=no version
  39. // the default settings show all MP4 videos, the highest quality FLV and no WebM
  40. var SHOW_DASH_FORMATS=false;
  41. var BUTTON_TEXT={'ar':'تنزيل','cs':'Stáhnout','de':'Herunterladen','en':'Download','es':'Descargar','fr':'Télécharger','hi':'डाउनलोड','hu':'Letöltés','id':'Unduh','it':'Scarica','ja':'ダウンロード','ko':'내려받기','pl':'Pobierz','pt':'Baixar','ro':'Descărcați','ru':'Скачать','tr':'İndir','zh':'下载','zh-TW':'下載'};
  42. var BUTTON_TOOLTIP={'ar':'تنزيل هذا الفيديو','cs':'Stáhnout toto video','de':'Dieses Video herunterladen','en':'Download this video','es':'Descargar este vídeo','fr':'Télécharger cette vidéo','hi':'वीडियो डाउनलोड करें','hu':'Videó letöltése','id':'Unduh video ini','it':'Scarica questo video','ja':'このビデオをダウンロードする','ko':'이 비디오를 내려받기','pl':'Pobierz plik wideo','pt':'Baixar este vídeo','ro':'Descărcați acest videoclip','ru':'Скачать это видео','tr': 'Bu videoyu indir','zh':'下载此视频','zh-TW':'下載此影片'};
  43. var DECODE_RULE=[];
  44. var RANDOM=7489235179; // Math.floor(Math.random()*1234567890);
  45. var CONTAINER_ID='download-youtube-video'+RANDOM;
  46. var LISTITEM_ID='download-youtube-video-fmt'+RANDOM;
  47. var BUTTON_ID='download-youtube-video-button'+RANDOM;
  48. var DEBUG_ID='download-youtube-video-debug-info';
  49. var STORAGE_URL='download-youtube-script-url';
  50. var STORAGE_CODE='download-youtube-signature-code';
  51. var STORAGE_DASH='download-youtube-dash-enabled';
  52. var isDecodeRuleUpdated=false;
  53. start();
  54. function start() {
  55. var pagecontainer=document.getElementById('page-container');
  56. if (!pagecontainer) return;
  57. if (/^https?:\/\/www\.youtube.com\/watch\?/.test(window.location.href)) run();
  58. var isAjax=/class[\w\s"'-=]+spf\-link/.test(pagecontainer.innerHTML);
  59. var logocontainer=document.getElementById('logo-container');
  60. if (logocontainer && !isAjax) { // fix for blocked videos
  61. isAjax=(' '+logocontainer.className+' ').indexOf(' spf-link ')>=0;
  62. }
  63. var content=document.getElementById('content');
  64. if (isAjax && content) { // Ajax UI
  65. var mo=window.MutationObserver||window.WebKitMutationObserver;
  66. if(typeof mo!=='undefined') {
  67. var observer=new mo(function(mutations) {
  68. mutations.forEach(function(mutation) {
  69. if(mutation.addedNodes!==null) {
  70. for (var i=0; i<mutation.addedNodes.length; i++) {
  71. if (mutation.addedNodes[i].id=='watch7-container' ||
  72. mutation.addedNodes[i].id=='watch7-main-container') { // old value: movie_player
  73. run();
  74. break;
  75. }
  76. }
  77. }
  78. });
  79. });
  80. observer.observe(content, {childList: true, subtree: true}); // old value: pagecontainer
  81. } else { // MutationObserver fallback for old browsers
  82. pagecontainer.addEventListener('DOMNodeInserted', onNodeInserted, false);
  83. }
  84. }
  85. }
  86. function onNodeInserted(e) {
  87. if (e && e.target && (e.target.id=='watch7-container' ||
  88. e.target.id=='watch7-main-container')) { // old value: movie_player
  89. run();
  90. }
  91. }
  92. function run() {
  93. if (document.getElementById(CONTAINER_ID)) return; // check download container
  94. if (document.getElementById('p') && document.getElementById('vo')) return; // Feather not supported
  95. var videoID, videoFormats, videoAdaptFormats, videoManifestURL, scriptURL=null;
  96. var isSignatureUpdatingStarted=false;
  97. var operaTable=new Array();
  98. var language=document.documentElement.getAttribute('lang');
  99. var textDirection='left';
  100. if (document.body.getAttribute('dir')=='rtl') {
  101. textDirection='right';
  102. }
  103. if (document.getElementById('watch7-action-buttons')) { // old UI
  104. fixTranslations(language, textDirection);
  105. }
  106. // obtain video ID, formats map
  107. var args=null;
  108. var usw=(typeof this.unsafeWindow !== 'undefined')?this.unsafeWindow:window; // Firefox, Opera<15
  109. if (usw.ytplayer && usw.ytplayer.config && usw.ytplayer.config.args) {
  110. args=usw.ytplayer.config.args;
  111. }
  112. if (args) {
  113. videoID=args['video_id'];
  114. videoFormats=args['url_encoded_fmt_stream_map'];
  115. videoAdaptFormats=args['adaptive_fmts'];
  116. videoManifestURL=args['dashmpd'];
  117. debug('DYVAM - Info: Standard mode. videoID '+(videoID?videoID:'none')+'; ');
  118. }
  119. if (usw.ytplayer && usw.ytplayer.config && usw.ytplayer.config.assets) {
  120. scriptURL=usw.ytplayer.config.assets.js;
  121. }
  122. if (videoID==null) { // unsafeWindow workaround (Chrome, Opera 15+)
  123. var buffer=document.getElementById(DEBUG_ID+'2');
  124. if (buffer) {
  125. while (buffer.firstChild) {
  126. buffer.removeChild(buffer.firstChild);
  127. }
  128. } else {
  129. buffer=createHiddenElem('pre', DEBUG_ID+'2');
  130. }
  131. injectScript ('if(ytplayer&&ytplayer.config&&ytplayer.config.args){document.getElementById("'+DEBUG_ID+'2").appendChild(document.createTextNode(\'"video_id":"\'+ytplayer.config.args.video_id+\'", "js":"\'+ytplayer.config.assets.js+\'", "dashmpd":"\'+ytplayer.config.args.dashmpd+\'", "url_encoded_fmt_stream_map":"\'+ytplayer.config.args.url_encoded_fmt_stream_map+\'", "adaptive_fmts":"\'+ytplayer.config.args.adaptive_fmts+\'"\'));}');
  132. var code=buffer.innerHTML;
  133. if (code) {
  134. videoID=findMatch(code, /\"video_id\":\s*\"([^\"]+)\"/);
  135. videoFormats=findMatch(code, /\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/);
  136. videoFormats=videoFormats.replace(/&amp;/g,'\\u0026');
  137. videoAdaptFormats=findMatch(code, /\"adaptive_fmts\":\s*\"([^\"]+)\"/);
  138. videoAdaptFormats=videoAdaptFormats.replace(/&amp;/g,'\\u0026');
  139. videoManifestURL=findMatch(code, /\"dashmpd\":\s*\"([^\"]+)\"/);
  140. scriptURL=findMatch(code, /\"js\":\s*\"([^\"]+)\"/);
  141. }
  142. debug('DYVAM - Info: Injection mode. videoID '+(videoID?videoID:'none')+'; ');
  143. }
  144. if (videoID==null) { // if all else fails
  145. var bodyContent=document.body.innerHTML;
  146. if (bodyContent!=null) {
  147. videoID=findMatch(bodyContent, /\"video_id\":\s*\"([^\"]+)\"/);
  148. videoFormats=findMatch(bodyContent, /\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/);
  149. videoAdaptFormats=findMatch(bodyContent, /\"adaptive_fmts\":\s*\"([^\"]+)\"/);
  150. videoManifestURL=findMatch(bodyContent, /\"dashmpd\":\s*\"([^\"]+)\"/);
  151. if (scriptURL==null) {
  152. scriptURL=findMatch(bodyContent, /\"js\":\s*\"([^\"]+)\"/);
  153. if (scriptURL) {
  154. scriptURL=scriptURL.replace(/\\/g,'');
  155. }
  156. }
  157. }
  158. debug('DYVAM - Info: Brute mode. videoID '+(videoID?videoID:'none')+'; ');
  159. }
  160. debug('DYVAM - Info: url '+window.location.href+'; useragent '+window.navigator.userAgent);
  161. if (videoID==null || videoFormats==null || videoID.length==0 || videoFormats.length==0) {
  162. debug('DYVAM - Error: No config information found. YouTube must have changed the code.');
  163. return;
  164. }
  165. // Opera 12 extension message handler
  166. if (typeof window.opera !== 'undefined' && window.opera && typeof opera.extension !== 'undefined') {
  167. opera.extension.onmessage = function(event) {
  168. var index=findMatch(event.data.action, /xhr\-([0-9]+)\-response/);
  169. if (index && operaTable[parseInt(index,10)]) {
  170. index=parseInt(index,10);
  171. var trigger=(operaTable[index])['onload'];
  172. if (typeof trigger === 'function' && event.data.readyState == 4) {
  173. if (trigger) {
  174. trigger(event.data);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. if (!isDecodeRuleUpdated) {
  181. DECODE_RULE=getDecodeRules(DECODE_RULE);
  182. isDecodeRuleUpdated=true;
  183. }
  184. if (scriptURL) {
  185. if (scriptURL.indexOf('//')==0) {
  186. var protocol=(document.location.protocol=='http:')?'http:':'https:';
  187. scriptURL=protocol+scriptURL;
  188. }
  189. fetchSignatureScript(scriptURL);
  190. }
  191. // video title
  192. var videoTitle=document.title || 'video';
  193. videoTitle=videoTitle.replace(/\s*\-\s*YouTube$/i, '').replace(/'/g, '\'').replace(/^\s+|\s+$/g, '').replace(/\.+$/g, '');
  194. videoTitle=videoTitle.replace(/[:"\?\*]/g, '').replace(/[\|\\\/]/g, '_'); // Mac, Linux, Windows
  195. if (((window.navigator.userAgent || '').toLowerCase()).indexOf('windows') >= 0) {
  196. videoTitle=videoTitle.replace(/#/g, '').replace(/&/g, '_'); // Windows
  197. } else {
  198. videoTitle=videoTitle.replace(/#/g, '%23').replace(/&/g, '%26'); // Mac, Linux
  199. }
  200. // parse the formats map
  201. var sep1='%2C', sep2='%26', sep3='%3D';
  202. if (videoFormats.indexOf(',')>-1) {
  203. sep1=',';
  204. sep2=(videoFormats.indexOf('&')>-1)?'&':'\\u0026';
  205. sep3='=';
  206. }
  207. var videoURL=new Array();
  208. var videoSignature=new Array();
  209. if (videoAdaptFormats) {
  210. videoFormats=videoFormats+sep1+videoAdaptFormats;
  211. }
  212. var videoFormatsGroup=videoFormats.split(sep1);
  213. for (var i=0;i<videoFormatsGroup.length;i++) {
  214. var videoFormatsElem=videoFormatsGroup[i].split(sep2);
  215. var videoFormatsPair=new Array();
  216. for (var j=0;j<videoFormatsElem.length;j++) {
  217. var pair=videoFormatsElem[j].split(sep3);
  218. if (pair.length==2) {
  219. videoFormatsPair[pair[0]]=pair[1];
  220. }
  221. }
  222. if (videoFormatsPair['url']==null) continue;
  223. var url=unescape(unescape(videoFormatsPair['url'])).replace(/\\\//g,'/').replace(/\\u0026/g,'&');
  224. if (videoFormatsPair['itag']==null) continue;
  225. var itag=videoFormatsPair['itag'];
  226. var sig=videoFormatsPair['sig']||videoFormatsPair['signature'];
  227. if (sig) {
  228. url=url+'&signature='+sig;
  229. videoSignature[itag]=null;
  230. } else if (videoFormatsPair['s']) {
  231. url=url+'&signature='+decryptSignature(videoFormatsPair['s']);
  232. videoSignature[itag]=videoFormatsPair['s'];
  233. }
  234. if (url.toLowerCase().indexOf('ratebypass')==-1) { // speed up download for dash
  235. url=url+'&ratebypass=yes';
  236. }
  237. if (url.toLowerCase().indexOf('http')==0) { // validate URL
  238. videoURL[itag]=url+'&title='+videoTitle;
  239. }
  240. }
  241. var showFormat=new Array();
  242. for (var category in FORMAT_RULE) {
  243. var rule=FORMAT_RULE[category];
  244. for (var index in FORMAT_TYPE){
  245. if (FORMAT_TYPE[index]==category) {
  246. showFormat[index]=(rule=='all');
  247. }
  248. }
  249. if (rule=='max') {
  250. for (var i=FORMAT_ORDER.length-1;i>=0;i--) {
  251. var format=FORMAT_ORDER[i];
  252. if (FORMAT_TYPE[format]==category && videoURL[format]!=undefined) {
  253. showFormat[format]=true;
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. var dashPref=getPref(STORAGE_DASH);
  260. if (dashPref=='1') {
  261. SHOW_DASH_FORMATS=true;
  262. } else if (dashPref!='0') {
  263. setPref(STORAGE_DASH,'0');
  264. }
  265. var downloadCodeList=[];
  266. for (var i=0;i<FORMAT_ORDER.length;i++) {
  267. var format=FORMAT_ORDER[i];
  268. if (format=='37' && videoURL[format]==undefined) { // hack for dash 1080p
  269. if (videoURL['137']) {
  270. format='137';
  271. }
  272. showFormat[format]=showFormat['37'];
  273. } else if (format=='38' && videoURL[format]==undefined) { // hack for dash 4K
  274. if (videoURL['138'] && !videoURL['266']) {
  275. format='138';
  276. }
  277. showFormat[format]=showFormat['38'];
  278. }
  279. if (!SHOW_DASH_FORMATS && format.length>2) continue;
  280. if (videoURL[format]!=undefined && FORMAT_LABEL[format]!=undefined && showFormat[format]) {
  281. downloadCodeList.push({url:videoURL[format],sig:videoSignature[format],format:format,label:FORMAT_LABEL[format]});
  282. debug('DYVAM - Info: itag'+format+' url:'+videoURL[format]);
  283. }
  284. }
  285. if (downloadCodeList.length==0) {
  286. debug('DYVAM - Error: No download URL found. Probably YouTube uses encrypted streams.');
  287. return; // no format
  288. }
  289. // find parent container
  290. var newWatchPage=false;
  291. var parentElement=document.getElementById('watch7-action-buttons');
  292. if (parentElement==null) {
  293. parentElement=document.getElementById('watch8-secondary-actions');
  294. if (parentElement==null) {
  295. debug('DYVAM Error - No container for adding the download button. YouTube must have changed the code.');
  296. return;
  297. } else {
  298. newWatchPage=true;
  299. }
  300. }
  301. // get button labels
  302. var buttonText=(BUTTON_TEXT[language])?BUTTON_TEXT[language]:BUTTON_TEXT['en'];
  303. var buttonLabel=(BUTTON_TOOLTIP[language])?BUTTON_TOOLTIP[language]:BUTTON_TOOLTIP['en'];
  304. // generate download code for regular interface
  305. var mainSpan=document.createElement('span');
  306. if (newWatchPage) {
  307. var spanIcon=document.createElement('span');
  308. spanIcon.setAttribute('class', 'yt-uix-button-icon-wrapper');
  309. var imageIcon=document.createElement('img');
  310. imageIcon.setAttribute('src', '//s.ytimg.com/yt/img/pixel-vfl3z5WfW.gif');
  311. imageIcon.setAttribute('class', 'yt-uix-button-icon');
  312. imageIcon.setAttribute('style', 'width:20px;height:20px;background-size:20px 20px;background-repeat:no-repeat;background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABG0lEQVRYR+2W0Q3CMAxE2wkYAdiEEWADmIxuACMwCmzABpCTEmRSO7YTQX+ChECV43t2nF7GYeHPuLD+0AKwC/DnWMAp/N5qimkBuAfBdRTF/+2/AV6ZYFUxVYuicAfoHegd6B3oHfhZB+ByF+JyV8FkrAB74pqH3DU5L3iGoBURhdVODIQF4EjEkWLmmhYALOQgNIBcHHke4buhxXAAaFnaAhqbQ5QAOHHkwhZ8balkx1ICCiEBWNZ+CivdB7REHIC2ZjZK2oWklDDdB1NSdCd/Js2PqQMpSIKYVcM8kE6QCwDBNRCqOBJrW0CL8kCYxL0A1k6YxWsANAiXeC2ABOEWbwHAWrwxpzgkmA/JtIqnxTOElmPnjlkc4A3FykAhA42AxwAAAABJRU5ErkJggg==);');
  313. spanIcon.appendChild(imageIcon);
  314. mainSpan.appendChild(spanIcon);
  315. }
  316. var spanButton=document.createElement('span');
  317. spanButton.setAttribute('class', 'yt-uix-button-content');
  318. spanButton.appendChild(document.createTextNode(buttonText+' '));
  319. mainSpan.appendChild(spanButton);
  320. if (!newWatchPage) { // old UI
  321. var imgButton=document.createElement('img');
  322. imgButton.setAttribute('class', 'yt-uix-button-arrow');
  323. imgButton.setAttribute('src', '//s.ytimg.com/yt/img/pixel-vfl3z5WfW.gif');
  324. mainSpan.appendChild(imgButton);
  325. }
  326. var listItems=document.createElement('ol');
  327. listItems.setAttribute('style', 'display:none;');
  328. listItems.setAttribute('class', 'yt-uix-button-menu');
  329. for (var i=0;i<downloadCodeList.length;i++) {
  330. var listItem=document.createElement('li');
  331. var listLink=document.createElement('a');
  332. listLink.setAttribute('style', 'text-decoration:none;');
  333. listLink.setAttribute('href', downloadCodeList[i].url);
  334. listLink.setAttribute('download', videoTitle+'.'+FORMAT_TYPE[downloadCodeList[i].format]);
  335. var listButton=document.createElement('span');
  336. listButton.setAttribute('class', 'yt-uix-button-menu-item');
  337. listButton.setAttribute('loop', i+'');
  338. listButton.setAttribute('id', LISTITEM_ID+downloadCodeList[i].format);
  339. listButton.appendChild(document.createTextNode(downloadCodeList[i].label));
  340. listLink.appendChild(listButton);
  341. listItem.appendChild(listLink);
  342. listItems.appendChild(listItem);
  343. }
  344. mainSpan.appendChild(listItems);
  345. var buttonElement=document.createElement('button');
  346. buttonElement.setAttribute('id', BUTTON_ID);
  347. if (newWatchPage) {
  348. buttonElement.setAttribute('class', 'yt-uix-button yt-uix-button-size-default yt-uix-button-opacity yt-uix-tooltip');
  349. } else { // old UI
  350. buttonElement.setAttribute('class', 'yt-uix-button yt-uix-tooltip yt-uix-button-empty yt-uix-button-text');
  351. buttonElement.setAttribute('style', 'margin-top:4px; margin-left:'+((textDirection=='left')?5:10)+'px;');
  352. }
  353. buttonElement.setAttribute('data-tooltip-text', buttonLabel);
  354. buttonElement.setAttribute('type', 'button');
  355. buttonElement.setAttribute('role', 'button');
  356. buttonElement.addEventListener('click', function(){return false;}, false);
  357. buttonElement.appendChild(mainSpan);
  358. var containerSpan=document.createElement('span');
  359. containerSpan.setAttribute('id', CONTAINER_ID);
  360. containerSpan.appendChild(document.createTextNode(' '));
  361. containerSpan.appendChild(buttonElement);
  362. // add the button
  363. if (!newWatchPage) { // watch7
  364. parentElement.appendChild(containerSpan);
  365. } else { // watch8
  366. parentElement.insertBefore(containerSpan, parentElement.firstChild);
  367. }
  368. // REPLACEWITH if (!isSignatureUpdatingStarted) {
  369. for (var i=0;i<downloadCodeList.length;i++) {
  370. addFileSize(downloadCodeList[i].url, downloadCodeList[i].format);
  371. }
  372. // }
  373. if (typeof GM_download !== 'undefined') {
  374. for (var i=0;i<downloadCodeList.length;i++) {
  375. var downloadFMT=document.getElementById(LISTITEM_ID+downloadCodeList[i].format);
  376. var url=(downloadCodeList[i].url).toLowerCase();
  377. if (url.indexOf('clen=')>0 && url.indexOf('dur=')>0 && url.indexOf('gir=')>0
  378. && url.indexOf('lmt=')>0) {
  379. downloadFMT.addEventListener('click', downloadVideoNatively, false);
  380. }
  381. }
  382. }
  383. addFromManifest('140', '141'); // replace fmt140 with fmt141 if found in manifest
  384. function downloadVideoNatively(e) {
  385. var elem=e.currentTarget;
  386. e.returnValue=false;
  387. if (e.preventDefault) {
  388. e.preventDefault();
  389. }
  390. var loop=elem.getAttribute('loop');
  391. if (loop) {
  392. GM_download(downloadCodeList[loop].url, videoTitle+'.'+FORMAT_TYPE[downloadCodeList[loop].format]);
  393. }
  394. return false;
  395. }
  396. function addFromManifest(oldFormat, newFormat) { // find newFormat URL in manifest
  397. if (videoManifestURL && videoURL[newFormat]==undefined && SHOW_DASH_FORMATS && FORMAT_RULE['m4a']!='none') {
  398. var matchSig=findMatch(videoManifestURL, /\/s\/([a-zA-Z0-9\.]+)\//i);
  399. if (matchSig) {
  400. var decryptedSig=decryptSignature(matchSig);
  401. if (decryptedSig) {
  402. videoManifestURL=videoManifestURL.replace('/s/'+matchSig+'/','/signature/'+decryptedSig+'/');
  403. }
  404. }
  405. if (videoManifestURL.indexOf('//')==0) {
  406. var protocol=(document.location.protocol=='http:')?'http:':'https:';
  407. videoManifestURL=protocol+videoManifestURL;
  408. }
  409. debug('DYVAM - Info: manifestURL '+videoManifestURL);
  410. crossXmlHttpRequest({
  411. method:'GET',
  412. url:videoManifestURL, // check if URL exists
  413. onload:function(response) {
  414. if (response.readyState === 4 && response.status === 200 && response.responseText) {
  415. var regexp = new RegExp('<BaseURL.+>(http[^<]+itag='+newFormat+'[^<]+)<\\/BaseURL>','i');
  416. var matchURL=findMatch(response.responseText, regexp);
  417. debug('DYVAM - Info: matchURL '+matchURL);
  418. if (!matchURL) return;
  419. matchURL=matchURL.replace(/&amp\;/g,'&');
  420. if (FORMAT_RULE['m4a']=='max') {
  421. for (var i=0;i<downloadCodeList.length;i++) {
  422. if (downloadCodeList[i].format==oldFormat) {
  423. downloadCodeList[i].format==newFormat;
  424. var downloadFMT=document.getElementById(LISTITEM_ID+oldFormat);
  425. downloadFMT.setAttribute('id', LISTITEM_ID+newFormat);
  426. downloadFMT.parentNode.setAttribute('href', matchURL);
  427. downloadCodeList[i].url=matchURL;
  428. downloadFMT.firstChild.nodeValue=FORMAT_LABEL[newFormat];
  429. addFileSize(matchURL, newFormat);
  430. }
  431. }
  432. } else if (FORMAT_RULE['m4a']=='all') {
  433. downloadCodeList.push(
  434. {url:matchURL,sig:videoSignature[newFormat],format:newFormat,label:FORMAT_LABEL[newFormat]});
  435. var downloadFMT=document.getElementById(LISTITEM_ID+oldFormat);
  436. var clone=downloadFMT.parentNode.parentNode.cloneNode(true);
  437. clone.firstChild.firstChild.setAttribute('id', LISTITEM_ID+newFormat);
  438. clone.firstChild.setAttribute('href', matchURL);
  439. downloadFMT.parentNode.parentNode.parentNode.appendChild(clone);
  440. downloadFMT=document.getElementById(LISTITEM_ID+newFormat);
  441. downloadFMT.firstChild.nodeValue=FORMAT_LABEL[newFormat];
  442. addFileSize(matchURL, newFormat);
  443. }
  444. }
  445. }
  446. });
  447. }
  448. }
  449. function injectStyle(code) {
  450. var style=document.createElement('style');
  451. style.type='text/css';
  452. style.appendChild(document.createTextNode(code));
  453. document.getElementsByTagName('head')[0].appendChild(style);
  454. }
  455. function injectScript(code) {
  456. var script=document.createElement('script');
  457. script.type='application/javascript';
  458. script.textContent=code;
  459. document.body.appendChild(script);
  460. document.body.removeChild(script);
  461. }
  462. function debug(str) {
  463. var debugElem=document.getElementById(DEBUG_ID);
  464. if (!debugElem) {
  465. debugElem=createHiddenElem('div', DEBUG_ID);
  466. }
  467. debugElem.appendChild(document.createTextNode(str+' '));
  468. }
  469. function createHiddenElem(tag, id) {
  470. var elem=document.createElement(tag);
  471. elem.setAttribute('id', id);
  472. elem.setAttribute('style', 'display:none;');
  473. document.body.appendChild(elem);
  474. return elem;
  475. }
  476. function fixTranslations(language, textDirection) {
  477. if (/^af|bg|bn|ca|cs|de|el|es|et|eu|fa|fi|fil|fr|gl|hi|hr|hu|id|it|iw|kn|lv|lt|ml|mr|ms|nl|pl|ro|ru|sl|sk|sr|sw|ta|te|th|uk|ur|vi|zu$/.test(language)) { // fix international UI
  478. var likeButton=document.getElementById('watch-like');
  479. if (likeButton) {
  480. var spanElements=likeButton.getElementsByClassName('yt-uix-button-content');
  481. if (spanElements) {
  482. spanElements[0].style.display='none'; // hide like text
  483. }
  484. }
  485. var marginPixels=10;
  486. if (/^bg|ca|cs|el|eu|hr|it|ml|ms|pl|sl|sw|te$/.test(language)) {
  487. marginPixels=1;
  488. }
  489. injectStyle('#watch7-secondary-actions .yt-uix-button{margin-'+textDirection+':'+marginPixels+'px!important}');
  490. }
  491. }
  492. function findMatch(text, regexp) {
  493. var matches=text.match(regexp);
  494. return (matches)?matches[1]:null;
  495. }
  496. function isString(s) {
  497. return (typeof s==='string' || s instanceof String);
  498. }
  499. function isInteger(n) {
  500. return (typeof n==='number' && n%1==0);
  501. }
  502. function getPref(name) { // cross-browser GM_getValue
  503. var a='', b='';
  504. try {a=typeof GM_getValue.toString; b=GM_getValue.toString()} catch(e){}
  505. if (typeof GM_getValue === 'function' &&
  506. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  507. return GM_getValue(name, null); // Greasemonkey, Tampermonkey, Firefox extension
  508. } else {
  509. var ls=null;
  510. try {ls=window.localStorage||null} catch(e){}
  511. if (ls) {
  512. return ls.getItem(name); // Chrome script, Opera extensions
  513. }
  514. }
  515. return;
  516. }
  517. function setPref(name, value) { // cross-browser GM_setValue
  518. var a='', b='';
  519. try {a=typeof GM_setValue.toString; b=GM_setValue.toString()} catch(e){}
  520. if (typeof GM_setValue === 'function' &&
  521. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  522. GM_setValue(name, value); // Greasemonkey, Tampermonkey, Firefox extension
  523. } else {
  524. var ls=null;
  525. try {ls=window.localStorage||null} catch(e){}
  526. if (ls) {
  527. return ls.setItem(name, value); // Chrome script, Opera extensions
  528. }
  529. }
  530. }
  531. function crossXmlHttpRequest(details) { // cross-browser GM_xmlhttpRequest
  532. if (typeof GM_xmlhttpRequest === 'function') { // Greasemonkey, Tampermonkey, Firefox extension, Chrome script
  533. GM_xmlhttpRequest(details);
  534. } else if (typeof window.opera !== 'undefined' && window.opera && typeof opera.extension !== 'undefined' &&
  535. typeof opera.extension.postMessage !== 'undefined') { // Opera 12 extension
  536. var index=operaTable.length;
  537. opera.extension.postMessage({'action':'xhr-'+index, 'url':details.url, 'method':details.method});
  538. operaTable[index]=details;
  539. } else if (typeof window.opera === 'undefined' && typeof XMLHttpRequest === 'function') { // Opera 15+ extension
  540. var xhr=new XMLHttpRequest();
  541. xhr.onreadystatechange = function() {
  542. if (xhr.readyState == 4) {
  543. if (details['onload']) {
  544. details['onload'](xhr);
  545. }
  546. }
  547. }
  548. xhr.open(details.method, details.url, true);
  549. xhr.send();
  550. }
  551. }
  552. function addFileSize(url, format) {
  553. function updateVideoLabel(size, format) {
  554. var elem=document.getElementById(LISTITEM_ID+format);
  555. if (elem) {
  556. size=parseInt(size,10);
  557. if (size>=1073741824) {
  558. size=parseFloat((size/1073741824).toFixed(1))+' GB';
  559. } else if (size>=1048576) {
  560. size=parseFloat((size/1048576).toFixed(1))+' MB';
  561. } else {
  562. size=parseFloat((size/1024).toFixed(1))+' KB';
  563. }
  564. if (elem.childNodes.length>1) {
  565. elem.lastChild.nodeValue=' ('+size+')';
  566. } else if (elem.childNodes.length==1) {
  567. elem.appendChild(document.createTextNode(' ('+size+')'));
  568. }
  569. }
  570. }
  571. var matchSize=findMatch(url, /[&\?]clen=([0-9]+)&/i);
  572. if (matchSize) {
  573. updateVideoLabel(matchSize, format);
  574. } else {
  575. try {
  576. crossXmlHttpRequest({
  577. method:'HEAD',
  578. url:url,
  579. onload:function(response) {
  580. if (response.readyState == 4 && response.status == 200) { // add size
  581. var size=0;
  582. if (typeof response.getResponseHeader === 'function') {
  583. size=response.getResponseHeader('Content-length');
  584. } else if (response.responseHeaders) {
  585. var regexp = new RegExp('^Content\-length: (.*)$','im');
  586. var match = regexp.exec(response.responseHeaders);
  587. if (match) {
  588. size=match[1];
  589. }
  590. }
  591. if (size) {
  592. updateVideoLabel(size, format);
  593. }
  594. }
  595. }
  596. });
  597. } catch(e) { }
  598. }
  599. }
  600. function findSignatureCode(sourceCode) {
  601. debug('DYVAM - Info: signature start '+getPref(STORAGE_CODE));
  602. var signatureFunctionName =
  603. findMatch(sourceCode,
  604. /\.set\s*\("signature"\s*,\s*([a-zA-Z0-9_$][\w$]*)\(/)
  605. || findMatch(sourceCode,
  606. /\.sig\s*\|\|\s*([a-zA-Z0-9_$][\w$]*)\(/)
  607. || findMatch(sourceCode,
  608. /\.signature\s*=\s*([a-zA-Z_$][\w$]*)\([a-zA-Z_$][\w$]*\)/); //old
  609. if (signatureFunctionName == null) return setPref(STORAGE_CODE, 'error');
  610. signatureFunctionName=signatureFunctionName.replace('$','\\$');
  611. var regCode = new RegExp(signatureFunctionName + '\\s*=\\s*function' +
  612. '\\s*\\([\\w$]*\\)\\s*{[\\w$]*=[\\w$]*\\.split\\(""\\);\n*(.+);return [\\w$]*\\.join');
  613. var regCode2 = new RegExp('function \\s*' + signatureFunctionName +
  614. '\\s*\\([\\w$]*\\)\\s*{[\\w$]*=[\\w$]*\\.split\\(""\\);\n*(.+);return [\\w$]*\\.join');
  615. var functionCode = findMatch(sourceCode, regCode) || findMatch(sourceCode, regCode2);
  616. debug('DYVAM - Info: signaturefunction ' + signatureFunctionName + ' -- ' + functionCode);
  617. if (functionCode == null) return setPref(STORAGE_CODE, 'error');
  618. var reverseFunctionName = findMatch(sourceCode,
  619. /([\w$]*)\s*:\s*function\s*\(\s*[\w$]*\s*\)\s*{\s*(?:return\s*)?[\w$]*\.reverse\s*\(\s*\)\s*}/);
  620. debug('DYVAM - Info: reversefunction ' + reverseFunctionName);
  621. if (reverseFunctionName) reverseFunctionName=reverseFunctionName.replace('$','\\$');
  622. var sliceFunctionName = findMatch(sourceCode,
  623. /([\w$]*)\s*:\s*function\s*\(\s*[\w$]*\s*,\s*[\w$]*\s*\)\s*{\s*(?:return\s*)?[\w$]*\.(?:slice|splice)\(.+\)\s*}/);
  624. debug('DYVAM - Info: slicefunction ' + sliceFunctionName);
  625. if (sliceFunctionName) sliceFunctionName=sliceFunctionName.replace('$','\\$');
  626. var regSlice = new RegExp('\\.(?:'+'slice'+(sliceFunctionName?'|'+sliceFunctionName:'')+
  627. ')\\s*\\(\\s*(?:[a-zA-Z_$][\\w$]*\\s*,)?\\s*([0-9]+)\\s*\\)'); // .slice(5) sau .Hf(a,5)
  628. var regReverse = new RegExp('\\.(?:'+'reverse'+(reverseFunctionName?'|'+reverseFunctionName:'')+
  629. ')\\s*\\([^\\)]*\\)'); // .reverse() sau .Gf(a,45)
  630. var regSwap = new RegExp('[\\w$]+\\s*\\(\\s*[\\w$]+\\s*,\\s*([0-9]+)\\s*\\)');
  631. var regInline = new RegExp('[\\w$]+\\[0\\]\\s*=\\s*[\\w$]+\\[([0-9]+)\\s*%\\s*[\\w$]+\\.length\\]');
  632. var functionCodePieces=functionCode.split(';');
  633. var decodeArray=[];
  634. for (var i=0; i<functionCodePieces.length; i++) {
  635. functionCodePieces[i]=functionCodePieces[i].trim();
  636. var codeLine=functionCodePieces[i];
  637. if (codeLine.length>0) {
  638. var arrSlice=codeLine.match(regSlice);
  639. var arrReverse=codeLine.match(regReverse);
  640. debug(i+': '+codeLine+' --'+(arrSlice?' slice length '+arrSlice.length:'') +' '+(arrReverse?'reverse':''));
  641. if (arrSlice && arrSlice.length >= 2) { // slice
  642. var slice=parseInt(arrSlice[1], 10);
  643. if (isInteger(slice)){
  644. decodeArray.push(-slice);
  645. } else return setPref(STORAGE_CODE, 'error');
  646. } else if (arrReverse && arrReverse.length >= 1) { // reverse
  647. decodeArray.push(0);
  648. } else if (codeLine.indexOf('[0]') >= 0) { // inline swap
  649. if (i+2<functionCodePieces.length &&
  650. functionCodePieces[i+1].indexOf('.length') >= 0 &&
  651. functionCodePieces[i+1].indexOf('[0]') >= 0) {
  652. var inline=findMatch(functionCodePieces[i+1], regInline);
  653. inline=parseInt(inline, 10);
  654. decodeArray.push(inline);
  655. i+=2;
  656. } else return setPref(STORAGE_CODE, 'error');
  657. } else if (codeLine.indexOf(',') >= 0) { // swap
  658. var swap=findMatch(codeLine, regSwap);
  659. swap=parseInt(swap, 10);
  660. if (isInteger(swap) && swap>0){
  661. decodeArray.push(swap);
  662. } else return setPref(STORAGE_CODE, 'error');
  663. } else return setPref(STORAGE_CODE, 'error');
  664. }
  665. }
  666. if (decodeArray) {
  667. setPref(STORAGE_URL, scriptURL);
  668. setPref(STORAGE_CODE, decodeArray.toString());
  669. DECODE_RULE=decodeArray;
  670. debug('DYVAM - Info: signature '+decodeArray.toString()+' '+scriptURL);
  671. // update download links and add file sizes
  672. for (var i=0;i<downloadCodeList.length;i++) {
  673. var elem=document.getElementById(LISTITEM_ID+downloadCodeList[i].format);
  674. var url=downloadCodeList[i].url;
  675. var sig=downloadCodeList[i].sig;
  676. if (elem && url && sig) {
  677. url=url.replace(/\&signature=[\w\.]+/, '&signature='+decryptSignature(sig));
  678. elem.parentNode.setAttribute('href', url);
  679. addFileSize(url, downloadCodeList[i].format);
  680. }
  681. }
  682. }
  683. }
  684. function isValidSignatureCode(arr) { // valid values: '5,-3,0,2,5', 'error'
  685. if (!arr) return false;
  686. if (arr=='error') return true;
  687. arr=arr.split(',');
  688. for (var i=0;i<arr.length;i++) {
  689. if (!isInteger(parseInt(arr[i],10))) return false;
  690. }
  691. return true;
  692. }
  693. function fetchSignatureScript(scriptURL) {
  694. var storageURL=getPref(STORAGE_URL);
  695. var storageCode=getPref(STORAGE_CODE);
  696. if (!(/,0,|^0,|,0$|\-/.test(storageCode))) storageCode=null; // hack for only positive items
  697. if (storageCode && isValidSignatureCode(storageCode) && storageURL &&
  698. scriptURL.replace(/^https?/i,'')==storageURL.replace(/^https?/i,'')) return;
  699. try {
  700. debug('DYVAM fetch '+scriptURL);
  701. isSignatureUpdatingStarted=true;
  702. crossXmlHttpRequest({
  703. method:'GET',
  704. url:scriptURL,
  705. onload:function(response) {
  706. debug('DYVAM fetch status '+response.status);
  707. if (response.readyState === 4 && response.status === 200 && response.responseText) {
  708. findSignatureCode(response.responseText);
  709. }
  710. }
  711. });
  712. } catch(e) { }
  713. }
  714. function getDecodeRules(rules) {
  715. var storageCode=getPref(STORAGE_CODE);
  716. if (storageCode && storageCode!='error' && isValidSignatureCode(storageCode)) {
  717. var arr=storageCode.split(',');
  718. for (var i=0; i<arr.length; i++) {
  719. arr[i]=parseInt(arr[i], 10);
  720. }
  721. rules=arr;
  722. debug('DYVAM - Info: signature '+arr.toString()+' '+scriptURL);
  723. }
  724. return rules;
  725. }
  726. function decryptSignature(sig) {
  727. function swap(a,b){var c=a[0];a[0]=a[b%a.length];a[b]=c;return a};
  728. function decode(sig, arr) { // encoded decryption
  729. if (!isString(sig)) return null;
  730. var sigA=sig.split('');
  731. for (var i=0;i<arr.length;i++) {
  732. var act=arr[i];
  733. if (!isInteger(act)) return null;
  734. sigA=(act>0)?swap(sigA, act):((act==0)?sigA.reverse():sigA.slice(-act));
  735. }
  736. var result=sigA.join('');
  737. return result;
  738. }
  739. if (sig==null) return '';
  740. var arr=DECODE_RULE;
  741. if (arr) {
  742. var sig2=decode(sig, arr);
  743. if (sig2) return sig2;
  744. } else {
  745. setPref(STORAGE_URL, '');
  746. setPref(STORAGE_CODE, '');
  747. }
  748. return sig;
  749. }
  750. }
  751. })();

comments powered by Disqus