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

4

6

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.

Maltz answered 27/2, 2012 at 21:9 Comment(0)
S
19
if (navigator.onLine) {
  // Online
} else {
  // Offline
}

It also supports event listeners.

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

Salted answered 27/2, 2012 at 21:15 Comment(2)
navigator.onLine is not correctly reflect the change immediately if network status changePongid
navigator.onLine is very unreliable. See #14283624Groth
D
3

Tried navigator.onLine ? I read it's unreliable but I just did a test (disconnecting from WiFi) and it worked.

Dedifferentiation answered 27/2, 2012 at 21:14 Comment(0)
L
3

navigator.onLine reports correctly in most instances, but one I found where it is incorrect is if you were to disable WiFi, but you have a PoE ethernet cable plugged into your NIC port. Even though the phone may be offline, navigator.onLine reports that the browser is actually online.

Leucocratic answered 19/9, 2012 at 0:48 Comment(0)
R
3

Sounds like navigator.Online checks for a network connection, not necessarily an internet connection. If you want to confirm the user has access to the internet, I think you can use this option in conjunction with a periodic or on demand Ajax request to google or some other reliable site and monitor the response to determine if the user has a stable internet connection.

Rhubarb answered 4/4, 2014 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.