GOOGLE CHROME: onUnload event when closing popup

I’m building a Google Chrome extension and I needed to run some code when the popup that opens when the user clicks the browser action is closed. Unfortunately, the “onunload” event is never trigered in this case. Well, according to the Chrome Extension mail list, this seems to be a bug and there are some proposed workarounds while this is not fixed:

1) poll chrome.extension.getViews() (using setInterval or setTimeout) to check if the popup has closed.

2) Use the messaging APIs to set up a Port between the popup and background
page. You then should get a onDisconnect event in the background page when
the popup goes away.

3) Have the background dispatch events using chrome.extension.getViews() –
if there is a popup in the list, you use the DOMWindow handle to call a
function in the popup directly.

I’m currently using workaround number 2 above, proposed by Antony Sargent.

Thanks to:

http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/cc6673d8e47a9bf4/06b9ab24ff5ac7e5?#06b9ab24ff5ac7e5

http://code.google.com/p/chromium/issues/detail?id=31262

This entry was posted in programming and tagged . Bookmark the permalink.

Leave a comment