4

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!

Tommy Li
  • 41
  • 2
  • _"navigator.onLine does not work either because it always return true."_ Even when not online? See [Jquery load() only working in firefox?](https://stackoverflow.com/questions/32996001/jquery-load-only-working-in-firefox/), [Read local XML with JS ](https://stackoverflow.com/questions/41279589/read-local-xml-with-js/) – guest271314 Feb 01 '19 at 18:51
  • navigator.onLine is known to be buggy – wOxxOm Feb 01 '19 at 18:53
  • That Javascript looks like a bit of a hack. See the Gist where it is published for workarounds and complaints: https://gist.github.com/jpsilvashy/5725579 –  Feb 01 '19 at 18:54
  • @TommyLi, you might want to replace window.location.hostname with something like `1.1.1.1` or `8.8.8.8` or `google.com` or anything external. See also [How to detect network state changes in a Chrome extension](//stackoverflow.com/a/42334842) – wOxxOm Feb 01 '19 at 18:55
  • The CORS Policy will error anytime you try to ajax or reach out from Chrome on your local machine. You can read more on local Chrome Cors Disable https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome. Checking network connection with Javascript means you would have to reach out to some file that lived off of the local machine to run. Will it live within an intranet, and be pulled to check the connection to the internet? – HeroZero Feb 01 '19 at 19:02
  • Hi guys thanks for your help. In this case, Chrome and the laptop themselves are connected to the network, but they cannot access the actual internet without logging in the firewall. Thus navigator.onLine will always return true because Chrome "technically" is connected to the network. – Tommy Li Feb 02 '19 at 19:39
  • @TommyLi Chrome will always return `true` for `navigator.onLine` even if your computer is NOT connected to any network. I would suggest using an AJAX request to an external website which is known to be always up. You could use `google.com`, but you can't guarantee their uptime. You can, however, guarantee the uptime of your ISP (you wouldn't have internet if they were down), however, most APIs (ipinfo.io) require you to make a purchase to obtain the domain of the ISP and the free plan would only give you the name of the ISP. I'm still stuck depending on `google.com` – undefined Oct 30 '19 at 22:36
  • If you send an AJAX request to your ISP servers, a) the connection is successful => there is an internet connection, or b) connection failed => ISP servers are down -> ISP is down -> your internet is also down – undefined Oct 30 '19 at 22:39

0 Answers0