Chrome OS, quickly tell me if there is an Internet connection or not through the Chrome extension?

I am trying to write an extension that will cache the contents of the page for offline reading. If the user activates the extension popup offline, I would like to show the contents in the cache. Currently, I think I can make an ajax request and wait if it doesn’t work, but if there is a part of the chrome API that will allow me to do it faster, it will be perfect.

I made some kind of search engine and did not meet anything.

+6
source share
4 answers
if (navigator.onLine) { // Online } else { // Offline } 

It also supports event listeners.

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

+16
source

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

+3
source

navigator.onLine reports correctly in most cases, but I found this to be wrong if you have to turn off Wi-Fi, but you have an Ethernet PoE cable connected to your NIC port. Despite the fact that the phone can be offline, navigator.onLine reports that the browser is actually on the network.

+3
source

Sounds like navigator.Online checks the network connection, not necessarily an Internet connection. If you want to confirm that the user has access to the Internet, I think you can use this parameter in conjunction with the periodic or on-demand Ajax request to Google or another reliable site and monitor the response to determine if the user has a stable Internet connection.

+3
source

Source: https://habr.com/ru/post/909486/


All Articles