MHQ ago

Show timestamps

目前為 2019-03-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         MHQ ago
// @namespace    none
// @version      2019.03.25.2253
// @description  Show timestamps
// @supportURL   https://Discord.me/TheShoeStore
// @author       technical13
// @match        https://www.munzee.com/m/*
// @grant        none
// ==/UserScript==
// jshint esversion: 6

var isDebug = false;
var intVerbosity = 0;
const ver = '2019.03.25.2253';
const scriptName = 'MHQ ago v' + ver;

function log( intV, strConsole, strLog, ...arrArgs ) {
  if ( strConsole === undefined ) { strConsole = 'log'; }
  if ( strLog === undefined ) { strLog = '%o'; }
  if ( intVerbosity >= intV && ( strConsole === 'groupEnd' ) ) { console[ strConsole ](); }
  if ( intV === 0 || ( isDebug && intVerbosity >= intV ) ) { console[ strConsole ]( '[%i]: %s: ' + strLog, intV, scriptName, ...arrArgs ); }
}

function countDown( intRawSeconds ) {
    var intSeconds = parseInt( intRawSeconds );
    var intHours = Math.floor( intSeconds / 3600 );
    intSeconds = intSeconds - ( intHours * 3600 );
    var intMinutes = Math.floor( intSeconds / 60 );
    intSeconds = intSeconds - ( intMinutes * 60 );
    var strCountDown = ( intHours > 0 ? intHours.toLocaleString() +
                        ' hour' + ( intHours === 1 ? '' : 's' ) : '' ) +
        ( intMinutes > 0 ? ( intHours > 0 ? ', ' : '' ) +
         intMinutes.toLocaleString() + ' minute' + ( intMinutes === 1 ? '' : 's' ) : '' ) +
        ( intSeconds > 0 ? ( intHours > 0 || intMinutes > 0 ? ', ' : '' ) +
         intSeconds.toLocaleString() + ' second' + ( intSeconds === 1 ? '' : 's' ) : '' );

    log( 4, 'log', 'countDown( %i ) is returning: %s', intRawSeconds, strCountDown );
    return strCountDown;
}

const intParamsStart = ( document.URL.indexOf( '?' ) + 1 );
const strParams = document.URL.substr( intParamsStart );
const arrParamSets = strParams.split( '&' );
var objParams = {};
arrParamSets.forEach( function( strParam ) {
    let arrParam = strParam.split( '=' );
    let strParamName = ( arrParam[ 0 ].toLowerCase() || '' );
    if ( strParamName === 'verbosity' ) {
        isDebug = true;
        intVerbosity = ( arrParam[ 1 ] ? ( parseInt( arrParam[ 1 ] ) < 0 ? 0 : ( parseInt( arrParam[ 1 ] ) > 9 ? 9 : parseInt( arrParam[ 1 ] ) ) ) : 9 );
    } else if ( strParamName === 'debug' ) {
        isDebug = toBoolean( arrParam[ 1 ] );
        intVerbosity = 1;
    }
} );

log( 1, 'warn', 'Debug mode is on with verbosity level: %o', intVerbosity );
log( 1, 'groupCollapsed', 'Verbosity options: (click to expand)' );
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.' );
log( 1, 'groupEnd' );

function toBoolean( val ) {
    const arrTrue = [ undefined, null, '', true, 'true', 1, '1', 'on', 'yes' ];
    val = ( typeof( val ) === 'string' ? val.toLowerCase() : val );

    log( 4, 'log', 'toBoolean() is returning: %o', ( arrTrue.indexOf( val ) !== -1 ? true : false ) );
    return ( arrTrue.indexOf( val ) !== -1 ? true : false );
}

const objFullTimeStringHQ = {
  year: 'numeric', month: 'long', day: 'numeric',
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  timeZone: 'America/Chicago', timeZoneName: 'short', hour12: false };
const objShorTimeStringHQ = {
  year: 'numeric', month: 'short', day: 'numeric',
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  timeZone: 'America/Chicago', hour12: false };

const address = document.URL.replace( /https?:\/\/www\.munzee\.com\/m\//i, '' ).split( '/' );
const userName = ( address[ 0 ] || undefined );
const subPage = ( address[ 1 ] || undefined );
const subSubPage = ( address[ 2 ] || undefined );
log( 1, 'info', 'userName = %s\tsubPage = %s\tsubSubPage = %s', userName, subPage, subSubPage );

( function() {
    'use strict';
    log( 0, 'info', 'Script loaded.' );

    $( '.expires-at' ).each( function( i, elem ) {
        var objExpires = new Date( $( elem ).attr( 'title' ) );
        var intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
        var strNewExpires = objExpires.toLocaleDateString( 'en-US', objShorTimeStringHQ ) +
            ' (<span id="expires-countdown">' + countDown( intSecondsUntilExpires ) + '</span>)</span>';
        var objNudgeable = new Date( $( elem ).attr( 'title' ) );
        objNudgeable = new Date( objNudgeable.setHours( objNudgeable.getHours() - 9 ) );
        var intSecondsUntilNudge = Math.floor( ( objNudgeable.valueOf() - ( new Date() ).valueOf() ) / 1000 );
        var strNewNudge = '';
        if ( intSecondsUntilNudge > 0 ) {
            strNewNudge = '<br /><span class="nudge-at" data-nudge-at="' + objNudgeable.valueOf() +
                '" title="' + objNudgeable.toISOString() + '">Nudgeable ' +
                objNudgeable.toLocaleDateString( 'en-US', objShorTimeStringHQ ) + ' (<span id="nudge-countdown">' +
                countDown( intSecondsUntilNudge ) + '</span>)';
        }
        $( elem ).html( strNewExpires + strNewNudge );
        setInterval( function() {
            intSecondsUntilNudge = Math.floor( ( objNudgeable.valueOf() - ( new Date() ).valueOf() ) / 1000 );
            if ( intSecondsUntilNudge > 0 ) {
                $( 'span#nudge-countdown' ).text( countDown( intSecondsUntilNudge ) );
            } else {
                $( 'span.nudge-at' ).remove();
            }
            intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
            if ( intSecondsUntilExpires > 0 ) {
                $( 'span#expires-countdown' ).text( countDown( intSecondsUntilExpires ) );
            } else {
                location.reload();
            }
        }, 1000 );
    } );

    $( '.deployed-at' ).each( function( i, elem ) {
        var strDeployed = new Date( $( elem ).attr( 'data-deployed-at' ) );
        var strRawAgo = $( elem ).text();
        if ( strRawAgo === 'seconds ago' || strRawAgo === 'a minute ago' ) {
            let intRawAgo = Math.round( ( ( new Date() ).valueOf() - strDeployed.valueOf() ) / 1000 );
            strRawAgo = intRawAgo + '&nbsp;seconds&nbsp;ago';
        } else if ( strRawAgo === 'an hour ago' || strRawAgo === '2 hours ago' ) {
            let intRawAgo = Math.round( ( ( ( new Date() ).valueOf() - strDeployed.valueOf() ) / 60 ) / 1000 );
            strRawAgo = intRawAgo + '&nbsp;minutes&nbsp;ago';
        } else {
            strRawAgo = strRawAgo.replace( ' ', '&nbsp;' );
        }
        switch( subPage ) {
            case 'archived' :
            case 'captures' :
            case 'deploys' :
                $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
                break;
            case 'socials' :
                if ( subSubPage === 'own' ) {
                    $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
                }
                break;
            case 'kennel' :
                if ( subSubPage === 'transported' ) {
                    $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
                }
                break;
            case 'blast' :
            case 'feed' :
            default:
                if ( !isNaN( subSubPage ) ) {
                    $( elem ).append( '<br />' + strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
                } else {
                    $( elem ).html( strDeployed.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + ' &shy;(' + strRawAgo + ')' );
                }
        }
    } );

    $( '.captured-at' ).each( function( i, elem ) {
        var strCaptured = new Date( $( elem ).attr( 'data-captured-at' ) );
        var strRawAgo = $( elem ).text();
        if ( strRawAgo === 'seconds ago' || strRawAgo === 'a minute ago' ) {
            let intRawAgo = Math.round( ( ( new Date() ).valueOf() - strCaptured.valueOf() ) / 1000 );
            strRawAgo = intRawAgo + '&nbsp;seconds&nbsp;ago';
        } else if ( strRawAgo === 'an hour ago' || strRawAgo === '2 hours ago' ) {
            let intRawAgo = Math.round( ( ( ( new Date() ).valueOf() - strCaptured.valueOf() ) / 60 ) / 1000 );
            strRawAgo = intRawAgo + '&nbsp;minutes&nbsp;ago';
        } else {
            strRawAgo = strRawAgo.replace( ' ', '&nbsp;' );
        }
        switch( subPage ) {
            case 'archived' :
            case 'captures' :
            case 'deploys' :
                $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
                break;
            case 'socials' :
                if ( subSubPage === 'own' ) {
                    $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objShorTimeStringHQ ) );
                }
                break;
            case 'kennel' :
                if ( subSubPage === 'transported' ) {
                    $( elem ).append( '<br />' + strCaptured.toLocaleDateString( 'en-US', objFullTimeStringHQ ) );
                }
                break;
            case 'blast' :
            case 'feed' :
            default:
                $( elem ).html( strCaptured.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + ' &shy;(' + strRawAgo + ')' );
        }
    } );

    $( '.blasted-at' ).each( function( i, elem ) {
        var strBlasted = new Date( $( elem ).attr( 'data-blasted-at' ) );
        $( elem ).append( '<p>' + strBlasted.toLocaleDateString( 'en-US', objFullTimeStringHQ ) + '</p>' );
    } );
} )();