DC_auto_refresh

Refresh the window if no network activity for a while.

目前為 2015-07-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name		DC_auto_refresh
// @author		Ladoria
// @version		0.8
// @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 alert visible, show it
			if($('#auto_refresh').is(":hidden")) {
				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');