The code im currently using is this
var focus;
function focuswindow() { focus = true; }
function hidewindow() { focus = false; }
window.onfocus = focuswindow();
window.onblur = hidewindow();
The idea is that it can be used like this
if( focus ) { //do something
}
However its not working. Also it only needs to work on Chrome (so no legacy IE stuff) since its for a Chrome extension.