proemarcgmail.com / Agent Stats ChangeUI

// ==UserScript==
// @name         Agent Stats ChangeUI
// @namespace    http://proest.net/
// @version      0.3.1
// @description  Change the visuals of www.agent-stats.com, a statistics tool for the game Ingress.
// @author       Marcus Proest
// @match        https://www.agent-stats.com/
// @match        https://www.agent-stats.com/index.php*
// @match        https://www.agent-stats.com/?sort_rank*
// @grant        none
// ==/UserScript==

function pad(num, size) {
    var s = "000000000" + num;
    return s.substr(s.length-size);
}

function formatLevel(textraw, testfor) {
    
    if(textraw && testfor.test(textraw.textContent)) {
        textraw = textraw.textContent.substring(textraw.textContent.indexOf(" level "));
        textraw = textraw.replace(" on ","<br />");
        textraw = textraw.replace(" (","<br /><div class=\"details\">");
        textraw = textraw.replace(")","</div>");        
        return textraw;
    }
    return 'undefined';
}

var cbadges=['bronze','silver','gold','platinum','black'];

apnow   = (document.evaluate("/html/body/div[2]/div[4]/div[2]/ul/li[1]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);
apweek  = (document.evaluate("/html/body/div[2]/div[4]/div[2]/ul/li[2]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);
apmonth = (document.evaluate("/html/body/div[2]/div[4]/div[2]/ul/li[3]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);
aptotal = (document.evaluate("/html/body/div[2]/div[4]/div[2]/ul/li[4]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);

ap = [];
for(var i = 1; i < 17; i++) {
    ap[i] = (document.evaluate('/html/body/div[2]/div[4]/div[2]/ul/li['+(i+4)+']', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);
}

var row = document.createElement("tr");
var td = [];

for(var i = 1; i < 6; i++) {
    td[i] = document.createElement("td");
    row.appendChild(td[i]);
}

td[1].innerHTML = 'level<br />action points<br /><div class=\"details\">Next Five Levels By AP</div>';
td[2].innerHTML = apnow.textContent.substring(apnow.textContent.indexOf(" gained ") + 7);
td[3].innerHTML = apweek.textContent.substring(apweek.textContent.indexOf(" gains ") + 7, apweek.textContent.indexOf(" AP ") + 3);
td[4].innerHTML = apmonth.textContent.substring(apmonth.textContent.indexOf(" gains ") + 7, apmonth.textContent.indexOf(" AP ") + 3);
td[5].innerHTML = aptotal.textContent.substring(aptotal.textContent.indexOf(" has ") + 5, aptotal.textContent.indexOf(" AP") + 3);

var testfor = /will get enough AP for level/;
var testcurrentlevel = /level (\d?\d)/;
var currentlevel = "";

var tdl = [];

//if(ap[1] && ap[1].textContent.includes(testfor)) {
if(ap[1] && testfor.test(ap[1].textContent)) {
    currentlevel = ap[1].textContent.match(testcurrentlevel)[1];  
}

for(var i = 1; i < 6; i++) {
	ap[i] = formatLevel(ap[i], testfor);
  tdl[i] = document.createElement("td");
	tdl[i].innerHTML = ap[i].trim();
	tdl[i].style.verticalAlign = 'top';
	row.appendChild(tdl[i]);
}

for(var i = 6; i < 17; i++) {
	ap[i] = formatLevel(ap[i], testfor);
}

var ba = [];

for(var i = 1; i < 17; i++) {
    var elem = (i+21)-currentlevel;
    ba[i] = (document.evaluate('/html/body/div[2]/div[4]/div[2]/ul/li['+elem+']/span[2]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);    
}

for(var i = 1; i < 6; i++) {
if(ba[i]) { tdl[i].innerHTML += '<span class="details">'+ba[i].innerHTML+'</span>'; }
}

$('body').append("<hr /><br /><br />");

var h = {};
var leveltest = /(level \d?\d)<br[ ]?[\/]?>([1-3]?\d)\/.*?([1]?[0-9])\/.*?(\d{4})<br[ ]?[\/]?><div class="details">[0-9,]* short<\/div>/;
var level = ""

for(var i = 1; i < 17; i++) {
	if(ap[i] && leveltest.test(ap[i].trim())) {
		var level = ap[i].trim().match(leveltest);
		h[level[4]+"-"+pad(level[3],2)+"-"+pad(level[2],2)+'<span id="salt-l1" />'] = [level[1], -1, ba[i]];
	}
}

var table = document.evaluate("//table[@id='predictionTable']/tbody/tr/td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var currenttitle = "";
var maxbadges = {};
var countbadges = 0;
for(var i = 0; i < table.snapshotLength; i++) {
    var titletest  = /(\D*)<div class="details">\D*<\/div>/;
    var datetest = /([0-3]?\d)\/.([1]?\d)\/.(\d{4})<div class="details">[0-9,]*[ ]short<\/div>/;
    var ss = table.snapshotItem(i).innerHTML;
    if(titletest.test(ss)) {
        maxbadges[currenttitle[1]] = countbadges;
        currenttitle = ss.match(titletest);
        countbadges = 0;
    }

    if(datetest.test(ss)) {
        var date = ss.match(datetest);
        countbadges++;
        h[date[3]+"-"+pad(date[2],2)+"-"+pad(date[1],2)+'<span id="salt-'+i+'" />'] = [currenttitle[1],countbadges];
    }
}

maxbadges[currenttitle[1]] = countbadges;

var keys = [];
for (var key in h) {
    if (h.hasOwnProperty(key)) {
        keys.push(key);
    }
}

keys.sort ();

for (i in keys) {
    var key = keys[i];
    var value = h[key];
    var badge = value[1] - maxbadges[value[0]];
    var myimg = '<img src="https://www.agent-stats.com/img/'+value[0]+'-'+cbadges[(4-maxbadges[value[0]]+value[1])]+'.png" />';
    var mytext = '<span class="details"> you will receive the</span> ';
    var mybadgetext = ' <span class="details">badge</span>';
    var mybadgecolor = cbadges[(4-maxbadges[value[0]]+value[1])];
    var neededbadgetext = "";
    if(value[1] < 0) {
        myimg = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABJ0RVh0U29mdHdhcmUAR3JlZW5zaG90XlUIBQAABWRJREFUWEe9V1lsVFUYvnPvnbl37uzTWTpdUGmLguGloAETobPdmmGmM0xLRLDEsGiUFvrgs+8uCSDO0A0MMYEgWwpxeRYJGME3MSaARhNl0cALGSiDn/85s3Rsp2W64Em+nHOmp///ne//zzn/FR7XpLFUXDkSy2v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04xMF9YaGPCrT4q1fphXxOAiz1GJ7pygnQ8pasHwg+cPiPMQzokio/xBi2ih8NZoxLswal2W5qoMmKVkqWntVyiKVSyu9wSlH3Be9LxZAevCViFog7r425GYrQT8t0BroSTSEw2OhuwDdhp52XnQ1HUkdrKnnBOOpYIceelxioUJRPNub1Uzw9GId8ZICX64Gy1wlnM5NnC1kMlGb3/rCQzZcNwU6GrDkfuTXFeapzEUCTvIiVUKp9YOOSb/bB1tRSSiDJZJUnZMZ0Odto127k1tbhQgJAN5pzFXB3Uc8LRdQXZp2vSWE9U3U9KsHBQSc3DcXMXHPHF5dPCUBqzE8PujtL9wUj+V/YI3FToqoOdOelkjV9JXAnKiTovkTgQgXybCGxeSsXEZog/boVEYFcqG3OwMV0w7Hf5y25+1ATKdiVD1RZtRBkl2Y8WE67WxhJT2R8ZZ0fU0UHffLRbQw2fZuKl3kL819I3o5+cD9HOp4v54xpTQs0E77Njw+V+0QP7ai+H4yVfYUy9YzWN+ZzGK9w8PC5KZmVkhoSrtTESppPxvLq3g26uDro+GYIw05z9xn/fG6SesGdtcU7fkSeSufI5n7YJwr86PAJdaQ8xQwAAAABJRU5ErkJggg==" />';
        if(value[2]) {		
            mytext = ' <span class="details">you will have enough ap to become </span> ';
		} else {
			mytext = ' <span class="details">you will become </span> ';
		}
        mybadgetext = '';
        mybadgecolor = '';
        if(value[2]) {
            neededbadgetext = ' <span class="details">'+value[2].innerHTML+'</span>';
        }
    }
    $('body').append(myimg + '<span class="details">On</span> ' + key + mytext + mybadgecolor + ' ' + value[0]  +mybadgetext + neededbadgetext +'<span class="details">.</span><br />');
}

document.getElementsByTagName("tr")[1].parentNode.insertBefore(row, document.getElementsByTagName("tr")[1]);
var list = (document.evaluate("/html/body/div[2]/div[4]/div[2]/ul", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)).snapshotItem(0);

$('body').append("<hr /><br /><br />");
$('body').append(list);