Is there an event for when a Chrome Extension popup is closed?

前端 未结 4 1791
说谎
说谎 2020-11-29 05:28

I\'ve already tried window.unload, window.beforeunload, etc. I\'m looking for a way to notify my background page once the popup is closed.

相关标签:
4条回答
  • 2020-11-29 05:41

    There is currently no way to figure out when the browser action popup was closed as window.unonload is triggered immediately when the popup finished loading, not when it's closed. There is a bug crbug.com/31262 for this.

    Three work arounds available are well described here. They include the port.onDisconnect trick and periodically polling chrome.extension.getViews() from either the popup or a background page.

    0 讨论(0)
  • 2020-11-29 05:43

    Probably a hacky way, but in the popup page you can listen to

      window.onblur = function(){}
    

    and send a message to active tab.

    0 讨论(0)
  • 2020-11-29 05:48

    Finally found the solution. Put below code in background.js/eventPage.js:

    chrome.windows.onFocusChanged.addListener(function(window) {
        //handle close event
    });
    
    0 讨论(0)
  • 2020-11-29 05:52

    You can try this. Connect to your background page with chrome.runtime.connect (or chrome.extension.connect before Chrome 26) and port.onDisconnect will be fired in your background page when the popup is closed.

    0 讨论(0)
提交回复
热议问题