Detect browser focus/out-of-focus via Google Chrome Extension
You can use the chrome.windows
API to monitor this. You would add a listener for the onFocusChanged
event and if the event returned -1
or chrome.windows.WINDOW_ID_NONE
then you know that the browser lost focus:
var inFocus = true; // global boolean to keep track of statechrome.windows.onFocusChanged.addListener(function(window) { if (window == chrome.windows.WINDOW_ID_NONE) { inFocus = false; } else { inFocus = true; }});
Maybe some don't see @Giedrius' comment. This interval works:
window.setInterval(checkBrowserFocus, 1000); function checkBrowserFocus(){ chrome.windows.getCurrent(function(browser){ console.log(browser.focused) })}
setInterval(function() { chrome.windows.getLastFocused(function(window) { console.log(window.id.toString() + " " + window.focused.toString()) });}, 1000);
or:
setInterval(function() { chrome.windows.getCurrent(function(window) { console.log(window.id.toString() + " " + window.focused.toString()) });}, 1000);
window.focused is false means that no chrome window is focused.