There are 2 previous versions of this script.
// ==UserScript==
// @name YouTube Auto Pause On Tab/Window Change
// @namespace http://userscripts.org/users/23652
// @description Automatically pauses the video when you switch to a different tab or window, and automatically plays when you come back to it
// @include http://*.youtube.com/watch?*v=*
// @include http://youtube.com/watch?*v=*
// @copyright JoeSimmons
// @version 1.0.1
// @license Creative Commons Attribution-Noncommercial 3.0 United States License
// ==/UserScript==
// Get ID
function $(ID,root) {return (root||document).getElementById(ID);}
// 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;
}
function pause() {
if(!GM_getValue("on",true)) {return;}
var mp=unsafeWindow._gel("movie_player");
if(mp && mp.getPlayerState && /[013]/.test(mp.getPlayerState())) mp.pauseVideo();
}
function play() {
if(!GM_getValue("on",true)) {return;}
var mp=unsafeWindow._gel("movie_player");
if(mp && mp.getPlayerState && mp.getPlayerState()==2) mp.playVideo();
}
function toggle() {
GM_setValue("on", !GM_getValue("on",true));
$("autopause").textContent="O"+(GM_getValue("on",true)?"n":"ff");
}
var nav=$("masthead-nav-main");
if(nav) nav.appendChild(create("span", {textContent:"Autopause:"}, new Array(create("a", {id:"autopause",textContent:"O"+(GM_getValue("on",true)?"n":"ff"),href:"javascript:void(0);",onclick:toggle}))));
window.addEventListener("blur", pause, false);
window.addEventListener("focus", play, false);