There are 21 previous versions of this script.
// Youtube without Flash Auto
// version 0.6.4 BETA!
// 2009-11-15
// Copyright (c) 2009, Arne Schneck, themiddleman
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
//
// Changes version 0.6.3 -> 0.6.4:
// * Add support for Midori
// * Fix the problem that clicking the view without Flash links would always
// play the highest quality
// Changes version 0.6.2 -> 0.6.3:
// * Resizing seems to make Opera crash
// => disable resizing in Opera for the moment until we know how to fix it
// Changes version 0.6.1 -> 0.6.2: (thanks to chromeuser8)
// * Add support for Google Chrome
// Changes version 0.6 -> 0.6.1: (thanks to magical_graam)
// * Now the script also works with the Youtube button that resizes the video
// Changes version 0.5.2 -> 0.6: (all changes by themiddleman)
// * Works again with changes on Youtube page
// * More options for video quality, cancle button on preferences
// * Cleaner code
// Changes version 0.5.1 -> 0.5.2:
// * Add support for Epiphany
// Changes version 0.5 -> 0.5.1:
// * Add support for Safari with GreaseKit (thanks to samuel hallé)
// Changes version 0.4 -> 0.5:
// * Add support for Opera
// Changes version 0.3.2 -> 0.4:
// * Add another link to make the the default quality configurable.
// (thanks to themiddleman for the patch)
// Changes version 0.3.1 -> 0.3.2:
// * Make default quality configurable at the beginning of this script.
// * Workaround for the problem that sometimes the download links wouldn't
// work or the video in the media player wouldn't start.
// Changes version 0.3 -> 0.3.1:
// * Now the mouse changes to a pointer on the links after "View without Flash"
// (thanks to themiddleman for the hint)
// Changes version 0.2.1 -> 0.3:
// * Works with the new Youtube layout (13 August 2009).
// Changes version 0.2 -> 0.2.1:
// * Works again with current Youtube page.
// Changes version 0.1 -> 0.2:
// * Now actually works if Flash is not installed
// * Fix separator symbols between links
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script. To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Access Bar", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name Youtube without Flash Auto
// @namespace none
// @description Adds links below the Youtube video to (a) download the video (HD .mp4 file, no converters are used, no external sites) (b) view the video with an embedded external player (like mplayerplug-in or the totem plugin)
// @include http://youtube.*/watch*
// @include http://*.youtube.*/watch*
// ==/UserScript==
function runScript() {
// Opera doesn't have unsafeWindow, GM_getValue and GM_setValue. We test
// whether we're in Opera and provide workarounds for that case.
if(/Opera|Safari|Epiphany|Chrome|Midori/.test(navigator.userAgent)) {
unsafeWindow = window;
GM_getValue = function ( cookieName, oDefault ) {
var cookieJar = document.cookie.split( "; " );
for( var x = 0; x < cookieJar.length; x++ ) {
var oneCookie = cookieJar[x].split( "=" );
if( oneCookie[0] == escape( cookieName ) ) {
try {
var footm = unescape( oneCookie[1] );
} catch(e) { return oDefault; }
return footm;
}
}
return oDefault;
};
GM_setValue = function ( cookieName, cookieValue, lifeTime ) {
if( !cookieName ) { return; }
if( lifeTime == "delete" ) { lifeTime = -10; } else { lifeTime = 31536000; }
document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) + ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() + ";path=/";
}
}
// The old formats, for reference:
// Youtube offers several different video formats. If several formats with high
// or normal quality are available, we choose the format that is furthest left
// in the lists below
//var formats_HD = [22];
//var formats_High = [34, 18, 35];
//var formats_Normal = [6, 5];
// The new formats, the string array is what is displayed to the user.
var formatsAvailable = new Array( 5, 6, 34, 18, 35, 22);
var formatsAvailableNames = new Array("Low 5", "Low 6", "High 34", "High 18", "High 35", "HD 22");
function PreferencesManager() {
this.preferencesId = "preferences";
this.parentLocation = document.getElementById('watch-ratings-views');
this.writePreferences = function() {
var prefbar = document.createElement("div");
prefbarHtml = '<div id="' + this.preferencesId + '" style="border:1px solid #000000; width:300px; margin-left:auto; margin-right:auto; padding:3px; margin-top:10px; display:none">'
+ '<h3>Youtube Without Flash Preferences</h3>'
+ '<div title="If the default quality is not available the next best quality video will be played">'
+ 'Default quality'
+ '<select id="preferencesDefaultQuality">';
for(var i = 0; i < formatsAvailable.length; i++) {
prefbarHtml += '<option value="' + formatsAvailable[i] + '">' + formatsAvailableNames[i] + '</option>'
}
prefbarHtml += '<option value="flash">View Flash</option>'
+ '</select></div>'
+ '<p align="right">'
+ '<a class="link" id="cancelPreferencesLink">Cancel</a> '
+ '<a class="link" id="savePreferencesLink">Save</a>'
+ '</p>'
+ '</div>';
prefbar.innerHTML = prefbarHtml;
this.parentLocation.parentNode.insertBefore(prefbar,this.parentLocation);
document.getElementById("preferencesDefaultQuality").value = GM_getValue("defaultQuality", "35");
var savePreferencesLink = document.getElementById('savePreferencesLink');
savePreferencesLink.addEventListener("click", savePreferences, true);
var cancelPreferencesLink = document.getElementById('cancelPreferencesLink');
cancelPreferencesLink.addEventListener("click", cancelPreferences, true);
};
this.loadPreferences = function() {
document.getElementById("preferencesDefaultQuality").value = getDefaultQuality();
};
this.savePreferences = function() {
this.setDefaultQuality(document.getElementById("preferencesDefaultQuality").value);
};
this.setPreferencesVisible = function(visible) {
if(visible) {
document.getElementById(this.preferencesId).style.display = "";
}
else {
document.getElementById(this.preferencesId).style.display = "none";
}
};
this.getDefaultQuality = function() {
var defaultDefaultQuality;
if(document.getElementById("preferencesDefaultQuality") == null) {
defaultDefaultQuality = '35';
}
else {
defaultDefaultQuality = document.getElementById("preferencesDefaultQuality").value;
}
return GM_getValue("defaultQuality", defaultDefaultQuality);
};
this.setDefaultQuality = function(quality) {
GM_setValue("defaultQuality", quality);
};
// This doesn't work on top where it belongs.
this.writePreferences();
}
var preferencesManager = new PreferencesManager();
function showPreferences() {
preferencesManager.setPreferencesVisible(true);
}
function savePreferences() {
preferencesManager.savePreferences();
preferencesManager.setPreferencesVisible(false);
}
function cancelPreferences() {
preferencesManager.setPreferencesVisible(false);
}
//http://diveintogreasemonkey.org/patterns/add-css.html
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
try {
style.innerHTML = css;
} catch(x) { style.innerText = css; }
head.appendChild(style);
}
// Add css for links.
// This is probably the best way to make text look like links, using the
// normal ways wont work in GM because there is nowhere to put
// "return false;" in the link.
addGlobalStyle(".link{color:#0033CC;}" +
".link:hover{cursor: pointer; text-decoration:underline;}");
var defaultQuality = preferencesManager.getDefaultQuality();
var playerDiv = document.getElementById("watch-player-div");
var playerDivLoad = playerDiv.innerHTML; // For restoring flash.
if(defaultQuality != "flash") {
// If they don't want flash clear it asap so it doesn't start autoplaying.
playerDiv.innerHTML = "";
}
var args = unsafeWindow.yt.config_.SWF_ARGS;
var urlmap = decodeURIComponent(args['fmt_url_map']).split(",");
var urlsAvailable = new Array(6);
for(var i = 0; i < urlmap.length; i++) {
var test = urlmap[i].split("|",2);
for(var j = 0; j < formatsAvailable.length; j++) {
if(test[0] == formatsAvailable[j]) {
urlsAvailable[j] = test[1];
}
}
}
function resizePlayer() {
//Resizing doesn't work in Opera for some reason.
//Need to find a way to resize in Opera.
//Until then, we simply disable resizing for Opera.
if (!(/Opera/.test(navigator.userAgent))) {
var player = document.getElementById('no-flash-player');
if (!(player == null)) {
if (unsafeWindow._hasclass(unsafeWindow._gel('baseDiv'), 'watch-wide-mode')) {
player.width = 960;
player.height = 582;
} else {
player.width = 640;
player.height = 388;
}
}
}
}
document.getElementById('player-toggle-switch').addEventListener('click', resizePlayer, false);
function writePlayer(quality) {
// If we use the regular video URL in the media player, the video sometimes
// won't start. Adding '&begin=0' to the video URL seems to fix the problem.
playerDiv.innerHTML = '<embed id="no-flash-player" type="application/x-mplayer2" src="' +
urlsAvailable[formatsAvailable.indexOf(quality)] + '&begin=0" scale="tofit" width="640" height="388"><br />';
resizePlayer();
// One could uncomment the following 2 lines and comment the previous 2 to use the
// HTML5 video player, although it won't work with most/all browsers because
// they don't support the video format youtube uses.
//playerDiv.innerHTML = '<video src="' +
// urlsAvailable[formatsAvailable.indexOf(quality)] + '" width="640" height="388" autoplay="true"></video>';
//alert("Playing video id " + id + " which is url: " + urlsAvailable[id]);
}
function restoreFlash() {
playerDiv.innerHTML = playerDivLoad;
}
var haveFlash;
var noplayerDiv = document.getElementById("watch-noplayer-div");
if(noplayerDiv == null) {
haveFlash = true;
}
else {
haveFlash = false;
}
var linkbar = document.createElement("div");
var linkViewFlash = "";
var linkViewPreferences = "";
var downloadLinks = "";
var playLinks = "";
for(var i = 0; i < formatsAvailable.length; i++) {
if(typeof(urlsAvailable[i]) != "undefined") {
downloadLinks += '| <a href="' + urlsAvailable[i] + '&begin=0">' +
formatsAvailableNames[i] + '</a> ';
playLinks += '| <a class="link" id="play' + formatsAvailableNames[i] + '">' +
formatsAvailableNames[i] + '</a> ';
}
}
if(haveFlash) {
linkViewFlash = ' ♦ <a class="link" id="restoreFlash">View Flash</a>';
}
linkViewPreferences = '<a class="link" id="preferencesLink">Preferences</a>';
linkbar.innerHTML = '<div id="dlbar" style="padding-top: 8px;">'
+ 'Download '
+ downloadLinks
+ ' ♦ View without Flash '
+ playLinks
+ linkViewFlash
+ '<div style="float:right;">' + linkViewPreferences + '</div>'
+ '</div>';
mainarea = document.getElementById('watch-ratings-views');
mainarea.parentNode.insertBefore(linkbar,mainarea);
for(var i = 0; i < formatsAvailable.length; i++) {
if(typeof(urlsAvailable[i]) != "undefined") {
var playLink = document.getElementById('play' + formatsAvailableNames[i]);
var writePlayerFunction = function(qual) {
return function (event) {
writePlayer(qual);
};
};
playLink.addEventListener("click", writePlayerFunction(formatsAvailable[i]), true);
}
}
if(haveFlash) {
var restoreFlashLink = document.getElementById('restoreFlash');
restoreFlashLink.addEventListener("click", restoreFlash, true);
}
var preferencesLink = document.getElementById('preferencesLink');
preferencesLink.addEventListener("click", showPreferences, true);
// Finally, write the player, if the desired format is not available we
// keep going down in quality until we find one.
if(defaultQuality != "flash") {
var defaultQualityId = formatsAvailable.indexOf(parseInt(defaultQuality));
while(typeof(urlsAvailable[defaultQualityId]) == "undefined") {
defaultQualityId--;
if(defaultQualityId < 0) {
break;// Just in case something goes wrong.
}
//alert(defaultQualityId);
}
writePlayer(parseInt(formatsAvailable[defaultQualityId]));
}
//alert("Your default quality is set to " + defaultQuality + " and I am playing " + formatsAvailable[defaultQualityId]);
}
if(/Chrome/.test(navigator.userAgent)) {
var script = document.createElement("script");
script.type = "application/javascript";
script.textContent = "(" + runScript + ")();";
document.body.appendChild(script);
} else {
runScript();
}
