Last active
August 13, 2018 14:21
-
-
Save lyzhnikspb/ebe8f02bbe0e8867b45fc35272e76d06 to your computer and use it in GitHub Desktop.
Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Download videos from YouTube | |
// @description Download videos from YouTube | |
// @author Makar Lyzhnik | |
// @version 0.1 | |
// @date 2018-08-13 | |
// @namespace http://googlesystem.blogspot.com | |
// @include https://www.youtube.com/* | |
// @match https://www.youtube.com/* | |
// @match http://s.ytimg.com/yts/jsbin/* | |
// @connect googlevideo.com | |
// @connect ytimg.com | |
// @grant GM_xmlhttpRequest | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @run-at document-end | |
// @license MIT License | |
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAB3RJTUUH2wMOCgIoGUYEAQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAARnQU1BAACxjwv8YQUAAAGSUExURfi/JO/v797e3sbGxq2traWlpZSUlJycnNbW1oyEhIRaWow5OZQhIZwYGKUQEKUICK0ICJQxMYxKSoxzc4x7e4RCQpQYGKUAAK0AALUAAL0AAK0QEIxra5QpKa0YGIxSUsYAAKUhIZR7e87Ozr0ICJRSUr29vYxjY6U5OaUpKa0hIb21tZwAALUICO/Ozu/GxqUxMZSEhLUYGO/W1r0YGKVCQpQQEL0pKffe3vfW1pxra5Q5OcZCQvfn585CQr2trZx7e8ZSUs5SUq05Oc5jY9ZjY84AAKWMjM5zc957e60pKdaMjOelpbWcnLWUlLVCQsYYGMYICNbOzpQICMYhIbV7e5xaWt6cnPfv79bGxt6lpe+9vc5KSs6lpb0xMc6EhM69vbUxMbUhIb1aWs61tcZaWuecnMYxMb1KSsZjY96UlNa1td7W1r17e9a9vZwQEN6trb1jY8YQENZra+fOzr1zc85aWufe3t6MjMY5OdZaWt61tdZ7e+/n5+e9vc6MjMZra+/e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'MP4 720p','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','140':'M4A 128kbps - audio','264':'MP4 1440p - no audio','266':'MP4 2160p - no audio','298':'MP4 720p60 - no audio','299':'MP4 1080p60 - no audio'}; | |
var FORMAT_TYPE={'18':'mp4','22':'mp4','43':'webm','44':'webm','45':'webm','46':'webm','135':'mp4','137':'mp4','138':'mp4','140':'m4a','264':'mp4','266':'mp4','298':'mp4','299':'mp4'}; | |
var FORMAT_ORDER=['18','43','135','44','22','298','45','137','299','46','264','138','266','140']; | |
var FORMAT_RULE={'mp4':'all','webm':'none','m4a':'all'}; | |
var SHOW_DASH_FORMATS=false; | |
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':'下載'}; | |
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':'下載此影片'}; | |
var DECODE_RULE=[]; | |
var RANDOM=7489235179; | |
var CONTAINER_ID='download-youtube-video'+RANDOM; | |
var LISTITEM_ID='download-youtube-video-fmt'+RANDOM; | |
var BUTTON_ID='download-youtube-video-button'+RANDOM; | |
var DEBUG_ID='download-youtube-video-debug-info'; | |
var STORAGE_URL='download-youtube-script-url'; | |
var STORAGE_CODE='download-youtube-signature-code'; | |
var STORAGE_DASH='download-youtube-dash-enabled'; | |
var isDecodeRuleUpdated=false; | |
start(); | |
function start() { | |
var pagecontainer=document.getElementById('page-container'); | |
if (!pagecontainer) return; | |
if (/^https?:\/\/www\.youtube.com\/watch\?/.test(window.location.href)) run(); | |
var isAjax=/class[\w\s"'-=]+spf\-link/.test(pagecontainer.innerHTML); | |
var logocontainer=document.getElementById('logo-container'); | |
if (logocontainer && !isAjax) { | |
isAjax=(' '+logocontainer.className+' ').indexOf(' spf-link ')>=0; | |
} | |
var content=document.getElementById('content'); | |
if (isAjax && content) { // Ajax UI | |
var mo=window.MutationObserver||window.WebKitMutationObserver; | |
if(typeof mo!=='undefined') { | |
var observer=new mo(function(mutations) { | |
mutations.forEach(function(mutation) { | |
if(mutation.addedNodes!==null) { | |
for (var i=0; i<mutation.addedNodes.length; i++) { | |
if (mutation.addedNodes[i].id=='watch7-main-container') { | |
run(); | |
break; | |
} | |
} | |
} | |
}); | |
}); | |
observer.observe(content, {childList: true, subtree: true}); | |
} else { | |
pagecontainer.addEventListener('DOMNodeInserted', onNodeInserted, false); | |
} | |
} | |
} | |
function onNodeInserted(e) { | |
if (e && e.target && (e.target.id=='watch7-main-container')) { | |
run(); | |
} | |
} | |
function run() { | |
if (document.getElementById(CONTAINER_ID)) return; | |
var videoID, videoFormats, videoAdaptFormats, videoManifestURL, scriptURL=null; | |
var isSignatureUpdatingStarted=false; | |
var operaTable=new Array(); | |
var language=document.documentElement.getAttribute('lang'); | |
var textDirection='left'; | |
if (document.body.getAttribute('dir')=='rtl') { | |
textDirection='right'; | |
} | |
if (document.getElementById('watch7-action-buttons')) { | |
fixTranslations(language, textDirection); | |
} | |
var args=null; | |
var usw=(typeof this.unsafeWindow !== 'undefined')?this.unsafeWindow:window; | |
if (usw.ytplayer && usw.ytplayer.config && usw.ytplayer.config.args) { | |
args=usw.ytplayer.config.args; | |
} | |
if (args) { | |
videoID=args['video_id']; | |
videoFormats=args['url_encoded_fmt_stream_map']; | |
videoAdaptFormats=args['adaptive_fmts']; | |
videoManifestURL=args['dashmpd']; | |
debug('DYVAM - Info: Standard mode. videoID '+(videoID?videoID:'none')+'; '); | |
} | |
if (usw.ytplayer && usw.ytplayer.config && usw.ytplayer.config.assets) { | |
scriptURL=usw.ytplayer.config.assets.js; | |
} | |
if (videoID==null) { | |
var buffer=document.getElementById(DEBUG_ID+'2'); | |
if (buffer) { | |
while (buffer.firstChild) { | |
buffer.removeChild(buffer.firstChild); | |
} | |
} else { | |
buffer=createHiddenElem('pre', DEBUG_ID+'2'); | |
} | |
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+\'"\'));}'); | |
var code=buffer.innerHTML; | |
if (code) { | |
videoID=findMatch(code, /\"video_id\":\s*\"([^\"]+)\"/); | |
videoFormats=findMatch(code, /\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/); | |
videoFormats=videoFormats.replace(/&/g,'\\u0026'); | |
videoAdaptFormats=findMatch(code, /\"adaptive_fmts\":\s*\"([^\"]+)\"/); | |
videoAdaptFormats=videoAdaptFormats.replace(/&/g,'\\u0026'); | |
videoManifestURL=findMatch(code, /\"dashmpd\":\s*\"([^\"]+)\"/); | |
scriptURL=findMatch(code, /\"js\":\s*\"([^\"]+)\"/); | |
} | |
debug('DYVAM - Info: Injection mode. videoID '+(videoID?videoID:'none')+'; '); | |
} | |
if (videoID==null) { // if all else fails | |
var bodyContent=document.body.innerHTML; | |
if (bodyContent!=null) { | |
videoID=findMatch(bodyContent, /\"video_id\":\s*\"([^\"]+)\"/); | |
videoFormats=findMatch(bodyContent, /\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/); | |
videoAdaptFormats=findMatch(bodyContent, /\"adaptive_fmts\":\s*\"([^\"]+)\"/); | |
videoManifestURL=findMatch(bodyContent, /\"dashmpd\":\s*\"([^\"]+)\"/); | |
if (scriptURL==null) { | |
scriptURL=findMatch(bodyContent, /\"js\":\s*\"([^\"]+)\"/); | |
if (scriptURL) { | |
scriptURL=scriptURL.replace(/\\/g,''); | |
} | |
} | |
} | |
debug('DYVAM - Info: Brute mode. videoID '+(videoID?videoID:'none')+'; '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment