title: Is there a way to check internet connection within Chrome extension?
body: <p>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. </p>
  
  <p>I tried this method from <a href="https://stackoverflow.com/questions/2384167/check-if-internet-connection-exists-with-javascript">Check if Internet Connection Exists with Javascript?</a></p>
  
  <pre><code>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 &gt;= 200 &amp;&amp; (xhr.status &lt; 300 || xhr.status === 304));
      } catch (error) {
          return false;
      }
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>"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."</p>
  
  <p>navigator.onLine does not work either because it always return true.</p>
  
  <p>Thanks!</p>