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-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         MouseHunt - Tournament Time Helper
// @author       Jia Hao (Limerence#0448 @Discord)
// @namespace    https://greasyfork.org/en/users/165918-jia-hao
// @version      1.1
// @description  Automatically converts "Begins in:" to your local time as well as adding the end time for tournaments.
// @include      http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @include      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/locale/en-ie.js
// @include      http://www.mousehuntgame.com/*
// @include      https://www.mousehuntgame.com/*
// ==/UserScript==

function load() {

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

	try {
		//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");
		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;

			//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 minute

				if (secondPair[1] !== 'hours') { //don't round up if second pair is in hours since it is already rounded up
					startTime = clonedTime.minute() || clonedTime.second() || clonedTime.millisecond() ? clonedTime.add(1, 'hour').startOf('hour') : clonedTime.startOf('hour');
				}

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

			//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>");
		}
	} catch (err) {
		console.log(err);
	}
}


$(document).ready(function() {
    if ((window.location.href).includes("tournament.php")) {
        load();
    }

    var handle = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() {
            var jsonString = JSON.parse(this.responseText);

            //First check is to see if user is at tournaments page, second check is to handle page 'refresh' after joining/leaving tournament
            if (jsonString.page_title === "MouseHunt | Tournaments" || jsonString.tournaments !== undefined) {
                load();
            }
        });
        handle.apply(this, arguments);
    };
});