Refresh Unavailable

Auto refresh when pages are unavailable

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Refresh Unavailable
// @namespace   iFantz7E.RefreshUnavailable
// @description Auto refresh when pages are unavailable
// @include     *
// @version     1.05
// @grant       none
// @run-at      document-start
// @copyright   2016, 7-elephant
// ==/UserScript==

(function ()
{	
	"use strict";
	// jshint multistr:true
	
function attachOnLoad(callback)
{
	window.addEventListener("load", function (e) 
	{
		callback();
	});
}

function attachOnReady(callback) 
{
	document.addEventListener("DOMContentLoaded", function (e) 
	{
		callback();
	});
}

function reload()
{
	window.location.reload();
}

function ready()
{
	var isAvailable = true;
	var reloadTime = 3000;
	
	if (document.body)
	{
		var child = document.body.firstChild;
		if (child && child.nodeType === 3 && child.textContent === "Service unavailable")
		{
			isAvailable = false;
		}
	}
	
	if (isAvailable)
	{
		if (document.title === "503 Service Temporarily Unavailable")
		{
			isAvailable = false;
			reloadTime = 30000;
		}
	}
	
	if (isAvailable)
	{
		if (document.title === "Network Error")
		{
			var eleBig = document.querySelector("body > blockquote:nth-child(2) > table:nth-child(1) "
				+ " > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) "
				+ " > font:nth-child(1) > big:nth-child(1)");
				
			if (eleBig && eleBig.textContent.trim() === "Network Error (tcp_error)")
			{
				isAvailable = false;
			}
		}
	}
	
	if (isAvailable)
	{
		var eleErr = document.querySelector("body > center:nth-child(1) > h1:nth-child(1)");
		if (eleErr && eleErr.textContent.trim() === "502 Bad Gateway")
		{
			isAvailable = false;
		}
	}
	
	if (isAvailable)
	{
		// Cloudflare
		var eleErr = document.querySelector(".cf-error-header-desc > h4");
		if (eleErr && eleErr.textContent.trim() === "Website is offline")
		{
			isAvailable = false;
			reloadTime = 10000;
		}
		
		if (isAvailable)
		{
			eleErr = document.querySelector(".cf-error-type");
			if (eleErr && eleErr.textContent.trim() === "Error")
			{
				isAvailable = false;
				reloadTime = 10000;
			}
		}
	}
	
	if (!isAvailable)
	{
		console.log("Autorefresh: Service unavailable in " + (reloadTime / 1000) + "s");
		setTimeout(reload, reloadTime);
	}
}

attachOnReady(function()
{
	ready();
});

})();

// End