I need to catch the event when a browser window is minimized/maximized using Google Chrome Extensions. How to do this ?
To know whether a given window is minimized / maximized, just use chrome.windows.get:
// assume windowId is given
chrome.windows.get(windowId, function(chromeWindow) {
// "normal", "minimized", "maximized" or "fullscreen"
alert('Window is ' + chromeWindow.state);
});
There's no event that tells you that a window is minimized / maximized. However, you can get notified of window focus changes via chrome.windows.onFocusChanged. This event provides an ID which can be used in the method shown at the top of my answer. Note that it may occasionally be called with "-1" (chrome.windows.WINDOW_ID_NONE). In this case, just assume that the window is minimized.
chrome.windows.onFocusChanged.addListener(function(windowId) {
if (windowId === -1) {
// Assume minimized
} else {
chrome.windows.get(windowId, function(chromeWindow) {
if (chromeWindow.state === "minimized") {
// Window is minimized
} else {
// Window is not minimized (maximized, fullscreen or normal)
}
});
}
});