When a packaged app is closing, its Event page (defined in the background scripts section of the manifest) gets notified with a chrome.runtime.onSuspend
event. Your code should be handled there. However...
This event indicates that the app is being unloaded and has very little time for cleaning up. As stated:
Once this event is triggered, the app runtime initiates the process of shutting down the app: all events cease to function and JavaScript execution stops. Any asynchronous tasks initiated during this event may not finish processing. Keep the clean-up operations synchronous and straightforward.
$.ajax()
is an asynchronous operation that can be slow. Hence, there is a high likelihood of failure when used in a clean-up routine.
In theory, it could be made synchronous, but this feature is disabled in Chrome Apps. Therefore, you cannot reliably make a network request while your app is closing.
There might be a workaround using onClosed
handlers:
chrome.app.window.create('window.html', function(win) {
win.onClosed.addListener(function() {
// ...
});
});
This approach could potentially work because any asynchronous tasks started from here technically begin before onSuspend
is invoked, preventing the event page from unloading. However, personal testing is necessary to confirm its effectiveness.