I need to catch the event when a browser window is minimized/maximized using Google Chrome Extensions. How to do this ?
Asked
Active
Viewed 4,970 times
1 Answers
4
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)
}
});
}
});

Rob W
- 341,306
- 83
- 791
- 678
-
if (windowId === -1) ... Why the API sometimes returns windowsId with value -1 ? – Ashraf Bashir Dec 28 '13 at 14:06
-
1@AshrafBashir `-1` is the constant `WINDOW_ID_NONE`. This windowId indicates that there's no focused window. Hence I suggested to assume that the window is minimized. – Rob W Dec 28 '13 at 14:41
-
Did not work this solution. Help me to detect minimize browser window event – The_PratikshaK Jun 14 '20 at 13:08