45

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.

ted
  • 13,596
  • 9
  • 65
  • 107
ebi
  • 4,862
  • 6
  • 29
  • 40
  • Have you tried [`chrome.tabs.onRemoved`](http://developer.chrome.com/extensions/tabs.html#event-onRemoved) or maybe [`chrome.windows.onRemoved`](http://developer.chrome.com/extensions/windows.html#event-onRemoved) if it counts as a window? – BeardFist Apr 03 '13 at 22:12
  • It doesn't count as a window. – ebi Apr 04 '13 at 02:24
  • 1
    See also: [Does onbeforeunload event trigger for popup.html in a google chrome extension?](http://stackoverflow.com/q/2315863/405550) – Zaz Oct 17 '16 at 14:39

8 Answers8

35

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.

方 觉
  • 4,042
  • 1
  • 24
  • 28
16

It's as simple as that:

// popup.js
chrome.runtime.connect({ name: "popup" });
// background.js
chrome.runtime.onConnect.addListener(function(port) {
    if (port.name === "popup") {
        port.onDisconnect.addListener(function() {
           console.log("popup has been closed")
        });
    }
});

Note that there are some edge cases where this method doesn't work. E.g. when having the popup open and then switching tabs by using a keyboard shortcut such as ctrl + t for example.

gignu
  • 1,763
  • 1
  • 14
  • 24
9

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

  window.onblur = function(){}

and send a message to active tab.

nxtwrld
  • 1,942
  • 14
  • 15
  • Unfortunately this triggers when switching app even with the popup still remaining open, but does not trigger when actually closing the popup by clicking the extension icon or current tab – Nick Cardoso Jul 01 '19 at 07:41
  • On Safari Web Extension, only this way works! – WillBC Feb 10 '21 at 03:44
4

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.

user7610
  • 25,267
  • 15
  • 124
  • 150
2

I'm surprised noone mentioned this (I've tested it on Firefox Nightly and it works there):

window.addEventListener("pagehide", function(event) {
    console.log(event);
 });
Petr L.
  • 414
  • 5
  • 13
  • Doesn't work for me in Chrome 96.0.4664.110. I've also tried `visibilitychange` event but it works inconsistently. Moving back to port.onDisconnect hack. – Dmitry Davydov Jan 04 '22 at 03:37
1

Try to use the visibilitychange event on the document object. It works fine for me:

document.addEventListener('visibilitychange', MyFunction, false);
Murillo
  • 19
  • 1
1

This is the solution I used for manifest V3:

Set up a connection in your popup's JavaScript file

chrome.runtime.connect();

In background.js, use onConnect to listen for the event

chrome.runtime.onConnect.addListener(port => {
  port.onDisconnect.addListener(()=>{
    //Your event will be fired in here
  })
})
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
zinzin
  • 19
  • 2
0

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

chrome.windows.onFocusChanged.addListener(function(window) {
    //handle close event
});
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225