I’m currently making a chrome extension, and want to have a status bar showing if the internet is connected. The network service at our school requires users to log on the firewall to use the internet.
I tried this method from Check if Internet Connection Exists with Javascript?
function hostReachable() {
// Handle IE and more capable browsers
var xhr = new(window.ActiveXObject || XMLHttpRequest)("Microsoft.XMLHTTP");
var status;
// Open new request as a HEAD to the root hostname with a random param to bust the cache
xhr.open("HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false);
// Issue request and handle response
try {
xhr.send();
return (xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304));
} catch (error) {
return false;
}
}
This function works if I declare it in the chrome console, but the console gives me this error if I put the function in the script of the extension.
"Access to XMLHttpRequest at 'file:///?rand=93870' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."
navigator.onLine does not work either because it always return true.
Thanks!