YouTube Extended Player Functionality

By JoeSimmons Last update Feb 28, 2010 — Installed 2,649 times.

There are 4 previous versions of this script.

// ==UserScript==
// @name           YouTube Extended Player Functionality
// @namespace      http://userscripts.org/users/23652
// @description    Adds extended functionality to the youtube player such as player refreshing if video stops loading, user specified seek ahead, stop video loading, and more
// @include        http://*.youtube.com/watch?*v=*
// @include        http://youtube.com/watch?*v=*
// @copyright      JoeSimmons
// @version        1.0.4
// @license        Creative Commons Attribution-Noncommercial 3.0 United States License
// ==/UserScript==

// Created by avg, modified by JoeSimmons
function create(a,b,c) {
	var ret=document.createElement(a.toLowerCase());
	if(b) for(var prop in b) if(prop.indexOf("on")==0) ret.addEventListener(prop.substring(2),b[prop],false);
		else if(",style,accesskey,id,name,src,href".indexOf(","+prop.toLowerCase())!=-1) ret.setAttribute(prop.toLowerCase(), b[prop]);
		else ret[prop]=b[prop];
	if(c) for(var i=0,l=c.length; i<l; i++) ret.appendChild(c[i]);
	return ret;
}

// Define GM_addStyle if it's not Firefox
if(typeof GM_addStyle==='undefined') 
    GM_addStyle = function(css) {
        var head = document.getElementsByTagName('head')[0], style = document.createElement('style');
        if (!head) {return}
        style.type = 'text/css';
        try {style.innerHTML = css} catch(x) {style.innerText = css}
        head.appendChild(style);
    }

// add some style to our div
GM_addStyle(<><![CDATA[
#pl_fu_drop {
text-align:right;
color:#000000;
width:100%;
background:transparent;
font-size: .9em;
font-family: arial, tahoma, verdana;
padding-top: .4em;
}
]]></>.toString());

// when hovered over, start timeout for actually showing the div
function show() {
// don't hide div, they hovered back over the link
window.clearTimeout(hideit);

// show the div
showit = window.setTimeout(show4real, 500);
}

// hide div
function hide() {
// hide the div, they went off the link
window.clearTimeout(showit);

// hide the div
hideit = window.setTimeout(hide4real, 500);
}

// actually show the div if the timeout if successfull (they waited half of a second on the link)
function show4real() {
document.getElementById("pl_fu_drop").style.display="block"; // show div
}

// hide the div if the timeout if successfull (they waited half of a second off the link)
function hide4real() {
document.getElementById("pl_fu_drop").style.display="none"; // hide div
}

if(!unsafeWindow.yt) return; // quit if there is no youtube config object
var doc = document.wrappedJSObject || document, // be sure we have a document, preferrably unwrapped (unsafe)
	hideit=null, showit=null, // blank timeout objects
	args = unsafeWindow.yt["config_"]["SWF_ARGS"], // grab the youtube swf arguments
	id = escape(args["video_id"]), // grab the video id
	t = escape(args["t"]), // grab the t argument
	nav = document.getElementById("masthead-nav-user"); // lastly grab the nav bar so we can insert the link
if(!doc || !args || !nav || !id || !t) return; // if any of those don't work, then quit

// add link to nav section
nav.insertBefore(create("a", {href:"javascript:void(0);",textContent:"Player Functions",id:"pl_fu",onmouseover:show,onmouseout:hide}), nav.firstChild);

// add the hidden dropdown div element with our "extended functions"
nav.parentNode.insertBefore(create("div", {id:"pl_fu_drop",style:"display: none;",onmouseout:hide,onmouseover:function(){clearTimeout(hideit);}}, new Array(
create("span", {textContent:"YouTube Extended Player Functionality",style:"font-size:120%;font-weight:bold;"}),
create("br"), create("br"),
create("a", {textContent:"Refresh Player (keep current duration)",href:"javascript:void(0);",onclick:refresh}),
create("br"),
create("a", {textContent:"Seek To (specify time)",href:"javascript:void(0);",onclick:seek}),
create("br"),
create("a", {textContent:"Stop Loading",href:"javascript:void(0);",onclick:stop})
)), nav.nextSibling.nextSibling.nextSibling);

// refresh the player, keeping current time
function refresh() {
var player=doc.getElementById("movie_player"), dur=player.getCurrentTime()||0;
if(player && id && player.getPlayerState()!=-1) player.loadVideoById(id, dur);
}

// seek to specific time
function seek() {
var time = prompt("Time to seek to:", "0:00"), regex=/(\d+):([\d\.]+)/, player=doc.getElementById("movie_player");
if(!time || time=="" || !regex.test(time)) return;
time=(parseFloat(time.match(regex)[1])*60) + parseFloat(time.match(regex)[2]);
if(player && player.seekTo) player.seekTo(time, true);
}

// stop player loading all-together
function stop() {
var player=doc.getElementById("movie_player");
if(player) {
player.stopVideo();
player.clearVideo();
}
}