Created
January 30, 2012 02:11
-
-
Save dwbfox/1701971 to your computer and use it in GitHub Desktop.
JS YouTube Download Userscript
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
(function () | |
{ | |
// Video Settings | |
var VIDEO_TITLE = document.getElementById('eow-title').getAttribute('title'); | |
var VIDEO_ID = window.location.href.match(/v=[A-Za-z0-9-_]{11}/)[0].substring(2); | |
// Script settings | |
var SETTINGS = { | |
"DEBUG_MODE": false, | |
"SCRIPT_VERSION": "1.0.3", | |
"QUALITY": { | |
"5": "240p (FLV)", | |
"34": "380p (FLV)", | |
"35": "480p (FLV)", | |
"18": "360p (MP4)", | |
"22": "720p (MP4)", | |
"37": "1080p (MP4)", | |
"43": "360p (WebM)", | |
"44": "480p (WebM)", | |
"45": "720p (WebM)", | |
"46": "1080p (WebM)", | |
"102": "720p (WebM)", | |
"100": "360p 3D(WebM)", | |
"101": "480p 3D(WebM)" | |
} | |
}; | |
con(JSON.stringify(SETTINGS)); | |
// Create the main interface div box along with the download button | |
createDownloadBox(); | |
// Get an array of all available download links | |
links = getDownloadLinks(); | |
// Populate the combox box with available links | |
populateList(links); | |
// Download button is clicked | |
document.getElementById('download-button').addEventListener('click', function (e) | |
{ | |
con(e); | |
window.location = document.getElementById('qualitySelect').value; | |
}); | |
function createDownloadBox() | |
{ | |
var headline = document.getElementById('watch-actions'); | |
// Create the main download widget box | |
var box = document.createElement('div'); | |
var boxCSS = 'padding-top:15px'; | |
box.setAttribute('style', boxCSS); | |
box.setAttribute('id', 'download-box'); | |
headline.appendChild(box); | |
// Attach the script log | |
var logoLink = document.createElement('a'); | |
var logoImg = document.createElement('img'); | |
logoImg.setAttribute('src', 'http://i40.tinypic.com/j7ax01.png'); | |
logoImg.setAttribute('class', 'yt-uix-tooltip-reverse yt-uix-tooltip'); | |
logoImg.setAttribute('data-tooltip-text', 'Visit YouTube + MP3 Script Website (opens a new window)'); | |
logoLink.href = 'http://userscripts.org/scripts/show/123836'; | |
logoLink.setAttribute('target', '_blank'); | |
logoLink.setAttribute('style', 'position:relative;top:6px;'); | |
logoLink.appendChild(logoImg); | |
box.appendChild(logoLink); | |
// Create the combo box | |
var selector = document.createElement('select'); | |
selector.setAttribute('id', 'qualitySelect'); | |
selector.setAttribute('style', ''); | |
box.appendChild(document.createTextNode('Available Formats ')); | |
box.appendChild(selector); | |
// Create the download button | |
var button = document.createElement('button'); | |
button.setAttribute('class', 'yt-uix-button yt-uix-tooltip'); | |
button.setAttribute('id', 'download-button'); | |
button.setAttribute('style', 'position:relative;bottom:2px;left:10px;'); | |
button.appendChild(document.createTextNode('Download video')); | |
box.appendChild(button); | |
return box; | |
} | |
function getDownloadLinks() | |
{ | |
var PAGE_SOURCE = document.body.innerHTML; | |
var downloadLinks = []; | |
var matches = PAGE_SOURCE.match(/m_map": "url=(.*?)", "/g)[0].split('url='); | |
// Iterate through each valid download link and clean it up | |
for (i = 0; i <= matches.length - 1; i++) | |
{ | |
// Make sure it's a valid download link | |
if (matches[i].indexOf('lscache') !== -1) | |
{ | |
downloadLinks.push(unescape(matches[i]).replace(/(\\u0026quality.+|\\u0026type.+)/, '')); | |
} | |
} | |
return downloadLinks; | |
} | |
function con(message) | |
{ | |
if (SETTINGS.DEBUG_MODE) GM_log(message); | |
} | |
function populateList(list) | |
{ | |
con('Populating combo box with ' + list.length + ' links.'); | |
var qSelect = document.getElementById('qualitySelect'); | |
// Append MP3 download link | |
qSelect.innerHTML += '<option value="http://www.youtube-mp3.org/#v=' + VIDEO_ID + '"> MP3</option>'; | |
// iterate through the download link list and append it to the select combo box | |
for (i = 0; i <= list.length - 1; i++) | |
{ | |
// Attach the video's title to the download link | |
var downloadLink = list[i] + "&title=" + VIDEO_TITLE; | |
// Get the itag query string which indicates video format and quality | |
var format = downloadLink.match(/itag=\d{1,3}/)[0].substr(5); | |
var quality = SETTINGS.QUALITY[format]; | |
// SKIP unknown quality - FIXME | |
if (typeof quality === 'undefined') | |
{ | |
con("Ran into unknown quality: " + format + ". Skipping..."); | |
continue; | |
} | |
con('Video format: ' + format); | |
qSelect.innerHTML += '<option value="' + downloadLink + '" >' + quality + '</option>'; | |
con('Link ' + (i + 1) + ' proccessed.'); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment