Refresh Unavailable

Auto refresh when pages are unavailable

  1. // ==UserScript==
  2. // @name Refresh Unavailable
  3. // @namespace iFantz7E.RefreshUnavailable
  4. // @description Auto refresh when pages are unavailable
  5. // @include *
  6. // @version 1.05
  7. // @grant none
  8. // @run-at document-start
  9. // @copyright 2016, 7-elephant
  10. // ==/UserScript==
  11.  
  12. (function ()
  13. {
  14. "use strict";
  15. // jshint multistr:true
  16. function attachOnLoad(callback)
  17. {
  18. window.addEventListener("load", function (e)
  19. {
  20. callback();
  21. });
  22. }
  23.  
  24. function attachOnReady(callback)
  25. {
  26. document.addEventListener("DOMContentLoaded", function (e)
  27. {
  28. callback();
  29. });
  30. }
  31.  
  32. function reload()
  33. {
  34. window.location.reload();
  35. }
  36.  
  37. function ready()
  38. {
  39. var isAvailable = true;
  40. var reloadTime = 3000;
  41. if (document.body)
  42. {
  43. var child = document.body.firstChild;
  44. if (child && child.nodeType === 3 && child.textContent === "Service unavailable")
  45. {
  46. isAvailable = false;
  47. }
  48. }
  49. if (isAvailable)
  50. {
  51. if (document.title === "503 Service Temporarily Unavailable")
  52. {
  53. isAvailable = false;
  54. reloadTime = 30000;
  55. }
  56. }
  57. if (isAvailable)
  58. {
  59. if (document.title === "Network Error")
  60. {
  61. var eleBig = document.querySelector("body > blockquote:nth-child(2) > table:nth-child(1) "
  62. + " > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) "
  63. + " > font:nth-child(1) > big:nth-child(1)");
  64. if (eleBig && eleBig.textContent.trim() === "Network Error (tcp_error)")
  65. {
  66. isAvailable = false;
  67. }
  68. }
  69. }
  70. if (isAvailable)
  71. {
  72. var eleErr = document.querySelector("body > center:nth-child(1) > h1:nth-child(1)");
  73. if (eleErr && eleErr.textContent.trim() === "502 Bad Gateway")
  74. {
  75. isAvailable = false;
  76. }
  77. }
  78. if (isAvailable)
  79. {
  80. // Cloudflare
  81. var eleErr = document.querySelector(".cf-error-header-desc > h4");
  82. if (eleErr && eleErr.textContent.trim() === "Website is offline")
  83. {
  84. isAvailable = false;
  85. reloadTime = 10000;
  86. }
  87. if (isAvailable)
  88. {
  89. eleErr = document.querySelector(".cf-error-type");
  90. if (eleErr && eleErr.textContent.trim() === "Error")
  91. {
  92. isAvailable = false;
  93. reloadTime = 10000;
  94. }
  95. }
  96. }
  97. if (!isAvailable)
  98. {
  99. console.log("Autorefresh: Service unavailable in " + (reloadTime / 1000) + "s");
  100. setTimeout(reload, reloadTime);
  101. }
  102. }
  103.  
  104. attachOnReady(function()
  105. {
  106. ready();
  107. });
  108.  
  109. })();
  110.  
  111. // End