javascriptgoogle-chrome-extensiongoogle-chrome-osnetwork-connection

Chrome OS, quickly tell if there is an internet connection or not via a Chrome extension?


I am trying to write an extension that will cache page content for offline reading. If the user activates the extension's popup while offline, I would like to show the cached content. Currently, I am thinking I can make an ajax request and wait to see if it fails, but if there is a part of the chrome API that would let me do this more quickly, that would be ideal.

I have done some googling and haven't come across anything.


Solution

  • if (navigator.onLine) {
      // Online
    } else {
      // Offline
    }
    

    It also supports event listeners.

    https://developer.mozilla.org/en/Online_and_offline_events