MHQ ago

Show timestamps

当前为 2018-11-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MHQ ago
  3. // @namespace none
  4. // @version 2018.11.27.2109
  5. // @description Show timestamps
  6. // @supportURL https://Discord.me/TheShoeStore
  7. // @author technical13
  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 = '2018.11.27.2109';
  16. const scriptName = 'MHQ ago v' + ver;
  17.  
  18. function log( intV, strConsole, strLog, ...arrArgs ) {
  19. if ( strConsole === undefined ) { strConsole = 'log'; }
  20. if ( strLog === undefined ) { strLog = '%o'; }
  21. if ( intVerbosity >= intV && ( strConsole === 'groupEnd' ) ) { console[ strConsole ](); }
  22. if ( intV === 0 || ( isDebug && intVerbosity >= intV ) ) { console[ strConsole ]( '[%i]: %s: ' + strLog, intV, scriptName, ...arrArgs ); }
  23. }
  24.  
  25. function countDown( intRawSeconds ) {
  26. var intSeconds = parseInt( intRawSeconds );
  27. var intHours = Math.floor( intSeconds / 3600 );
  28. intSeconds = intSeconds - ( intHours * 3600 );
  29. var intMinutes = Math.floor( intSeconds / 60 );
  30. intSeconds = intSeconds - ( intMinutes * 60 );
  31. var strCountDown = ( intHours > 0 ? intHours.toLocaleString() +
  32. ' hour' + ( intHours === 1 ? '' : 's' ) : '' ) +
  33. ( intMinutes > 0 ? ( intHours > 0 ? ', ' : '' ) +
  34. intMinutes.toLocaleString() + ' minute' + ( intMinutes === 1 ? '' : 's' ) : '' ) +
  35. ( intSeconds > 0 ? ( intHours > 0 || intMinutes > 0 ? ', ' : '' ) +
  36. intSeconds.toLocaleString() + ' second' + ( intSeconds === 1 ? '' : 's' ) : '' );
  37.  
  38. log( 4, 'log', 'countDown( %i ) is returning: %s', intRawSeconds, strCountDown );
  39. return strCountDown;
  40. }
  41.  
  42. const intParamsStart = ( document.URL.indexOf( '?' ) + 1 );
  43. const strParams = document.URL.substr( intParamsStart );
  44. const arrParamSets = strParams.split( '&' );
  45. var objParams = {};
  46. arrParamSets.forEach( function( strParam ) {
  47. let arrParam = strParam.split( '=' );
  48. let strParamName = ( arrParam[ 0 ].toLowerCase() || '' );
  49. if ( strParamName === 'verbosity' ) {
  50. isDebug = true;
  51. intVerbosity = ( arrParam[ 1 ] ? ( parseInt( arrParam[ 1 ] ) < 0 ? 0 : ( parseInt( arrParam[ 1 ] ) > 9 ? 9 : parseInt( arrParam[ 1 ] ) ) ) : 9 );
  52. } else if ( strParamName === 'debug' ) {
  53. isDebug = toBoolean( arrParam[ 1 ] );
  54. intVerbosity = 1;
  55. }
  56. } );
  57.  
  58. log( 1, 'warn', 'Debug mode is on with verbosity level: %o', intVerbosity );
  59. log( 1, 'groupCollapsed', 'Verbosity options: (click to expand)' );
  60. 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.' );
  61. log( 1, 'groupEnd' );
  62.  
  63. function toBoolean( val ) {
  64. const arrTrue = [ undefined, null, '', true, 'true', 1, '1', 'on', 'yes' ];
  65. val = ( typeof( val ) === 'string' ? val.toLowerCase() : val );
  66.  
  67. log( 4, 'log', 'toBoolean() is returning: %o', ( arrTrue.indexOf( val ) !== -1 ? true : false ) );
  68. return ( arrTrue.indexOf( val ) !== -1 ? true : false );
  69. }
  70.  
  71. const objFullTimeStringHQ = {
  72. year: 'numeric', month: 'long', day: 'numeric',
  73. hour: 'numeric', minute: 'numeric', second: 'numeric',
  74. timeZone: 'America/Chicago', timeZoneName: 'short', hour12: false };
  75. const objShorTimeStringHQ = {
  76. year: 'numeric', month: 'short', day: 'numeric',
  77. hour: 'numeric', minute: 'numeric', second: 'numeric',
  78. timeZone: 'America/Chicago', hour12: false };
  79.  
  80. const address = document.URL.replace( /https?:\/\/www\.munzee\.com\/m\//i, '' ).split( '/' );
  81. const userName = ( address[ 0 ] || undefined );
  82. const subPage = ( address[ 1 ] || undefined );
  83. const subSubPage = ( address[ 2 ] || undefined );
  84. log( 1, 'info', 'userName = %s\tsubPage = %s\tsubSubPage = %s', userName, subPage, subSubPage );
  85.  
  86. ( function() {
  87. 'use strict';
  88. log( 0, 'info', 'Script loaded.' );
  89.  
  90. $( '.expires-at' ).each( function( i, elem ) {
  91. var objExpires = new Date( $( elem ).attr( 'title' ) );
  92. var intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
  93. var strNewExpires = objExpires.toLocaleDateString( 'en-US', objShorTimeStringHQ ) +
  94. ' (<span id="expires-countdown">' + countDown( intSecondsUntilExpires ) + '</span>)</span>';
  95. var objNudgeable = new Date( $( elem ).attr( 'title' ) );
  96. objNudgeable = new Date( objNudgeable.setHours( objNudgeable.getHours() - 9 ) );
  97. var intSecondsUntilNudge = Math.floor( ( objNudgeable.valueOf() - ( new Date() ).valueOf() ) / 1000 );
  98. var strNewNudge = '';
  99. if ( intSecondsUntilNudge > 0 ) {
  100. strNewNudge = '<br /><span class="nudge-at" data-nudge-at="' + objNudgeable.valueOf() +
  101. '" title="' + objNudgeable.toISOString() + '">Nudgeable ' +
  102. objNudgeable.toLocaleDateString( 'en-US', objShorTimeStringHQ ) + ' (<span id="nudge-countdown">' +
  103. countDown( intSecondsUntilNudge ) + '</span>)';
  104. }
  105. $( elem ).html( strNewExpires + strNewNudge );
  106. setInterval( function() {
  107. intSecondsUntilNudge = Math.floor( ( objNudgeable.valueOf() - ( new Date() ).valueOf() ) / 1000 );
  108. if ( intSecondsUntilNudge > 0 ) {
  109. $( 'span#nudge-countdown' ).text( countDown( intSecondsUntilNudge ) );
  110. } else {
  111. $( 'span.nudge-at' ).remove();
  112. }
  113. intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
  114. if ( intSecondsUntilExpires > 0 ) {
  115. $( 'span#expires-countdown' ).text( countDown( intSecondsUntilExpires ) );
  116. } else {
  117. location.reload();
  118. }
  119. }, 1000 );
  120. } );
  121.  
  122. $( '.deployed-at' ).each( function( i, elem ) {
  123. var strDeployed = new Date( $( elem ).attr( 'data-deployed-at' ) );
  124. switch( subPage ) {
  125. case 'archived' :
  126. case 'captures' :
  127. case 'deploys' :
  128. $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
  129. break;
  130. case 'socials' :
  131. if ( subSubPage === 'own' ) {
  132. $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
  133. }
  134. break;
  135. case 'kennel' :
  136. if ( subSubPage === 'transported' ) {
  137. $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
  138. }
  139. break;
  140. case 'blast' :
  141. case 'feed' :
  142. default:
  143. if ( !isNaN( subSubPage ) ) {
  144. $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
  145. } else {
  146. $( elem ).text( strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + ' (' + $( elem ).text() + ')' );
  147. }
  148. }
  149. } );
  150.  
  151. $( '.captured-at' ).each( function( i, elem ) {
  152. var strCaptured = new Date( $( elem ).attr( 'data-captured-at' ) );
  153. switch( subPage ) {
  154. case 'archived' :
  155. case 'captures' :
  156. case 'deploys' :
  157. $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
  158. break;
  159. case 'socials' :
  160. if ( subSubPage === 'own' ) {
  161. $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
  162. }
  163. break;
  164. case 'kennel' :
  165. if ( subSubPage === 'transported' ) {
  166. $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
  167. }
  168. break;
  169. case 'blast' :
  170. case 'feed' :
  171. default:
  172. $( elem ).text( strCaptured.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + ' (' + $( elem ).text() + ')' );
  173. }
  174. } );
  175.  
  176. $( '.blasted-at' ).each( function( i, elem ) {
  177. var strBlasted = new Date( $( elem ).attr( 'data-blasted-at' ) );
  178. $( elem ).append( '<p>' + strBlasted.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + '</p>' );
  179. } );
  180. } )();