How to contact if the user interrupts the page loading?

In my extension, I rely on the DOMContentLoaded event. It works great. But it fails if the user cancels the page loading in the middle. And my extension just doesn't work. I want him to degrade gracefully here. So, anyway to handle this case?

+4
source share
1 answer

One option is to use nsIWebProgressListener to track the progress of page load events. The onStateChange method onStateChange executed with the STATE_STOP status when the request is completed, even if the user has interrupted the page loading.

Here is a simple example:

 const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START; const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP; window.addEventListener("load", function(e) { gBrowser.addProgressListener({ QueryInterface: function(aIID) { if (aIID.equals(Components.interfaces.nsIWebProgressListener) || aIID.equals(Components.interfaces.nsISupportsWeakReference) || aIID.equals(Components.interfaces.nsISupports)) return this; throw Components.results.NS_NOINTERFACE; }, onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) { if (aFlag & STATE_STOP) { // load finished or was stopped alert("stop"); } }, onLocationChange: function(aProgress, aRequest, aURI) { }, onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) { }, onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) { }, onSecurityChange: function(aWebProgress, aRequest, aState) { } }); }, false); 

This is an example of a toy, of course. Further reading of some implementation details:

+3
source

Source: https://habr.com/ru/post/1379187/


All Articles