webNavigation.onDOMContentLoaded URL 过滤器不匹配 DNS 错误 URL

webNavigation.onDOMContentLoaded URL filter does not match DNS error URL

参考我之前问题的答案。简而言之:当导航 webRequest 中发生错误时(例如 DNS 查找错误),选项卡导航到的 URL 在 url 属性 中可用 webNavigation.onDOMContentLoaded 事件用于导航到显示的错误页面,但显示的 actual URL(即 about:neterror URL)无法通过其他方式。

我想按照答案的方法获取错误页面URL。我写了这个示例代码,我在浏览器中收到一个错误页面,但是当我使用 webNavigation.onDOMContentLoaded 来获取错误的实际 URL 时,代码 returns 什么也没有。注意,代码returns如果没有错误则正确URL。

这是我的例子 (test.js):

var filter = {
  url:
  [
    {hostContains: "pagedoesnotexist.com"}
  ]
}

function logOnDOMContentLoaded(details) {
  console.log("onDOMContentLoaded: " + details.url);
}

browser.webNavigation.onDOMContentLoaded.addListener(logOnDOMContentLoaded, filter);

而且,manifest.json

{
  "manifest_version": 2,
  "name": "test
  "version": "1.0",
  "background": {
    "scripts": ["test.js"]
  },
  "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest",
    "webNavigation"
  ] 
}

您的代码无法按您希望的方式工作,因为您要查找的 URL 不包含 pagedoesnotexist.com 作为主机的一部分。发生错误的 URL 是 query 的一部分,而不是主机。

不幸的是,使用 events.UrlFilterqueryContains 似乎有错误(我仍在调查我所看到的行为)。我发现对您的代码进行以下修改是有效的:

var errorDomain = 'pagedoesnotexist.com';
var filter = {
  url:
  [
    //{urlPrefix: 'about:neterror'} // works
    // The simple RegExps in the following urlMatches have the
    // possibility to produce false positives on matching the
    // domain.  In other words, some other error URLs could match
    // this RegExp.  However, a more complex RegExp which would
    // prevent such false positive matches would depend on the
    // exact criteria you desire to use for matching.  For
    // instance, are you wanting to match sub-domains?  Only HTTP? 
    // both HTTP and HTTPS?  Any protocol (e.g.  FTP)?
    {urlMatches: '^about:neterror\?.*' + errorDomain + '.*'} // works.
    //{urlMatches: '.*pagedoesnotexist.com.*'} // works
    //{urlMatches: '.*page.*'} // works
    //{queryContains: 'pagedoesnotexist.com'} // Does NOT work (potentially a Firefox bug)
    //{queryContains: 'page'} // Does NOT work (potentially a Firefox bug)
  ]
}

function logOnDOMContentLoaded(details) {
  console.log("onDOMContentLoaded: " + details.url);
}

browser.webNavigation.onDOMContentLoaded.addListener(logOnDOMContentLoaded, filter);