5

I want to make an userscript (right now I am only worried about Chrome compatibility) reload the page every 30 seconds (I mean, that's not the point, I just want to somehow execute some script while the internet is offline), in case the internet goes down and the user is not here to see it.

Now, the problem is, as soon as the internet goes down the page will be redirected to "This webpage is not available" and all userscripts simply stop working.

I already tried to add @include * to the metadata, hoping that this would allow the userscript to work on said page, however I had no success.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Aloc1234
  • 155
  • 2
  • 6
  • I don't understand what your problem is. Do you need to detect when a user loses Internet connectivity, or are you having problems doing the redirection? – Halcyon Feb 10 '13 at 14:03
  • Have you tried wrapping the page in an iframe? – flacs Feb 10 '13 at 14:07
  • Your scripts stays even when the iframe doesn't load successfully. – flacs Feb 10 '13 at 14:13
  • Yes, I need both to detect when a user loses internet connectivity and to run a certain script (COULD be a page reload every 30 seconds) when that happens. The problem is that, as soon as internet goes down Tampermonkey(Chrome's extensison for running userscripts) simply stop executing userscripts. – Aloc1234 Feb 10 '13 at 14:14
  • I didn't know what an iframe was, I'm going to research on that and hopefully try to apply in my case, thanks ! – Aloc1234 Feb 10 '13 at 14:15
  • @Aloc1234 have you ever found a reliable solution for this problem? – Jonas Sourlier Nov 06 '19 at 10:09

2 Answers2

2

Chrome's "This webpage is not available" display is not a standard page with a "Greaseable" scheme. This means that Tampermonkey scripts and Chrome userscripts will not fire for it. You would have to make a full-fledged Chrome extension. (More details at this question.)

There are some workarounds:

(1) You could switch your DNS provider to one like OpenDNS. OpenDNS hijacks DNS resolution errors and serves up a spammy "search" page instead. (The page is guide.opendns.com, IIRC.)

That search page is greaseable by Tampermonkey. Your script could search for OpenDNS' "unavailable" messages and know that the site was offline.

(2) Use AJAX to poll the page headers and only reload when the page is both up and online.

Use GM_xmlhttpRequest() (or jQuery) to make a head request. If the request errors-out, then the site is offline, don't reload the page.

Likewise, if the Last-Modified header is the same as when you last checked, then there is no point in reloading the page (usually).

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • The DNS solution is great and would work perfectly, the problem is, it's for various users so it's not possible. However, I am definitelly going the Chrome extension path, it seems the most sensible and practical approach. Thanks for the help. – Aloc1234 Feb 12 '13 at 12:16
0

As mentioned in the comments:

Wrap the page in an <iframe> and execute the userscript in the parent. Once the connection goes down, Chrome should continue to execute the Userscript in the parent because it is still loaded unlike the iframe which will fail to load as soon as you refresh it. Please note that you might get some same-origin-policy difficulties: afaik you won't be able to notice that the iframe failed to load.

flacs
  • 3,913
  • 4
  • 19
  • 20
  • Quite honestly I haven't been able to apply your solution so far -- I can create iframes, but how would I wrap the whole page in an iframe? – Aloc1234 Feb 10 '13 at 16:33
  • i used the iframe trick for monitoring Printers at work. a simple page with 10 iframes, each loading a different printer status page. each iframe was set to refresh itself every 10 sec. worked a charm – RozzA Mar 16 '14 at 02:31