2017-06-14 53 views
0

参考我以前的问题here的答案。简要说明:当导航webRequest(例如,DNS查找错误)中发生错误时,导航到显示的错误页面的事件的url属性中可以使用标签导航到的URL,但显示的实际 URL显示(即about:neterror URL)不能通过其他方式获得。webNavigation.onDOMContentLoaded URL过滤器不匹配DNS错误URL

我想跟随答案获取错误页面URL的方法。我编写了这个示例代码,我在浏览器中收到一个错误页面,但是当我使用webNavigation.onDOMContentLoaded来获取错误的实际URL时,代码完全没有返回。请注意,如果没有错误,代码将返回正确的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" 
    ] 
} 

回答

0

您的代码不工作,你想要的方式,因为您正在寻找的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);