4

好吧,我目前正在开发一个 Google Chrome 扩展程序,我需要获取所有 DNS 和 404 错误来进行重定向。问题只是我真的不明白这怎么可能......

如果是域错误,我想获取域名,对于 404 错误,我想获取名称页面。

例子 :

坏域:http : //www.justforthetest.com/ => 获取 justforthetest

404 错误:http ://www.valeriemates.com/professinal.html => 获取专业

希望有人可以为我提供一些帮助...在此先感谢!

4

1 回答 1

3

好吧,我能做的最多就是向这个 URL 发送一个新的 XHR 请求并检查返回的状态。对于错误的域状态似乎是0,对于 404 页面它是404

背景.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();
    }
}); 
于 2011-03-17T16:57:47.290 回答