Greasy Fork

MouseHunt - Tournament Time Helper

Automatically converts "Begins in:" to your local time as well as adding the end time for tournaments.

目前为 2018-01-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         MouseHunt - Tournament Time Helper
// @author       Jia Hao (Limerence#0448 @Discord)
// @namespace    https://www.facebook.com/chocologicall
// @version      1.0
// @description  Automatically converts "Begins in:" to your local time as well as adding the end time for tournaments.
// @include      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/locale/en-ie.js
// @include      http://www.mousehuntgame.com/tournament.php
// @include      https://www.mousehuntgame.com/tournament.php
// @include		 http://apps.facebook.com/mousehunt/*
// @include		 https://apps.facebook.com/mousehunt/*
// @include      http://www.mousehuntgame.com/canvas*
// @include      https://www.mousehuntgame.com/canvas*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    //Get local time
    var now = moment(new Date());
    console.log(now);

    //Editing the Table
    //Shrinking tournament description to fit a new column
    var tournamentNames = document.getElementsByClassName("tournamentPage-tournament-column name");
    for (var i = 0; i < tournamentNames.length; i++) {
        tournamentNames[i].style.width = "43.5%";
    }

    //Changing the header "Begins in:" to "Begins at:"
    //Adding the header "Ends at:"
    //Looping since there are potentially special tournament tabs
    var beginsHeader = document.getElementsByClassName("tournamentPage-tournament-column label");
    for (i = 0; i < beginsHeader.length; i++) {
        if (beginsHeader[i].innerHTML === "Begins in:") {
            beginsHeader[i].insertAdjacentHTML('afterend', "<div class='tournamentPage-tournament-column label'>Ends at:</div>");
            beginsHeader[i].innerHTML = "Begins at:";
        }
    }

    //Replace starting times of each tournament
    var tournaments = document.getElementsByClassName("tournamentPage-tournamentRow tournamentPage-tournamentData basic pending");
    for (i = 0; i < tournaments.length; i++) {
        var beginsAt = tournaments[i].children[1].innerHTML;
        var duration = tournaments[i].children[2].innerHTML;
        var dayhourminute = beginsAt.split("<br>"); //[0] is first pair of [number] day/hour/minute left, [1] is second pair of [number] day/hour/minute left
        var firstPair = dayhourminute[0].split(" "); //[0] is numeric value, [1] is day/hour/minute
        var secondPair = dayhourminute[1].split(" "); //[0] is numeric value, [1] is day/hour/minute
        var clonedTime = moment(now);
        var startTime = 0;
        var endTime = 0;

        console.log(firstPair[0] + firstPair[1] + secondPair[0] + secondPair[1]);

        //Pluralize the words so that they can be used directly when adding time
        if (!firstPair[1].endsWith("s")) {
            firstPair[1] = firstPair[1].concat("s");
        }
        if (!secondPair[1].endsWith("s")) {
            secondPair[1] = secondPair[1].concat("s");
        }

        if (firstPair[1] === 'days') { //if time remaining starts with days, we have to round up the current hour or minutes
            startTime = now.minute() || now.second() || now.millisecond() ? clonedTime.add(1, 'hour').startOf('hour').add(1, 'minute').startOf('minute') : now.startOf('hour');
        } else if (firstPair[1] === 'hours') { //if time remaining starts with hours, we have to round up the current minute
            startTime = now.second() || now.millisecond() ? clonedTime.add(1, 'minute').startOf('minute') : now.startOf('minute');
        } else if (firstPair[1] === 'minutes') { //if time remaining starts with minutes, we have to round up the current second
            startTime = now.millisecond() ? clonedTime.add(1, 'second').startOf('second') : now.startOf('second');
        }

        //The start time of the tournament
        startTime = moment(clonedTime).add(firstPair[1], firstPair[0]).add(secondPair[1], secondPair[0]).toDate();
        tournaments[i].children[1].innerHTML = moment(startTime).format("dddd<br>D MMM YYYY<br>h:00 A");

        //End time of the tournament
        endTime = moment(startTime).add('hour', duration.split(" ")[0]);
        tournaments[i].children[2].innerHTML = moment(endTime).format("dddd<br>D MMM YYYY<br>h:00 A");

        //Duration of the tournament
        tournaments[i].children[2].insertAdjacentHTML('afterend', "<div class='tournamentPage-tournament-column value'>" + duration + "</div>");
    }

})();