1

I need to listen to Chrome popup's closing event. I tried listening to onDisconnect event as suggested here :

background.js

  var port = chrome.runtime.connect({ name: "hello" });
  port.onDisconnect.addListener(function() {
    console.log("Disconnected");
  });

However, I get Disconnected every time I open the popup, not when I close it. Does anyone know why this is happening?

Community
  • 1
  • 1
Maximus S
  • 10,759
  • 19
  • 75
  • 154
  • 2
    You were misunderstanding that answer. You're supposed to connect **from** the popup, and listen for disconnect from the background. – Xan Aug 01 '14 at 08:14
  • @Xan How do you get the reference to `port` object from the background? – Maximus S Aug 01 '14 at 19:12
  • 1
    A port represents one end of a 2-ended pipe. You have to listen for opening ports and establish the connection from the background. – Xan Aug 01 '14 at 19:19

1 Answers1

2

Workaround was:

  addEventListener("unload", function (event) {
    doSomethingInPopup();
  }, true);
Maximus S
  • 10,759
  • 19
  • 75
  • 154