zeeTreehouses

More details of myths currently hosted in treehouses and skylands. Expiry times powered by sohcah's CuppaZee

当前为 2020-05-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         zeeTreehouses
// @namespace    none
// @version      2020.05.14.2016
// @description  More details of myths currently hosted in treehouses and skylands. Expiry times powered by sohcah's CuppaZee
// @author       technical13
// @supportURL   https://discord.me/TheShoeStore
// @match        https://www.munzee.com/m/*/*
// @match        https://www.munzee.com/flows/*
// @grant        none
// ==/UserScript==
// jshint esversion: 6

var isDebug = false;
var intVerbosity = 0;
const ver = '2020.05.14.2016';
const scriptName = 'zeeTreehouses v' + ver;

const apiPrimaryURL = 'https://flame.cuppazee.uk/munzee/bouncers/v1';
const apiBackupURL = 'https://us-central1-cuppazeex.cloudfunctions.net/munzee';

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 ); }
}

var address = document.URL.replace( /https?:\/\/www\.munzee\.com\/?/i, '' ).split( '/' );
var isUserPage = false;
if ( address[ 0 ] === 'm' ) {
    isUserPage = true;
    address = address.slice( 1 );
}
const pageUserName = ( address[ 0 ] || undefined );
const subPage = ( address[ 1 ] || undefined );
const subSubPage = ( address[ 2 ] || undefined );
const isErrorPage = ( $( 'div#error' ).length === 0 ? false : true );
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 = toBoolean( arrParam[ 1 ] );
        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;
    } else if ( strParamName === 'report' ) {
        objParams.report = decodeURIComponent( arrParam[ 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 };

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 = '';
    if ( window.screen.width < 1200 ) {
        strCountDown = ( intHours.toLocaleString() <= 9 ? '0' : '' ) + intHours.toLocaleString() +
            ':' + ( intMinutes.toLocaleString() <= 9 ? '0' : '' ) + intMinutes.toLocaleString() +
            ':' + ( intSeconds.toLocaleString() <= 9 ? '0' : '' ) + intSeconds.toLocaleString();
    } else {
        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;
}

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

    try {
        log( 3, 'info', '\npageUserName: %s\nobjParams.report: %o\n!isNaN( parseInt( subPage ) ): %o\nsubPage: %o\n!isErrorPage: %o',
            pageUserName, objParams.report, !isNaN( parseInt( subPage ) ), subPage, !isErrorPage );
        if ( pageUserName === 'flows' && objParams.report ) {
            log( 1, 'log', 'Do error report...' );
            $( 'textarea#message' ).val( objParams.report );
        }
        else if ( !isNaN( parseInt( subPage ) ) && subPage !== undefined && pageUserName !== 'flows' && !isErrorPage ) {
            log( 1, 'log', 'Process main script.' );
            const pinSrc = $( 'div#munzee-name > a > img.pin' ).attr( 'src' );
            const arrPinSrc = pinSrc.split( '/' );
            const isTreehouse = ( arrPinSrc[ ( arrPinSrc.length - 1 ) ].split( '.' )[ 0 ].slice( 0, 9 ) === 'treehouse' ? true : false );
            const isSkyland = ( arrPinSrc[ ( arrPinSrc.length - 1 ) ].split( '.' )[ 0 ].slice( 0, 7 ) === 'skyland' ? true : false );
            const intHosting = parseInt( ( isTreehouse || isSkyland ? arrPinSrc[ ( arrPinSrc.length - 1 ) ].split( '.' )[ 0 ].slice( 9 ) : 1 ) === '' ? 0 : ( isTreehouse || isSkyland ? arrPinSrc[ ( arrPinSrc.length - 1 ) ].split( '.' )[ 0 ].slice( 9 ) : 1 ) );
            log( 3, 'info', '\npinSrc: %o\narrPinSrc: %o\nisTreehouse: %o\nisSkyland: %o\nintHosting', pinSrc, arrPinSrc, isTreehouse, isSkyland, intHosting );

            if ( isTreehouse || isSkyland ) {
                $( 'head' ).append( '<style type="text/css">div.unicorn { margin: 5px; font-size: 16px; font-weight: bold; }</style>' );
                $.get( { url: apiPrimaryURL + '?munzee=' + subPage + '&username=' + pageUserName } )
                    .done( cuppaZee => {
                    log( 8, 'info', 'Response from: %s : %o',
                        apiPrimaryURL + '?munzee=' + subPage + '&username=' + pageUserName, cuppaZee );
                    $( 'div.unicorn' ).each( ( ndx, unicorn ) => {
                        let bouncer = cuppaZee.data[ ndx ];
                        var objExpires = new Date( moment( bouncer.good_until * 1000 ).format() );
                        var intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
                        var strNewExpires = ' expiring in <span id="expires-countdown-treehouse' + ( ndx + 1 ) + '" title="' + objExpires.toLocaleDateString( 'en-US', objShorTimeStringHQ ) + '">' + countDown( intSecondsUntilExpires ) + '</span>';
                        $( unicorn ).html( $( unicorn ).html().replace( 'This Munzee is c', 'C' ) );
                        let thisUnicorn = $( unicorn ).find( 'a' );
                        let owner = thisUnicorn.attr( 'href' ).split( /https?:\/\/www\.munzee\.com\/m\//i )[ 1 ].split( '/' )[ 0 ];
                        thisUnicorn.before( '<a href="https://www.munzee.com/m/' + owner + '">' + owner + '</a>\'s ' );
                        thisUnicorn.after( strNewExpires );

                        setInterval( function() {
                            intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
                            if ( intSecondsUntilExpires > 0 ) {
                                $( 'span#expires-countdown-treehouse' + ( ndx + 1 ) + '' ).text( countDown( intSecondsUntilExpires ) );
                            } else { location.reload(); }
                        }, 1000 );
                    } );
                } ).fail( errCuppaZee => {
                    log( 0, 'error', 'Failed to get response from: %s : %o',
                        apiPrimaryURL + '?munzee=' + subPage + '&username=' + pageUserName, errCuppaZee );
                    var notifyCZ = confirm( scriptName + '\n\tERROR!\t(details in console)\n\tFailed to get bouncer from: flame.cuppazee.uk\n\tPlease press [Cancel] and update the script\n\t\tbefore pressing [OK] to notify the API author\n\t\tas there may already be a fix!\n\n\t\t\t\tThanks!' );
                    if ( notifyCZ ) {
                        window.location.href = 'https://www.munzee.com/flows/?username=sohcah&report=' + encodeURIComponent( 'Version: ' + scriptName + '\nPage: ' + window.location.href + '\nUserAgent: ' + navigator.userAgent + '\nError: ' + errCuppaZee + 'Request: https://flame.cuppazee.uk/munzee/bouncers/v1?munzee=' + subPage + '&username=' + pageUserName + '\n Reporter comments: none' );
                    }
                    $.get( { url: apiBackupURL + '?munzee=' + subPage + '&user=' + pageUserName } )
                        .done( oldCuppaZee => {
                        $( 'div.unicorn' ).each( ( ndx, unicorn ) => {
                            let bouncer = oldCuppaZee.details.bouncers[ ndx ];
                            var objExpires = new Date( moment( bouncer.good_until * 1000 ).format() );
                            var intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
                            var strNewExpires = ' expiring in <span id="expires-countdown-treehouse' + ( ndx + 1 ) + '" title="' + objExpires.toLocaleDateString( 'en-US', objShorTimeStringHQ ) + '">' + countDown( intSecondsUntilExpires ) + '</span>';
                            $( unicorn ).html( $( unicorn ).html().replace( 'This Munzee is c', 'C' ) );
                            let thisUnicorn = $( unicorn ).find( 'a' );
                            let owner = thisUnicorn.attr( 'href' ).split( /https?:\/\/www\.munzee\.com\/m\//i )[ 1 ].split( '/' )[ 0 ];
                            thisUnicorn.before( '<a href="https://www.munzee.com/m/' + owner + '">' + owner + '</a>\'s ' );
                            thisUnicorn.after( strNewExpires );

                            setInterval( function() {
                                intSecondsUntilExpires = Math.floor( ( objExpires.valueOf() - ( new Date() ).valueOf() ) / 1000 );
                                if ( intSecondsUntilExpires > 0 ) {
                                    $( 'span#expires-countdown-treehouse' + ( ndx + 1 ) + '' ).text( countDown( intSecondsUntilExpires ) );
                                } else {
                                    location.reload();
                                }
                            }, 1000 );
                        } );
                    } ).fail( errOldCuppaZee => {
                        log( 0, 'error', 'Failed to get response from: %s : %o',
                            apiBackupURL + '?munzee=' + subPage + '&user=' + pageUserName, errOldCuppaZee );
                        var notifyCZ = confirm( scriptName + '\n\tERROR!\tFailed to get bouncer from us-central1-cuppazeex.cloudfunctions.net backup API\n\t\t\t(details in console)\n\tPlease press [Cancel] and update the script\n\t\tbefore pressing [OK] to notify the API author\n\t\tas there may already be a fix!\n\n\t\t\t\tThanks!' );
                        if ( notifyCZ ) {
                            window.location.href = 'https://www.munzee.com/flows/?username=sohcah&report=' + encodeURIComponent( 'Version: ' + scriptName + '\nPage: ' + window.location.href + '\nUserAgent: ' + navigator.userAgent + '\nError: ' + errCuppaZee + 'Request: https://us-central1-cuppazeex.cloudfunctions.net/munzee?munzee=' + subPage + '&user=' + pageUserName + '\n Reporter comments: none' );
                        }
                        $( 'div.unicorn:first' ).before( '<div id="czFailed" class="unicorn"><img style="height: 32px;" src="https://gardenpainter.ide.sk/delete_munzee.png"> ' + scriptName + ' failed to get response from CuppaZee.</div>' );
                        $( 'div.unicorn' ).each( ( ndx, unicorn ) => {
                            $( unicorn ).html( $( unicorn ).html().replace( 'This Munzee is c', 'C' ) );
                            let thisUnicorn = $( unicorn ).find( 'a' );
                            let owner = thisUnicorn.attr( 'href' ).split( /https?:\/\/www\.munzee\.com\/m\//i )[ 1 ].split( '/' )[ 0 ];
                            thisUnicorn.before( '<a href="https://www.munzee.com/m/' + owner + '">' + owner + '</a>\'s ' );
                        } );
                    } );
                } );
            }
        }
    } catch ( errScript ) {
        log( 0, 'error', 'Encountered an error: %o', errScript );
        var notifyAuthor = confirm( scriptName + '\n\tERROR!\t\t(details in console)\n\tPlease press [Cancel] and update the script\n\t\tbefore pressing [OK] to notify the author\n\t\tas there may already be a fix!\n\n\t\t\t\tThanks!' );
        if ( notifyAuthor ) {
            window.location.href = 'https://www.munzee.com/flows/?username=technical13&report=' + encodeURIComponent( 'Version: ' + scriptName + '\nPage: ' + window.location.href + '\nUserAgent: ' + navigator.userAgent + '\nError: ' + errScript + '\n Reporter comments: none' );
        }
    }
} )();