webNavigation.onDOMContentLoaded URL filter does not match DNS error URL

只愿长相守 提交于 2019-12-12 05:28:34

问题


Reference to the answer for my previous question here. Briefly: When an error occurs in a navigation webRequest (e.g. a DNS lookup error), the URL to which the tab navigates is available in the url property of webNavigation.onDOMContentLoaded event for the navigation to the displayed error page, but the actual URL displayed (i.e. the about:neterror URL) is not available through other means.

I want to follow the answer's method for getting the error page URL. I wrote this example code where I receive an error page in the browser but when I use webNavigation.onDOMContentLoaded to get the actual URL for the error, the code returns nothing at all. Note that, the code returns the correct URL if there is no error.

Here is my example (test.js):

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

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

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

And, the manifest.json

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

回答1:


Your code does not work the way you desire, because the URL you are looking for does not contain pagedoesnotexist.com as part of the host. The URL on which the error occurred is part of the query, not the host.

Unfortunately, using the events.UrlFilter key queryContains appears to have bugs (I'm still looking into the behavior I'm seeing). I found the following modifications to your code to be effective:

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


来源:https://stackoverflow.com/questions/44551619/webnavigation-ondomcontentloaded-url-filter-does-not-match-dns-error-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!