How to Detect Window On Minimize/Maximize Event in Chrome Extension?

后端 未结 1 1215
醉话见心
醉话见心 2021-01-16 06:24

I need to catch the event when a browser window is minimized/maximized using Google Chrome Extensions. How to do this ?

相关标签:
1条回答
  • 2021-01-16 06:39

    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)
                }
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题