webNavigation.onDOMContentLoaded URL filter does not match DNS error URL
Asked Answered
C

1

0

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"
  ] 
}
Coherent answered 14/6, 2017 at 17:58 Comment(0)
K
0

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);
Ky answered 14/6, 2017 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.