好吧,我目前正在开发 Google Chrome 扩展程序,我需要获取所有 DNS 和 404 错误以进行重定向。问题很简单,我真的不明白这怎么可能......
如果是域错误,我想获取域名,如果是 404 错误,我想获取名称页面。
示例:
错误域:http://www.justforthetest.com/ => 只为测试获取
404 错误:http://www.valeriemates.com/professinal.html => 获取专业人士
希望有人能给我一些帮助... 提前致谢!
最佳答案
嗯,我能做的最多就是向这个 URL 发送一个新的 XHR 请求并检查返回的状态。对于错误的域,状态似乎是 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();
}
});
关于javascript - 使用 chrome 扩展获取 DNS 错误和错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5341452/