damiancd / ACLeague LMP PRO 104% counter

// ==UserScript==
// @name         ACLeague LMP PRO 104% counter
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  ACLeague LMP PRO 104% counter
// @author       damiancd
// @include      http://managerdc7.rackservice.org:50915/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    $(document).ready(function() {
        //LAMBO OP TORY CHUJOWE
        // NO I CO TU ZAGLÄ„DASZ CWELASIE XD
        var percents = 104;
        var totalAvgTime = 0;
        var avgTime = 0;
        var timeToGetIn = 0;
        var percentTime = 0;

        var counterPro = 1;
        var toPro = 16;

        var counterSemiPro = 1;
        var toSemiPro = 10;
        var page = $('body > div > div.page-header > div:nth-child(3) > div > form > div > div:nth-child(2) > a').html();
        var isOnCorrectPage = (page === 'Show selected');
        if (isOnCorrectPage) {
            for(var i=1; i<=3; i++) {
                var firstThreeLapTime = $('body > div > div.row > div > table > tbody > tr:nth-child(' + i + ') > td:nth-child(4)').html();
                totalAvgTime = parseInt(totalAvgTime) + parseInt(timeToMillis(firstThreeLapTime));
            }
            avgTime = parseInt(totalAvgTime/3);
            percentTime = parseInt(avgTime*(percents/100));
            timeToGetIn = Math.ceil(percentTime/1000)*1000;

            for(var j=1; j<120; j++) {
                var lapTime = $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(4)').html();
                var lapTimeMs = parseInt(timeToMillis(lapTime));
                if (lapTimeMs < timeToGetIn) {
                    $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(4)').css('background-color', '#92ef9a');
                    var validLaps = $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(13)').html();
                    if (validLaps >= 20) {
                        if(counterPro < toPro) {
                            $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(5)').css('background-color', '#92ef9a').append("<strong><br><style='font-size: 12px;'>" + counterPro + ". PRO</strong></style>");
                            counterPro++;
                        } else if(counterSemiPro < toSemiPro) {
                            $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(5)').css('background-color', '#f7cf8f').append("<strong><br><style='font-size: 12px;'>" + counterSemiPro + ". sPRO</strong></style>");
                            counterSemiPro++;
                        }
                    } else {
                        $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(5)').css('background-color', '#f78f8f').append(' LAPS<20');
                    }
                }
                var percentsToLeaders = (parseFloat(lapTimeMs/avgTime).toFixed(3))*100;
                var roundedPercents = Math.round(percentsToLeaders * 100) / 100;

                $('body > div > div.row > div > table > tbody > tr:nth-child(' + j + ') > td:nth-child(4)').append('<br>' + roundedPercents + '%');
            }
        }
    });
})();

function timeToMillis(lapTime) {
    if(lapTime) {
        var millisSplit = lapTime.split('.');
        var minutesSeconds = millisSplit[0].split(':');

        var minutes = minutesSeconds[0];
        var seconds = minutesSeconds[1];
        var millis = millisSplit[1];

        return (minutes*60*1000 + seconds*1000 + parseInt(millis));
    }
}