Refresher

Automatically reload the page at ?interval=

  1. // ==UserScript==
  2. // @name Refresher
  3. // @namespace none
  4. // @version 2020.07.16.1823
  5. // @description Automatically reload the page at ?interval=
  6. // @author technical13
  7. // @supportURL https://Discord.me/TheShoeStore
  8. // @match https://www.munzee.com/m/*
  9. // @grant none
  10. // ==/UserScript==
  11. // jshint esversion: 6
  12.  
  13. var isDebug = false;
  14. var intVerbosity = 0;
  15. const ver = '2020.07.16.1823';
  16. const scriptName = 'Refresher v' + ver;
  17.  
  18. function toBoolean( val ) {
  19. const arrTrue = [ undefined, null, '', true, 'true', 1, '1', 'on', 'yes' ];
  20. val = ( typeof( val ) === 'string' ? val.toLowerCase() : val );
  21.  
  22. log( 4, 'log', 'toBoolean() is returning: %o', ( arrTrue.indexOf( val ) !== -1 ? true : false ) );
  23. return ( arrTrue.indexOf( val ) !== -1 ? true : false );
  24. }
  25. function log( intV, strConsole, strLog, ...arrArgs ) {
  26. if ( strConsole === undefined ) { strConsole = 'log'; }
  27. if ( strLog === undefined ) { strLog = '%o'; }
  28. if ( intVerbosity >= intV && ( strConsole === 'groupEnd' ) ) { console[ strConsole ](); }
  29. if ( intV === 0 || ( isDebug && intVerbosity >= intV ) ) { console[ strConsole ]( '[%i]: %s: ' + strLog, intV, scriptName, ...arrArgs ); }
  30. }
  31. const intParamsStart = ( document.URL.indexOf( '?' ) + 1 );
  32. const strParams = document.URL.substr( intParamsStart );
  33. const arrParamSets = strParams.split( '&' );
  34. var objParams = {};
  35. arrParamSets.forEach( function( strParam ) {
  36. let arrParam = strParam.split( '=' );
  37. let strParamName = ( arrParam[ 0 ].toLowerCase() || '' );
  38. if ( strParamName === 'verbosity' ) {
  39. isDebug = true;
  40. intVerbosity = ( arrParam[ 1 ] ? ( parseInt( arrParam[ 1 ] ) < 0 ? 0 : ( parseInt( arrParam[ 1 ] ) > 9 ? 9 : parseInt( arrParam[ 1 ] ) ) ) : 9 );
  41. log( 2, 'info', 'Found parameter `%s` with a value of: %o', strParamName, objParams[ strParamName ] );
  42. }
  43. else if ( strParamName === 'debug' ) {
  44. isDebug = toBoolean( arrParam[ 1 ] );
  45. intVerbosity = 1;
  46. log( 2, 'info', 'Found parameter `%s` with a value of: %o', strParamName, objParams[ strParamName ] );
  47. }
  48. else if ( strParamName === 'refresh' || strParamName === 'interval' ) {
  49. objParams.interval = parseInt( arrParam[ 1 ] );
  50. log( 2, 'info', 'Found parameter `%s` with a value of: %o', strParamName, objParams.interval );
  51. }
  52. else {
  53. log( 2, 'info', 'Found parameter `%s` with a value of: %o', strParamName, arrParam[ 1 ] );
  54. }
  55. } );
  56.  
  57. log( 1, 'warn', 'Debug mode is on with verbosity level: %o', intVerbosity );
  58. log( 1, 'groupCollapsed', 'Verbosity options: (click to expand)' );
  59. log( 1, 'log', '1) Summary\n2) Parameters retrieved from URL\n3) Variables set to objParams\n4) Function returns\n9) ALL debugging info and this notice.' );
  60. log( 1, 'groupEnd' );
  61.  
  62. function createFogOfWar( options = {} ) {
  63. if ( !options.color ) { options.color = '#000000'; }
  64. if ( !options.opacity ) { options.opacity = '67%'; }
  65. if ( !options.zIndex && !options[ 'z-index' ] ) { options.zIndex = '100'; }
  66.  
  67. var fogOfWar = document.createElement( 'div' );
  68. fogOfWar.id = 'fog-of-war';
  69. fogOfWar.style.width = '100%';
  70. fogOfWar.style.height = '100%';
  71. fogOfWar.style.position = 'fixed';
  72. fogOfWar.style.top = '0px';
  73. fogOfWar.style.backgroundColor = options.color;
  74. fogOfWar.style.opacity = options.opacity;
  75. fogOfWar.style.zIndex = options.zIndex;
  76. $( 'body' ).append( fogOfWar );
  77. }
  78. function doConfig() {
  79. createFogOfWar();
  80.  
  81. var closeButton = document.createElement( 'button' );
  82. closeButton.style.padding = '2px 6px';
  83. closeButton.classList = 'btn btn-danger';
  84. closeButton.addEventListener( 'click', () => { $( 'div#fog-of-war' ).remove(); configBox.remove(); } );
  85. closeButton.innerText = 'Cancel';
  86.  
  87. var goButton = document.createElement( 'button' );
  88. goButton.style.padding = '2px 6px';
  89. goButton.classList = 'btn green';
  90. goButton.addEventListener( 'click', () => {
  91. if ( window.location.search === '' ) {
  92. window.location.assign( window.location.href + '?interval=' + getInterval.value );
  93. } else {
  94. window.location.assign( window.location.href.replace( window.location.search, '?interval=' + getInterval.value ) );
  95. }
  96. } );
  97. goButton.innerText = 'Refresh!';
  98.  
  99. var getInterval = document.createElement( 'input' );
  100. getInterval.type = 'number';
  101. getInterval.style.width = '50px';
  102. getInterval.style.color = '#000000';
  103. getInterval.style.textAlign = 'right';
  104. getInterval.style.paddingRight = '2px';
  105. getInterval.min = 3;
  106. getInterval.max = 300;
  107. getInterval.value = 7;
  108.  
  109. var getIntervalBox = document.createElement( 'div' );
  110. getIntervalBox.style.width = '100%';
  111. getIntervalBox.style.padding = '10px';
  112. getIntervalBox.append(
  113. document.createTextNode( 'Refresh page every ' ), getInterval, document.createTextNode( ' seconds ' ),
  114. document.createElement( 'br' ), document.createElement( 'br' ),
  115. goButton, document.createTextNode( ' ' ), closeButton
  116. );
  117.  
  118. var configBox = document.createElement( 'div' );
  119. configBox.style.width = '320px';
  120. configBox.style.top = '20%';
  121. configBox.style.right = '20%';
  122. configBox.style.position = 'fixed';
  123. configBox.style.textAlign = 'center';
  124. configBox.style.fontWeight = 'bold';
  125. configBox.style.fontSize = 'larger';
  126. configBox.style.color = '#71B33C';
  127. configBox.style.backgroundColor = '#000000';
  128. configBox.style.border = '1px solid #FF00FF';
  129. configBox.style.zIndex = '250';
  130.  
  131. configBox.append( getIntervalBox );
  132.  
  133. $( 'body' ).append( configBox );
  134. }
  135.  
  136. function favTitle( pinImage ) {
  137. if ( ( new RegExp( '(skyland|treehouse)' ) ).test( pinImage ) ) {
  138. var intUnicorns = parseInt( pinImage.split( '/' ).pop().split( '.' )[ 0 ].replace( /(skyland|treehouse)/, '0' ) );
  139. $( 'title' ).text( '(' + intUnicorns + ') ' + $( 'title' ).text() );
  140. }
  141. var link = ( document.querySelector( "link[rel*='icon']" ) || document.createElement( 'link' ) );
  142. link.type = 'image/x-icon';
  143. link.rel = 'shortcut icon';
  144. link.href = pinImage;
  145. document.getElementsByTagName( 'head' )[ 0 ].appendChild( link );
  146. }
  147.  
  148. ( function() {
  149. 'use strict';
  150. log( 0, 'info', 'Script loaded.' );
  151.  
  152. favTitle( $( 'img.pin' ).attr( 'src' ) );
  153.  
  154. var reloadButton = document.createElement( 'button' );
  155. reloadButton.classList = 'btn green pull-right';
  156. reloadButton.style.marginBottom = '10px';
  157. reloadButton.title = 'Click to reload now!';
  158. reloadButton.addEventListener( 'click', () => { window.location.reload(); } );
  159.  
  160. if ( $.isNumeric( objParams.interval ) ) {
  161. $( 'p.status-date' )[ 0 ].style.color = '#000000';
  162. $( 'p.status-date' )[ 0 ].style.fontWeight = 'bold';
  163.  
  164. reloadButton.id = 'Refresher-' + objParams.interval;
  165. reloadButton.style.margin = '0px 3px 10px 0px';
  166. reloadButton.innerText = 'Reloading in: ' + ( objParams.interval - 1 ) + ' second' + ( objParams.interval === 1 ? '' : 's' );
  167.  
  168. var stopButton = document.createElement( 'button' );
  169. stopButton.id = 'Refresher-stop';
  170. stopButton.classList = 'btn btn-danger pull-right';
  171. stopButton.style.margin = '0px 0px 10px 3px';
  172. stopButton.title = 'Click to stop reloading!';
  173. stopButton.addEventListener( 'click', () => {
  174. window.location.assign( window.location.href.replace( window.location.search, '' ) );
  175. } );
  176. stopButton.innerText = '×';
  177.  
  178. $( 'div#munzee-name' ).prepend( stopButton, reloadButton );
  179.  
  180. var doReload = objParams.interval;
  181. window.setInterval( () => {
  182. if ( reloadButton.innerText !== 'Reloading now!' ) {
  183. var strLastSecond = reloadButton.innerText.match( /Reloading in: ([\d]+) seconds?/i )[ 1 ];
  184. var intLastSecond = parseInt( strLastSecond );
  185. var intNextSecond = intLastSecond - 1; doReload = intNextSecond;
  186. if ( intNextSecond === 0 ) { reloadButton.innerText = 'Reloading now!'; }
  187. else if ( intNextSecond === 1 ) { reloadButton.innerText = 'Reloading in: 1 second'; }
  188. else { reloadButton.innerText = reloadButton.innerText.replace( strLastSecond, intNextSecond ); }
  189. }
  190. else if ( doReload <= 0 ) { window.location.reload(); }
  191. }, 1000 );
  192. }
  193. else {
  194. reloadButton.style.margin = '0px 0px 10px 3px';
  195. reloadButton.style.color = '#000000';
  196. reloadButton.style.fontWeight = 'bolder';
  197. reloadButton.innerText = '⟳';
  198.  
  199. var configButton = document.createElement( 'button' );
  200. configButton.id = 'Refresher-Config';
  201. configButton.classList = 'btn btn-warning pull-right';
  202. configButton.style.color = '#000000';
  203. configButton.style.fontWeight = 'bold';
  204. configButton.style.margin = '0px 3px 10px 0px';
  205. configButton.title = 'Click to start automatic refresh of this page!';
  206. configButton.addEventListener( 'click', () => { doConfig(); } );
  207. configButton.innerText = 'Refresher!?';
  208.  
  209. $( 'div#munzee-name' ).prepend( reloadButton, configButton );
  210. }
  211. } )();