Greasy Fork

MouseHunt - Tournament Time Helper

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

目前为 2018-03-08 提交的版本。查看 最新版本

// ==UserScript==
// @name         MouseHunt - Tournament Time Helper
// @author       Jia Hao (Limerence#0448 @Discord)
// @namespace    https://greasyfork.org/en/users/165918-jia-hao
// @version      1.3
// @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 saveTimeFormat() {
    var tournaments = document.getElementsByClassName("tournamentPage-tournamentRow tournamentPage-tournamentData basic pending");
    var timeFormatCheckbox = document.getElementById('timeformat');
    var startTime, endTime, day, date, time;

    if (timeFormatCheckbox.checked) {
        window.localStorage.setItem('timeformat', '24-hour'); //save settings
        for (i = 0; i < tournaments.length; i++) {
            startTime = tournaments[i].children[1].innerHTML.split("<br>");
            endTime = tournaments[i].children[2].innerHTML.split("<br>");
            day = startTime[0];
            date = startTime[1];
            time = startTime[2];
            tournaments[i].children[1].innerHTML = moment(day + " " + date + " " + time, "dddd D MMM YYYY h:mm A").format("dddd<br>D MMM YYYY<br>HH:mm").concat(" hrs");
            day = endTime[0];
            date = endTime[1];
            time = endTime[2];
            tournaments[i].children[2].innerHTML = moment(day + " " + date + " " + time, "dddd D MMM YYYY h:mm A").format("dddd<br>D MMM YYYY<br>HH:mm").concat(" hrs");
        }
    } else {
        window.localStorage.setItem('timeformat', '12-hour'); //save settings
        for (i = 0; i < tournaments.length; i++) {
            startTime = tournaments[i].children[1].innerHTML.split("<br>");
            endTime = tournaments[i].children[2].innerHTML.split("<br>");
            day = startTime[0];
            date = startTime[1];
            time = startTime[2];
            tournaments[i].children[1].innerHTML = moment(day + " " + date + " " + time + " hrs", "dddd D MMM YYYY HH:mm").format("dddd<br>D MMM YYYY<br>h:mm A");
            day = endTime[0];
            date = endTime[1];
            time = endTime[2];
            tournaments[i].children[2].innerHTML = moment(day + " " + date + " " + time + " hrs", "dddd D MMM YYYY HH:mm").format("dddd<br>D MMM YYYY<br>h:mm A");
        }
    }
}

function load() {

    //Get local time and time format preference
    var now = moment(new Date());
    if (Math.abs(moment(now).zone()) % 60 > 0) {
        now.add('minutes', 30);
    }
    var timeFormat = "12-hour";

    try {
        timeFormat = window.localStorage.getItem('timeformat');
        if (timeFormat === null) { //Default time format is 12-hour.
            window.localStorage.setItem('timeformat', '12-hour');
        }
    } catch (e) {
        console.log('Browser does not support localStorage');
    }

	try {

        //Checkbox for user time format preference
        if (!document.getElementById('timeformat')) {
            var tournamentHeading = document.getElementsByClassName("tournamentPage-tournamentHeading")[0];
            tournamentHeading.innerHTML += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            tournamentHeading.innerHTML += "<input id='timeformat' type='checkbox'>Display timings in 24-hour format";
        }
        if (timeFormat === "24-hour") {
            document.getElementById('timeformat').checked = true;
        }
        $('#timeformat').click(function() {saveTimeFormat();});

		//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;
            var formatString;

			//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();
            //End time of the tournament
			endTime = moment(startTime).add('hour', duration.split(" ")[0]);
			//Duration of the tournament
			tournaments[i].children[2].insertAdjacentHTML('afterend', "<div class='tournamentPage-tournament-column value'>" + duration + "</div>");

            if (timeFormat === '12-hour') {
                tournaments[i].children[1].innerHTML = moment(startTime).format("dddd<br>D MMM YYYY<br>h:00 A");
                tournaments[i].children[2].innerHTML = moment(endTime).format("dddd<br>D MMM YYYY<br>h:00 A");
            } else {
                tournaments[i].children[1].innerHTML = moment(startTime).format("dddd<br>D MMM YYYY<br>HH:00").concat(" hrs");
                tournaments[i].children[2].innerHTML = moment(endTime).format("dddd<br>D MMM YYYY<br>HH:00").concat(" hrs");
            }

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