How can I detect an error loading a page from an extension?

Is there a way to determine if an error page is being displayed?

I mean, for example, if the user enters http://non-existing-url.com or the site is simply not available.

Anything that looks like a Chrome event is webNavigation.onErrorOccured .

If there is no similar event, perhaps there is a way to check the Tab Tab state (200, 404, 502, 0, etc.)?

+3


source to share


1 answer


Oops haven't seen this thread. Here's how you can do it: neterror load

from: mozillaZine :: "Issue download page" detection in firefox

must read the document through the web browser navigation. because window.location is different

anyway, docuri is really nice when an error occurs. it tells you clearly what is wrong with it in parameter e. those are examples of some dokrises that are loaded:



o: neterror e = dnsNotFound & u = HTTP% 3A // www.cu.reporterror% 28% 27afew / & c = UTF-8 & d = Firefox% 20can% 27t% 20find% 20the% 20server% 20at% 20www.cu .reporterror% 28% 27afew.

o: neterror e = malformedURI & u = O% 3Abalk & c = & d = Value% 20URL% 20is% 20not% 20valid% 20and% 20cannot%

you can see the first one is dnsNotFound and the second one is malformedURI

var listenToPageLoad_IfProblemLoadingPage = function(event) {

    var win = event.originalTarget.defaultView;
    var webnav = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
    //console.log('webnav:', webnav, 'webnav of selectedtab:', window.gBrowser.webNavigation);
    var docuri = webnav.document.documentURI; //can also try event.originalTarget.linkedBrowser.webNavigation.document.documentURI <<i didnt test this linkedBrowser theory but its gotta be something like that
    var location = win.location + ''; //I add a " + ''" at the end so it makes it a string so we can use string functions like location.indexOf etc

    if (win.frameElement) {
      // Frame within a tab was loaded. win should be the top window of
      // the frameset. If you don't want do anything when frames/iframes
      // are loaded in this web page, uncomment the following line:
      // return;
      // Find the root document:
      //win = win.top;
      if (docuri.indexOf('about:neterror') == 0) {
          Components.utils.reportError('IN FRAME - PROBLEM LOADING PAGE LOADED docuri = "' + docuri + '"');
      }
    } else {
        if (docuri.indexOf('about:neterror') == 0) {
            Components.utils.reportError('IN TAB - PROBLEM LOADING PAGE LOADED docuri = "' + docuri + '"');
        }
    }
}


window.gBrowser.addEventListener('DOMContentLoaded', listenToPageLoad_IfProblemLoadingPage, false);

      

+1


source







All Articles