Checking Internet connection in Chrome / Chromium Plugin; The uploaded image is loaded into the cache

I am trying to check if there is an internet connection inside the chrome plugin. While there is an API access that suggests testing this, it does test if an internet connection is possible, in theory. To get this information, I am trying to upload an image

checkConnection() {
    var newImg = new Image;
    newImg.src = url;
    newImg.onload = function() { ... }
    newImg.onerror = function() { ... }
}

      

I am using the Image object to avoid all these same initial policy issues when using a get request with JavaScript (I am using code from a plugin, so there is no address that has the same origin as my code: /). Basically my code from above works. I have a setTimeout that calls checkConnection every time. However, when the image has been loaded successfully once (on plugin reload), it is stored in the cache and loaded from there when the connection fails.

Do you have an idea how to get around this problem? Or do you know a sane way to test your internet connection inside a Chrome plugin without setting up a server that can carry over the origin of my request?

+3


source to share


1 answer


You have 3 options that I know of ...

Checking your internet connection

After some quick Googling I found navigator.onLine

. It looks like you can use this to test your internet connection, but it's not always completely accurate.

Stop image from caching

If you have control over the domain where the image is hosted , you can set headers to tell the browser not to cache the image.



Cache-Control: no-cache, must-revalidate // HTTP/1.1
Expires: Sat, 26 Jul 1997 05:00:00 GMT // Date in the past

      

If you are not in control of the domain when uploading the image, try adding a GET variable to the URL with a random number or current timestamp.

eg.

https://example.com/image.png?time=1496222683
https://example.com/image.png?time=1496223323
https://example.com/image.png?time=1496222313

      

This can trick the browser into thinking that you are requesting a new resource.

+3


source







All Articles