DC_auto_refresh

Refresh the window if no network activity for a while.

As of 18. 07. 2015. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name		DC_auto_refresh
// @author		Ladoria
// @version		0.9
// @grant       none
// @description	Refresh the window if no network activity for a while.
// @match		http://www.dreadcast.net/Main
// @copyright	2015+, Ladoria
// @namespace InGame
// ==/UserScript==

var reload_asked = new Date();
var request_attempts = 0;
var attempts_limit = 3;
var refresh_time_limit = 120000; // In ms. Time limit to refresh again.
var abort_time_fixed = refresh_time_limit / 1000;
var abort_time = abort_time_fixed;
var workingInterval = undefined;
var pause_script = false;

var alert_messages = new Array()
alert_messages.ask_to_reload = 'Attention, vous semblez avoir été déconnecté. Rechargement de la page dans <span class="seconds"></span> seconde(s) <br><div class="btnTxt abort"><div>Annuler</div></div>&nbsp;&nbsp;<div class="btnTxt reload"><div>Recharger</div></div>';
alert_messages.unconnected = 'Attention, vous semblez toujours déconnecté.'
alert_messages.connected = '<span class="ok_message">Vous semblez avoir été reconnecté.</span>&nbsp;&nbsp;<div class="btnTxt hide"><div>OK</div></div>'

$(document).ready( function() {
	$('head').append('<style>#auto_refresh  {display: none;position: absolute;top: 31px;width: 100%;z-index:1000000;text-align: center;}#auto_refresh .alert  {display: inline-block;padding: 5px;color: red;background-color: white;text-align: center;font-size: 20px;position: relative;}#auto_refresh .alert .abort, #auto_refresh .alert .reload, #auto_refresh .alert .hide  {display: inline-block;margin-top: 10px;width: 100px;}#auto_refresh .ok_message{color: green;}</style>');
	$("body").append('<div id="auto_refresh"><div class="alert fakeToolTip"></div></div>');
	
	function show_alert(message) {
		$('#auto_refresh .alert').html(alert_messages[message]);
		
		if('ask_to_reload' == message) {
			abort_time = abort_time_fixed;
			
			$('#auto_refresh .seconds').html(abort_time);
			refresh_abort_time();
			
			// Abort reload
			// User want to abort, then notify user and pause script
			$('#auto_refresh .abort').on('click', function() {				
				abort_reload();
		
				pause_script = true;
				
				show_alert('unconnected');
			});
			
			// Do reload
			$('#auto_refresh .reload').on('click', function() {
				do_reload();
			});
		}
		else if ('connected' == message) {
			// hide alert
			$('#auto_refresh .hide').on('click', function() {
				hide_alert();
			});
		}
		
		$('#auto_refresh').show();
	}
	
	function hide_alert() {
		$('#auto_refresh').hide();
	}
	
	function refresh_abort_time() {
		$('#auto_refresh .seconds').html(abort_time);
		
		workingInterval = setInterval( function() {
			if(0 < abort_time)
				abort_time--;
			
			$('#auto_refresh .seconds').html(abort_time);
		}, 1000);
	}
	
	// Stop countdown
	function abort_reload() {
		clearInterval(workingInterval);
		
		request_attempts = 0;
	}
	
	// Reload window
	function do_reload() {
		// Useless, but lovely
		last_refresh = new Date();
		request_attempts = 0;
		
		window.location.reload();
	}

	// Countdown to reload window
	setInterval(function() {
		if(true == pause_script) return;
	
		var date_now = new Date().getTime();
		
		// If no network activity for a while, reload.
		if (request_attempts >= attempts_limit) {
			// If no yes/no alert visible, show it
			if(0 == $('#auto_refresh .abort').length) {
				show_alert('ask_to_reload');
				
				// Delay to refresh
				reload_asked = new Date();
			}
				
			// If reload failed, waiting for a while and after delay to refresh
			if (date_now - reload_asked.getTime() >= refresh_time_limit) {
				
				do_reload();
			}
		}
	}, 1000);
	
	// Update request attempt number
	$(document).ajaxComplete( function(a,b,c) {
		if(/Check/.test(c.url)) {
			// If request succeeded
			if(b.readyState == 4) {
				// If reconnected after lost connexion
				// If connected again, resume script and notify
				if(attempts_limit <= request_attempts || true == pause_script) {
					pause_script = false;
					show_alert('connected');
					
					abort_reload();
				}
					
				request_attempts = 0;
			}
			else
				request_attempts++;
		}
	});
});
console.log('DC - Auto Refresh started');