4

那麼,我目前正在開發Google Chrome擴展,並且我需要獲取所有DNS和404錯誤以進行重定向。問題很簡單,我真的不知道它是如何可能的...使用Chrome擴展獲取DNS錯誤和錯誤404

如果這是一個域錯誤,我想獲取域名和404錯誤,我想獲取名稱頁。

例子:

不良域名:http://www.justforthetest.com/ =>取justforthetest

404錯誤:http://www.valeriemates.com/professinal.html =>獲取不是職業

希望有人能提供給我一些幫助...... 提前感謝!

+0

需要什麼樣的信息,以使重定向?大多數網站都有一個自定義的404頁面,Chrome瀏覽器尚不允許訪問請求和響應標頭。 – mattsven 2011-03-17 15:48:42

+0

我已經編輯我的文章,爲您提供更多信息;) – Sindar 2011-03-17 16:01:03

回答

3

那麼,我能做的最多的事情就是發送一個新的XHR請求到這個URL並檢查返回的狀態。對於錯誤的域名狀態似乎是0,對於404頁是404

background.html

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    if(changeInfo.status == "loading") { 
     var xhr = new XMLHttpRequest(); 
     xhr.open("GET", tab.url, true); 
     xhr.onreadystatechange = function() { 
      if (xhr.readyState == 4) { 
       if(xhr.status == 0) { 
        console.log("wrong domain:", tab.url); 
       } else if(xhr.status == 404) { 
        console.log("404 page:", tab.url); 
       } else if(xhr.status == 200) { 
        console.log("regular page:", tab.url); 
       } 
      } 
     } 
     xhr.send(); 
    } 
}); 
+0

不是一個壞主意,但我需要把它作爲一個事件是不是? 我如何創建一個事件來檢查我何時更改網頁? – Sindar 2011-03-17 17:04:23

+0

@Sindar好吧,我把它放在'chrome.tabs.onUpdated'裏面 – serg 2011-03-17 17:12:53

+0

如果我想在出現錯誤時進行重定向,我需要在xhr.send()之前更改tab.url? – Sindar 2011-03-17 17:47:06