0

I have searched, but cannot find the answer. I have coded a chrome extension and I need to do some cleanup when the extension's pop-up window closes. Is there a way to do this? What I want to do is execute a function when the window closes.

Stewart Lynch
  • 875
  • 9
  • 26

2 Answers2

0

window.onunload should work in a Chrome popup. You will likely have very limited time once the event fires though as Chrome is aggressive about clearing extension resources.

window.onunload = funcRef; 
abraham
  • 46,583
  • 10
  • 100
  • 152
  • No. This doesn't work. Apparently, I am not alone. See https://code.google.com/p/chromium/issues/detail?id=31262 – Stewart Lynch May 06 '14 at 15:04
  • Reading the comments it sounds like it works except for a few edge cases like rapidly clicking on the browserAction button. – abraham May 06 '14 at 15:31
  • I will have to experiment some more, because it didn't work for me. Perhaps I am not putting the call in the right place. – Stewart Lynch May 06 '14 at 16:38
0

Since onunload doesnt work for all cases in the extension popup case, try this:

When the popup shows (on dom loaded) send a message to your background page on a setInterval. From the background,start an interval on the first message and keep checking until you dont receive a message (so popup closed). Rapidly opening/closing the popup could be tricky to ge right but is possible by adding popup 'session' identifiers like timestamp at dom load. Of course the cleanup is done by background so it needs to know what to cleanup too (in the message params maybe)

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • 2
    Wouldn't opening a port and watching for `onDisconnect` in the background be simpler and better? – Xan May 08 '14 at 14:49
  • Besides, this might not answer the question, if the cleanup needs to be done in the popup's context. – Xan May 08 '14 at 14:52