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



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

我想按照答案的方法获取错误页面 URL。我编写了此示例代码,其中我在浏览器中收到一个错误页面,但是当我使用 webNavigation.onDOMContentLoaded 获取错误的实际 URL 时,代码根本不返回任何内容。请注意,如果没有错误,代码将返回正确的 URL。

这是我的例子(测试.js(:

var filter = {
  url:
  [
    {hostContains: "pagedoesnotexist.com"}
  ]
}
function logOnDOMContentLoaded(details) {
  console.log("onDOMContentLoaded: " + details.url);
}
browser.webNavigation.onDOMContentLoaded.addListener(logOnDOMContentLoaded, filter);

并且,清单.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 是查询的一部分,而不是主机。

不幸的是,使用这些事件。UrlFilter 键queryContains似乎有错误(我仍在研究我所看到的行为(。 我发现对您的代码进行以下修改是有效的:

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

最新更新