YouTube Random Video Playlist

By JoeSimmons Last update Jun 30, 2009 — Installed 343 times.

There are 1 previous version of this script.

// ==UserScript==
// @name           YouTube Random Video Playlist
// @namespace      http://userscripts.org/users/23652
// @description    Pick a user, and this script will automatically watch random videos from that user until you tell it to stop via user script command
// @include        http://*.youtube.com/*
// @copyright      JoeSimmons
// @version        1.0.0
// @license        Creative Commons Attribution-Noncommercial 3.0 United States License
// ==/UserScript==

// get() function by JoeSimmons
// Syntax: get('http://www.google.com/', handleResponse);
function get(url, cb) {
GM_xmlhttpRequest({
    method: 'GET',
    url: url,
    headers: {'Accept': 'application/atom+xml,application/xml,text/xml,text/html'},
    onload: function(r) {cb(r);}
});
}

// inArray
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function random(auto) {
if(!auto && GM_getValue('watching_random',false) && confirm('Stop watching random videos?')) {
GM_setValue('watching_random',false);
clearInterval(intv);
return;
} else if(!GM_getValue('watching_random',false)) {
var user = prompt('Username to watch?', GM_getValue('last_user',''));
GM_setValue('last_user', user);
} else if(GM_getValue('watching_random',false)) var user=GM_getValue('last_user','');
if(!user || user=='') return;
get('http://www.youtube.com/profile?user='+user+'&view=videos', function(r){
var tmpvideos = r.responseText.replace(/[\n\t]/g,'').match(/watch\?v=[^"&]+/g), videos=new Array();
if(!tmpvideos) {alert('User doesn\'t exist or doesn\'t have any videos.'); return;}
for(var i=0; i<tmpvideos.length; i++) if(!videos.inArray(tmpvideos[i].substring(8))) videos.push(tmpvideos[i].substring(8));
tmpvideos=null;
GM_setValue('watching_random', true);
window.location = "http://www.youtube.com/watch?v="+videos[Math.round(Math.random()*(videos.length-1))];
});
}

var intv;
if(GM_getValue('watching_random',false)) {
window.addEventListener('load', function(){
if(!document.getElementById('movie_player')) {
GM_setValue('watching_random', false);
return;
}
intv=setInterval(function(){
if(unsafeWindow._gel('movie_player').getPlayerState()==0) {
clearInterval(intv);
random(true);
}
}, 500);
}, false);
}

GM_registerMenuCommand('Random Video Playlist (or stop playlist)', function(){random(false);});