您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Refresh the window if no network activity for a while.
当前为
- // ==UserScript==
- // @name DC_auto_refresh
- // @author Ladoria
- // @version 0.5
- // @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 last_refresh = new Date();
- var request_attempts = 0;
- var attempts_limit = 1;
- 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> <div class="btnTxt reload"><div>Recharger</div></div>';
- alert_messages.unconnected = 'Attention, vous semblez toujours déconnecté.'
- alert_messages.connected = 'Vous semblez avoir été reconnecté. <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;}</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
- last_refresh = new Date();
- }
- // If reload failed, waiting for a while and after delay to refresh
- if (date_now - last_refresh.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) {
- request_attempts = 0;
- // If connected again, resume script and notify
- if(true == pause_script) {
- pause_script = false;
- show_alert('connected');
- abort_reload();
- }
- }
- else
- request_attempts++;
- }
- });
- });
- console.log('DC - Auto Refresh started');