如何检测扩展中的页面加载错误

How to detect page loading error from extension?

本文关键字:加载 错误 扩展 何检测 检测      更新时间:2023-09-26

有没有办法检测选项卡是否显示"错误页面"?

我的意思是,例如,如果用户输入任何 http://non-existing-url.com 或只是网站不可用。

任何类似于Chrome的webNavigation.onErrorHappened事件。

如果没有类似的事件,也许有一种方法可以检查 Tab http 状态(200、404、502、0 等)?

哎呀没有看到这个话题。这是你可以怎么做,寻找关于:网络错误加载

来自: MozillaZine :: 检测火狐浏览器中的"加载页面问题"

必须通过浏览器的Web导航来阅读文档。

无论如何,当发生错误时,docuri真的很好。 它清楚地告诉您 e 参数中的问题。这些是一些加载的 docuris 的示例:

about: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.

about:neterror?e=malformedURI&u=about%3Abalk&c=&d=The%20URL%20is%20not%20valid%20and%20cannot%

你可以看到第一个是dnsNotFound,第二个是格式错误的URI

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);