Fetch DNS error and Error 404 with a chrome extension - javascript

Well, i'm currently developping a Google Chrome extension and i need to fetch all the DNS and 404 error to make a redirection. The problem is simply that i really don't see how it's possible...
If it's a domain error i want to fetch the domain name and for a 404 error i want to fetch the name page.
Example :
Bad Domain : http://www.justforthetest.com/ => Fetch justforthetest
404 Error : http://www.valeriemates.com/professinal.html => Fetch professinal
Hope someone could provide me some helps...
Thanks in advance !

Well, the most I was able to do is to send a new XHR request to this URL and check returned status. For wrong domains 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();
}
});

Related

Redirection after a HEAD xhr

I want to reload the current page with javascript if a specific url is doing a redirection.I don't need to know the redirection url.The xhr request is being performed and i do get 302 redirect when i inspect the request but my specific redirection isn't happening.
var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'https://www.filterbypass.me/s.php?k=BbrSU%2F%2F39m5gKhLDr31ZqjKYgrAdALscySz%2B6YarDYTH5G9hSDc%2B&b=4&f=norefer');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 301 || xhr.status == 302) {
//url expired , reload
console.log('perform redirection');
window.location.replace('https://www.google.com');
}
}
};
xhr.send();
I don't see the console.log and i already tried with xhr.readyState == 2.

How can I get the substatus code of the exception with java script? like 500.1

I would like to read the substatus code of the exception like 500.1 on the client side. How to achieve this?
here is my code on the server side:
if(string.IsNullOrEmpty(userEmail)) {
Response.StatusCode = 500;
Response.SubStatusCode = 1;
Response.StatusDescription = "Email fehlt";
return;
}
Client side:
if (xhr.status == 500 && thrownError.indexOf("Email") > -1) {
alert('Email is missing...');
}
else {
alert('Error...');
}
On client side you can use the getAllResponseHeaders() method of XMLHttpRequest.
If a correct http header is really sent, it will read it.
Example:
var request = new XMLHttpRequest();
request.open("GET", "ajax.php", true);
request.send();
request.onreadystatechange = function() {
if(this.readyState == this.HEADERS_RECEIVED) {
console.log(this.getAllResponseHeaders());
}
}
Output is separated by "\r\n".
If somehow the substatus cannot go through, maybe you can use another http header field for your goal, even if it is not according to the standards...

xmlHTTP request error: "failed"

var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','http://example.com/',true);
xmlHTTP.send();
I just keep getting XHR failed loading: GET, can anyone help???
Thanks
If the server your contacting doesn't have CORS set in the header (e.g. access-control-allow-origin: *), you will not be able to make the request. If you don't have access to the server to set a CORS header, you'll need to contact the server from a server you do control and then pass that to the browser (either with a CORS header or not if it's served from the same domain)
But your code works fine. The problem is with your http://example.com/ not returning
http://jsbin.com/zuhobidako/edit?html,js,console,output
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','https://jsonplaceholder.typicode.com',true);
xmlHTTP.send();
xmlHTTP.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.body.innerHTML =
this.responseText;
}
};
For server status errors, add this to onreadystatechange
if( this.status > 299 && this.readyState == 4) {
console.log('Server Error: ' + xmlHTTP.statusText);
}
For xmlHTTP code errors...
xmlHTTP.onerror = function() {
console.log('xmlHTTP Error', xmlHTTP.responseText)
}

Adding Request Headers on JavaScript

I am preparing a JavaScript. Below is the code for the same:
<html>
<head>
<body>
<script>
var getJSON = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined'
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.onreadystatechange = function() {
var status;
var data;
if (xhr.readyState == 4) {
status = xhr.status;
if (status == 200) {
data = JSON.parse(xhr.responseText);
successHandler && successHandler(data);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
getJSON('https://example.com/lol.json', function(data) {
alert('Your Token is: ' + data.token);
}, function(status) {
alert('Something went wrong.');
});
</script>
</body>
</head>
</html>
So, this snipped is without "access-control-allow-origin" and I am running this locally, so I have used file:/// by disabling the security features of chrome.
chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
I need to run this one without disabling the security features means by adding the request headers like:
res.setHeader('Access-Control-Allow-Origin', "http://"+req.headers.host+':8000');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
next();
Actually, I am not too good in JavaScript and messing with the same since morning. Can Any one help me regarding this that how I merge these request headers in the snippet.
You will have to update the startup of the chrome by using the following :
-disable-web-security -user-data-dir
You can add this to response header in your server side:
file://

WFS-T call to Geoserver using dojo.request.xhr

I am using dojo.request.xhr to make a post reqeust to geoserver.
But the problem is when i'm using XMLHttpRequest to make the post request , it is working fine on the other hand when i'm using dojo.reqeust.xhr for the same thing its giving the following error.
*
org.xmlpull.v1.XmlPullParserException: only whitespace content allowed
before start tag and not i (position: START_DOCUMENT seen i... #1:1)
only whitespace content allowed before start tag and not i (position:
START_DOCUMENT seen i... #1:1)
*
dojo.request.xhr code -
xhr(url, {
handleAs : "xml",
data : postData,
method : "POST",
headers : {
'Content-Type' : 'text/xml',
},
}).then(function(data){
console.log(data);
}, function(err){
console.log("Error : " + err);
});
above code is not working and giving the above mentioned error.
This is same post request using the XMLHttpRequest :-
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader('Content-type', 'text/xml');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
alert("Error");
return;
}
var xml = req.responseXML;
console.log(xml);
}
if (req.readyState == 4) return;
req.send(postData);
To check the XML(that i'm sending as postData) whether it is valid or not i used GeoServer's demo request tool to build the WFS request and its working fine.
UPDATE :- This is the link of XML file that i'm sending as a postData.
post_XML_File
Can anyone have any idea what i'm doing wrong?? Thanks in advance.

Categories