derLoki / AGDQ Marker

// ==UserScript==
// @name         AGDQ Marker
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  This script will highlight the currently active run for AGDQ 2017. It will also scroll to the highlighted position
// @author       derLoki
// @match        https://gamesdonequick.com/schedule
// @grant        none
// @copyright	 2017, derLoki
// ==/UserScript==

(function() {
    'use strict';

    // Your code here... 
    var curDate = new Date();
    var sync = "5/12/2016 ";
    var color = "#58bd56";

    function formatAMPM(date) {
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return strTime;
    }



    var curTime = formatAMPM(curDate);


    var fortnightAway = curDate,
        date = fortnightAway.getDate(), 
        month = "January,February,March,April,May,June,July,August,September,October,November,December"
    .split(",")[fortnightAway.getMonth()];

    function nth(d) {
        if(d>3 && d<21) return 'th'; // thanks kennebec
        switch (d % 10) {
            case 1:  return "st";
            case 2:  return "nd";
            case 3:  return "rd";
            default: return "th";
        }
    } 
    var day = month+" "+date+nth(date);

    var daySplit = $(".day-split").find("*:contains("+day+")").parent().index();
    var trs = $("tbody tr");
    var checkDate = new Date(sync+curTime);

    var lastid = 0;

    for(var i = daySplit+1; i < trs.length; i++){

        var tr = $(trs[i]);
        if(tr.hasClass("day-split") || tr.hasClass("second-row"))
            continue;

        var td = $(tr.find("td")[0]);
        var tdTime = td.text();
        var tdDate = new Date(sync+tdTime);
        if(tdDate > checkDate){

            var oldTr = $(trs[lastid]);
            var oldTr2 = $(trs[lastid+1]);

            oldTr.css("background-color",color);
            oldTr2.css("background-color",color);
            break;

        }
        lastid = i;
    }
    setTimeout(function() {
        trs[lastid].scrollIntoView(true);
    }, 500);



})();