Extract DNS error and 404 error with chrome extension

Well, I am currently developing a Google Chrome extension, and I need to get all the DNS error and 404 to do the redirection. The problem is that I really don't see how this is possible ...

If this is a domain error, I want to get the domain name, and for 404 error I want to get the name page.

Example:

Bad domain: http://www.justforthetest.com/ => Extract justfortetest

404 Error: http://www.valeriemates.com/professinal.html => Get professinal

Hope someone can help me ... Thanks in advance!

+4
source share
1 answer

Well, the most I could do was send a new XHR request to this URL and check the returned status. For invalid domains, the status seems to be 0 , for 404 pages it is 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(); } }); 
+3
source

Source: https://habr.com/ru/post/1344127/


All Articles