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?
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.