// ==UserScript==
// @name Cloudflare reloader
// @namespace 713781fc39fef35607e28d8a5e5e632e6946ae0e
// @version 0.7
// @description Automatically reloads on Cloudflare hosted pages on Errors
// @author /u/AyrA_ch
// @include http://*/*
// @include https://*/*
// @grant none
// ==/UserScript==
//Changelog
//
//0.7 - Change URL on reload to avoid POST request
//0.6 - Fix generic error handling
//0.5 - Adding generic error handling
//0.4 - Adding 502 error handling
//0.3 - Adding 524 error handling
//0.2 - Adding comments
//0.1 - Initial version
(function () {
'use strict';
//Number of seconds to wait for reload.
//If you set this too low, it will not work.
//With 20 seconds, cloudflare already tends to give you back the Error page without even trying.
var RELOAD_TIMER = 30;
//Should we update the title bar every 5 seconds
var UPDATE_TITLE = true;
//The element we show the counter in.
var p = document.querySelector("h1");
//Function that does essentially everything
var timer = function (x) {
if (x > 0) {
p.textContent = "Reloading in " + x + " Seconds...";
} else if (x === 0) {
p.textContent = "Reloading now";
var s = location.search;
var r = Math.random() * 10000 | 0;
if (!s || s.length < 2) {
s = "?";
}
//Remove custom reload tag and cloudflare reload info
s = s.substr(1).split('&').filter(function (v) {
return !v.match(/^_reload_[\d.]+=/) &&
!v.match(/^__cf_chl_jschl_tk__=/);
});
s = ["?_reload_" + r + "=" + r].concat(s).join("&");
location.search = s;
//location.reload();
} else {
p.textContent += ".";
}
if (UPDATE_TITLE && x % 5 === 0) {
document.title = p.textContent;
}
//Using .bind() is a cheap way of passing arguments to a function without making a closure
window.setTimeout(timer.bind(this, x - 1), 1000);
};
//Check for common Cloudflare headers
if (document.querySelector("#cf-error-details") &&
document.querySelector(".cf-wrapper") &&
document.querySelector(".cf-error-type") &&
document.querySelector(".cf-error-code")) {
//Just so the user knows what is going on if he opens the console
console.info("Cloudflare reloader element match. Will reload after " + RELOAD_TIMER + " seconds.");
timer(RELOAD_TIMER);
} else {
//I am not sure what cloudflare does if you set your browser language to something different.
//If this becomes an issue, we can instead check for the presence of certain elements in the document.
if (
document.title === "Website is offline | 522: Connection timed out" ||
document.title.indexOf("| 524: A timeout occurred") > 0 ||
document.title.indexOf("| 502: Bad gateway") > 0) {
//Just so the user knows what is going on if he opens the console
console.info("Cloudflare reloader title match. Will reload after " + RELOAD_TIMER + " seconds.");
timer(RELOAD_TIMER);
}
}
})();
/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.
DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/