Search code examples
javascriptgoogle-chromegreasemonkeyuserscriptstampermonkey

How to keep my userscript working, even when the site goes offline?


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.


Solution

  • 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).