forked from motasay/YouTube-Download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarklet.js
1 lines (1 loc) · 6.18 KB
/
bookmarklet.js
1
javascript:function%20getURLMap(bodyHTML){var%20urlMap=null;var%20urlMapStartIndex=bodyHTML.indexOf('"url_encoded_fmt_stream_map"');if(urlMapStartIndex!=-1){urlMap=bodyHTML.substring(urlMapStartIndex);var%20urlMapEndIndex=urlMap.indexOf('",%20');if(urlMapEndIndex!=-1){urlMap=urlMap.substring(0,urlMapEndIndex);}}if(urlMap==null)throw%20'Error:%20Couldn\'t%20find%20url%20map.';urlMap=urlMap.replace(/\\u0026/g,'&');return%20urlMap;}function%20getCleanURL(url){var%20mainParams={};var%20splits=url.split("&");for(var%20i=0;i<splits.length;i++){var%20keyVal=getKeyAndVal(splits[i]);if(keyVal)mainParams[keyVal[0]]=keyVal[1];}var%20cleanURL=null;if('url'%20in%20mainParams)cleanURL=unescape(mainParams['url']);else{var%20regexes=['url=(http.+?videoplayback.+?id=.+?)(\u0026|&)quality=','(http.+?videoplayback.+?id=.+?)(\u0026|&)'];for(var%20i=0;i<regexes.length;i++){var%20match=regexes[i].exec(url);if(match!=null){cleanURL=unescape(match[1]);break;}}}if(cleanURL){if(cleanURL.indexOf('signature=')==-1&&cleanURL.indexOf('sig=')==%20-1){var%20sig=null;if('signature'%20in%20mainParams)sig=mainParams['signature'];else%20sig=mainParams['sig'];if(sig)cleanURL=cleanURL+'&signature='+sig;}else%20if(cleanURL.indexOf('sig=')!=-1){cleanURL.replace(/sig=/g,'signature=');}return%20cleanURL+'&title='+document.title.match(/^(.*)%20-%20YouTube$/)[1];}return%20null;}function%20getKeyAndVal(str){var%20m=str.match(/^([^=]*)=(.*)$/);if(m)return%20[m[1],m[2]];return%20null;}function%20getLinksAndFormats(urlMap){var%20allLinks=urlMap.split(',');var%20linksAndFormats=new%20Array();var%20numOfLinks=0;for(var%20i=0;i<allLinks.length;i++){var%20link=allLinks[i];link=getCleanURL(link);var%20itagIndex=link.lastIndexOf('itag=');if(itagIndex!=-1){var%20fmt=parseInt(link.substring(itagIndex+5));if(!isNaN(fmt)){linksAndFormats[fmt.toString()]=link;numOfLinks++;}}}if(numOfLinks%20==%200)throw%20'Failed%20to%20find%20download%20links.';linksAndFormats[0]=numOfLinks;return%20linksAndFormats;}function%20getHTMLForLinks(linksAndFormats){var%20numOfLinks=linksAndFormats[0];var%20standardLinksHTML='';var%20hdLinksHTML='';var%20thereIsHD=false;var%20addDash=false;var%20addedSmall=false;if(linksAndFormats['5']){standardLinksHTML+='Small(<a%20href=\''+linksAndFormats['5']+'\'><span%20style="font-weight:normal">FLV</span></a>)%20%20%20%20%20';addDash=true;}var%20openedBracket=false,closedBracket=false;if(linksAndFormats['18']){if(addDash)standardLinksHTML+='%20-%20';standardLinksHTML+='Medium(<a%20href=\''+linksAndFormats['18']+'\'><span%20style="font-weight:normal">MP4</span></a>';addDash=false;openedBracket=true;}if(linksAndFormats['34']){if(!openedBracket)standardLinksHTML+='-%20Medium(';else%20standardLinksHTML+=',';standardLinksHTML+='<a%20href=\''+linksAndFormats['34']+'\'><span%20style="font-weight:normal">FLV</span></a>';openedBracket=true;}if(linksAndFormats['43']){if(!openedBracket)standardLinksHTML+='-%20Medium(';else%20standardLinksHTML+=',';standardLinksHTML+='<a%20href=\''+linksAndFormats['43']+'\'><span%20style="font-weight:normal">WebM</span></a>)';addDash=true;openedBracket=true;closedBracket=true;}if(openedBracket&&!closedBracket)standardLinksHTML+=')';if(!addDash&&(linksAndFormats['18']||linksAndFormats['34']))addDash=true;openedBracket=false,closedBracket=false;if(linksAndFormats['35']){if(addDash)standardLinksHTML+='%20-%20';standardLinksHTML+='Large(<a%20href=\''+linksAndFormats['35']+'\'><span%20style="font-weight:normal">FLV</span></a>';addDash=false;openedBracket=true;}if(linksAndFormats['44']){if(!openedBracket)standardLinksHTML+='-%20Large(';else%20standardLinksHTML+=',';standardLinksHTML+='<a%20href=\''+linksAndFormats['44']+'\'><span%20style="font-weight:normal">WebM</span></a>)';addDash=true;openedBracket=true;closedBracket=true;}if(openedBracket&&!closedBracket)standardLinksHTML+=')';addDash=false,openedBracket=false,closedBracket=false;if(linksAndFormats['22']){hdLinksHTML+='720p(<a%20href=\''+linksAndFormats['22']+'\'><span%20style="font-weight:normal">MP4</span></a>';thereIsHD=true;addDash=true;openedBracket=true;}if(linksAndFormats['45']){if(!openedBracket)hdLinksHTML+='-%20720p(';else%20hdLinksHTML+=',';hdLinksHTML+='<a%20href=\''+linksAndFormats['45']+'\'><span%20style="font-weight:normal">WebM</span></a>)';thereIsHD=true;addDash=true;openedBracket=true;closedBracket=true;}if(openedBracket&&!closedBracket)hdLinksHTML+=')';if(!addDash&&(linksAndFormats['22']||linksAndFormats['45']))addDash=true;openedBracket=false,closedBracket=false;if(linksAndFormats['37']){if(addDash)hdLinksHTML+='%20-%20';hdLinksHTML+='1080p(<a%20href=\''+linksAndFormats['37']+'\'><span%20style="font-weight:normal">MP4</span></a>';thereIsHD=true;addDash=true;openedBracket=true;}if(linksAndFormats['46']){if(!openedBracket)hdLinksHTML+='-%201080p(';else%20hdLinksHTML+=',';hdLinksHTML+='<a%20href=\''+linksAndFormats['45']+'\'><span%20style="font-weight:normal">WebM</span></a>)';thereIsHD=true;addDash=true;openedBracket=true;closedBracket=true;}if(openedBracket&&!closedBracket)hdLinksHTML+=')';var%20title='<h2%20style="color:#333;text-shadow:1px%201px%20#fff;">Download('+numOfLinks+'%20links%20found)</h2><p%20style="color:#555;text-align:center;font-weight:bold">';if(thereIsHD)return%20unescape(title+standardLinksHTML+'<br%20/>'+hdLinksHTML+'</p>');else%20return%20unescape(title+standardLinksHTML+'</p>');}function%20start(){if(document.URL.indexOf('http://www.youtube.com/watch?v=')==-1)return;var%20error=null;var%20urlMap=null;var%20linksAndFormats=null;try{urlMap=getURLMap(document.body.innerHTML);linksAndFormats=getLinksAndFormats(urlMap);}catch(err){error=err;}var%20download_div=document.createElement('span');download_div.id='youtube-download-span';var%20style=download_div.style;style.padding='5px';style.borderRadius='1em';style.lineHeight='1.6';style.display='inline-block';style.margin='5px%20auto';style.boxShadow='4px%204px%203px%20#999';style.border='1px%20#999%20solid';style.backgroundColor='#ffe';if(error==null){download_div.innerHTML=getHTMLForLinks(linksAndFormats);}else{download_div.innerHTML=unescape('<h3%20style="color:#cc0000;">'+error+'</h3>');}var%20container_div=document.createElement('div');container_div.style.textAlign='center';container_div.appendChild(download_div);document.body.insertBefore(container_div,document.body.firstChild);}start();