DC_auto_refresh

Refresh the window if no network activity for a while.

目前为 2015-07-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name DC_auto_refresh
  3. // @author Ladoria
  4. // @version 0.5
  5. // @grant none
  6. // @description Refresh the window if no network activity for a while.
  7. // @match http://www.dreadcast.net/Main
  8. // @copyright 2015+, Ladoria
  9. // @namespace InGame
  10. // ==/UserScript==
  11.  
  12. var last_refresh = new Date();
  13. var request_attempts = 0;
  14. var attempts_limit = 1;
  15. var refresh_time_limit = 120000; // In ms. Time limit to refresh again.
  16. var abort_time_fixed = refresh_time_limit / 1000;
  17. var abort_time = abort_time_fixed;
  18. var workingInterval = undefined;
  19. var pause_script = false;
  20.  
  21. var alert_messages = new Array()
  22. 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>';
  23. alert_messages.unconnected = 'Attention, vous semblez toujours déconnecté.'
  24. alert_messages.connected = 'Vous semblez avoir été reconnecté.&nbsp;&nbsp;<div class="btnTxt hide"><div>OK</div></div>'
  25.  
  26. $(document).ready( function() {
  27. $('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>');
  28. $("body").append('<div id="auto_refresh"><div class="alert fakeToolTip"></div></div>');
  29. function show_alert(message) {
  30. $('#auto_refresh .alert').html(alert_messages[message]);
  31. if('ask_to_reload' == message) {
  32. abort_time = abort_time_fixed;
  33. $('#auto_refresh .seconds').html(abort_time);
  34. refresh_abort_time();
  35. // Abort reload
  36. // User want to abort, then notify user and pause script
  37. $('#auto_refresh .abort').on('click', function() {
  38. abort_reload();
  39. pause_script = true;
  40. show_alert('unconnected');
  41. });
  42. // Do reload
  43. $('#auto_refresh .reload').on('click', function() {
  44. do_reload();
  45. });
  46. }
  47. else if ('connected' == message) {
  48. // hide alert
  49. $('#auto_refresh .hide').on('click', function() {
  50. hide_alert();
  51. });
  52. }
  53. $('#auto_refresh').show();
  54. }
  55. function hide_alert() {
  56. $('#auto_refresh').hide();
  57. }
  58. function refresh_abort_time() {
  59. $('#auto_refresh .seconds').html(abort_time);
  60. workingInterval = setInterval( function() {
  61. if(0 < abort_time)
  62. abort_time--;
  63. $('#auto_refresh .seconds').html(abort_time);
  64. }, 1000);
  65. }
  66. // Stop countdown
  67. function abort_reload() {
  68. clearInterval(workingInterval);
  69. request_attempts = 0;
  70. }
  71. // Reload window
  72. function do_reload() {
  73. // Useless, but lovely
  74. last_refresh = new Date();
  75. request_attempts = 0;
  76. window.location.reload();
  77. }
  78.  
  79. // Countdown to reload window
  80. setInterval(function() {
  81. if(true == pause_script) return;
  82. var date_now = new Date().getTime();
  83. // If no network activity for a while, reload.
  84. if (request_attempts >= attempts_limit) {
  85. // If no alert visible, show it
  86. if($('#auto_refresh').is(":hidden")) {
  87. show_alert('ask_to_reload');
  88. // Delay to refresh
  89. last_refresh = new Date();
  90. }
  91. // If reload failed, waiting for a while and after delay to refresh
  92. if (date_now - last_refresh.getTime() >= refresh_time_limit) {
  93. do_reload();
  94. }
  95. }
  96. }, 1000);
  97. // Update request attempt number
  98. $(document).ajaxComplete( function(a,b,c) {
  99. if(/Check/.test(c.url)) {
  100. // If request succeeded
  101. if(b.readyState == 4) {
  102. request_attempts = 0;
  103. // If connected again, resume script and notify
  104. if(true == pause_script) {
  105. pause_script = false;
  106. show_alert('connected');
  107. abort_reload();
  108. }
  109. }
  110. else
  111. request_attempts++;
  112. }
  113. });
  114. });
  115. console.log('DC - Auto Refresh started');