Detecting the browser quit event (using sdk)

For one of my add-ons, I need to be able to detect browser quit event, and send a logout request to a page before it closes.

I am using add-on sdk. The page about working with events doesn’t say anything about a browser close event.

Is it even possible to do this using sdk?

Thanks!

You can use the low-level system/events API and any of these observer notifications.

2 Likes

Thanks!

I tried it out, and it doesn’t always seem to work. I am testing it with this script:

var ev = require("sdk/system/events");

ev.on("quit-application-requested", function(eSubject, eTopic, eData) {
    console.log(eSubject.type);
});

If I just close the browser without creating a new tab or accessing some page, I can see the output as:

console.log: logout-test: quit-application-requested

However, it fails pretty much always if I access some page, or open a new tab or window before hitting the close button. Even ctrl+q doesn’t seem to do anything differently.

If I open a new hidden frame in the background, will it close when I close the last browser window? Can I make use of that in any way? Perhaps by tracking when that hidden frame is about to be closed?

Thanks!

You might want to try with quit-application-granted. That’s called when it’s clearer that the application will close.

That could work, but I don’t know how that would work with the SDK, since it doesn’t have the same concept of a background page as WebExtensions or Chrome Extensions do.

1 Like

Thanks!
Tried your suggestion, but same problem again. Could you please check if this is a bug?

  • Use the script above. Run using jpm -b /usr/bin/firefox run.
  • Open any page in the browser and close it however you want.

Console doesn’t show the quit-application-granted event. If you close the browser without opening any new page, it works.

Is it just my system?