2017-06-14 58 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);