tobias88 / Pmanager.org National Team Players

// ==UserScript==
// @name          Pmanager.org National Team Players
// @namespace     http://tampermonkey.net/
// @description   Fetch All of the National Team Players
// @include       https://www.pmanager.org/convocatoria.asp?id=*
// @require       https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @version       2.01
// @license MIT
// @grant         GM_xmlhttpRequest
// ==/UserScript==

let $ = window.$;

let button;
let tbody;
let players;
const originalColumnCount = 10;
let originalTeamPlayersFontCount;
let fetched = false;

$(document).ready(function()
{
	tbody = $("#feature").find("tbody").eq(1);
	players = tbody.find(".list1, .list2");
	CreateButton();
}); // ready

async function FetchPlayers()
{
	if (fetched || false == confirm("Ne használd gyakran, mert az ingyenes tárhelyünk nem szereti!\nBiztosan szeretnéd?"))
		return;
	fetched = true;
	button.find("img").attr("src","https://style-cdn.ravelrycache.com/images/progress.gif");
	originalTeamPlayersFontCount = players.find("font.team_players").size();
	console.log(originalTeamPlayersFontCount);
	while (originalTeamPlayersFontCount != players.find("font.team_players").size() + players.size())
	{
		DownloadPlayerData();
		await Sleep(2000);
	}
	ModifyHeader();
	console.log("DONE!");
	button.find("img").attr("src","http://www.pmanagertutorial.nhely.hu/images/other/ok.png");
}

function Sleep(ms)
{
	return new Promise(resolve => setTimeout(resolve, ms));
}

async function DownloadPlayerData()
{
	console.log("currentdownload");
	// gathering data
	const playerLinks = players.find("a[href*='jog_id']");
	for (let playerNumber = 0; playerNumber < playerLinks.size(); ++playerNumber)
	{
		if (players.eq(playerNumber).find("td").size() != originalColumnCount)
		{
			console.log(playerNumber + " already downloaded");
			continue;
		}
		const href = playerLinks.eq(playerNumber).attr("href");
		const afterEqualityPos = href.indexOf("=") + 1;
		const playerId = href.substr(afterEqualityPos, href.length - afterEqualityPos);
		console.log(playerId + " download");
		(function(playerNumber)
		{ // save playerNumber, because ajax is asyncronous
			GM_xmlhttpRequest({
				method: "GET",
				url: "http://www.pmanagertutorial.nhely.hu/pm_piac/json.php?name=json_name&password=json_password&id=" + playerId,
				timeout: 5000,
				onload: function (xhr)
				{
					if (xhr.responseText.charAt(0) == "[")
					{
						let data;
						try
						{
							data = JSON.parse(xhr.responseText);
						}
						catch(e)
						{
							data = eval("(" + xhr.responseText + ")");
						}
						ShowPlayerData(data, playerNumber);
					}
				}
			});
		})(playerNumber);//
		/*// test for not found
        const responseText = '[{"Poszt":"?","Nev":"?","ID":"?","Kor":"?","Vedes":"?","Keresztezes":"?","Reflex":"?","Hatarozottsag":"?",\n' +
                              '"Szereles":"?","Fejjatek":"?","Atadas":"?","Helyezkedes":"?","Golszerzes":"?","Technika":"?","Gyorsasag":"?",\n' +
                              '"Ero":"?","Datum":"?","GS":"?","Orszag":"?","Adottsag":"?","Tehetseg":"?"}]';
        const data = JSON.parse(responseText);
        //ShowPlayer(data, playerNumber);
        // test for found
        const responseText2 = '[{"Poszt":"V JK","Nev":"A. Karaivanov","ID":"15558582","Kor":"31","Vedes":"8","Keresztezes":"1","Reflex":"8",' +
                               '"Hatarozottsag":"4","Szereles":"19","Fejjatek":"20","Atadas":"6","Helyezkedes":"1","Golszerzes":"2","Technika":"5",' +
                               '"Gyorsasag":"5","Ero":"20","Datum":"2018-02-12","GS":"73.0%","Orszag":"99","Adottsag":"Nincs","Tehetseg":"Pocsék"}]'
        const data2 = JSON.parse(responseText2);
        //ShowPlayer(data, playerNumber);
        ShowPlayerData(playerNumber % 2 ? data2 : data, playerNumber);//*/
	}
}

function ModifyHeader()
{
	// modify existed columns
	const header = tbody.find("td.cabecalhos");
	header.eq(0).attr("width","3%");
	header.eq(4).text("Kor");
	header.eq(4).attr("width","3%");
	header.eq(5).attr("width","5%");
	header.eq(6).attr("width","5%");
	header.eq(7).attr("width","5%");
	header.eq(8).replaceWith("<td class=\"cabecalhos\" width=\"3%\" align=\"center\">M</td>");
	// the new column
	header.eq(9).replaceWith("<td class=\"cabecalhos\" width=\"100%\" align=\"center\">Adat</td>");
	header.eq(3).remove();
}

function ShowPlayerData(data, playerNumber)
{
	const myLineTd = players.eq(playerNumber).find("td");
	myLineTd.eq(0).attr("width","5%");
	for (let column = 6; column < 8; ++column)
	{
		const cell = myLineTd.eq(column);
		const imgs = cell.find("img");
		const cellSrc = imgs.eq(1).attr("src");
		const color = cellSrc.substr(4, cellSrc.length - 4 - 7);
		imgs.remove();
		cell.attr("align", "center");
		cell.attr("style", "background-color:" + (color == "green" ? "LightGreen" : color == "red" ? "Tomato" : "LightSalmon ") + ";");
	}
	// remove
	myLineTd.eq(3).remove();
	if (data[0].ID == "?")
	{
		myLineTd.eq(9).text("");
		return;
	}
	// add
	myLineTd.eq(4).append("(" + data[0].Kor + ")");
	myLineTd.eq(7).append("(" + data[0].Tehetseg.trim() + ")");
	myLineTd.eq(9).html(GetDataString(data));
}

function GetDataString(data)
{
	let ret = "<u>" + data[0].Datum + ":</u> ";
	if (data[0].Poszt.charAt(0) == "G")
	{
		ret += "<b><u> " + data[0].Vedes + "</u>-<u>" + data[0].Keresztezes + "</b></u> " +
            "<b> " + data[0].Reflex + "-" + data[0].Hatarozottsag + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(Sz: " +data[0].Szereles + ", F: " + data[0].Fejjatek + ", Á: " + data[0].Atadas + ")";
	}
	else if (data[0].Poszt.charAt(0) == "V")
	{
		ret += "<b><u> " + data[0].Szereles + "</u>-<u>" + data[0].Fejjatek + "</b></u> " +
            "<b> " + data[0].Atadas + "-" + data[0].Helyezkedes + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>";
	}
	else if (data[0].Poszt.charAt(0) == "K")
	{
		ret += "<b><u> " + data[0].Atadas + "</u>-<u>" + data[0].Helyezkedes + "</b></u> " +
            "<b> " + data[0].Szereles + "-" + data[0].Technika + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(F: " + data[0].Fejjatek + ", G: " + data[0].Golszerzes + ")";

	}
	else if (data[0].Poszt.charAt(0) == "T")
	{
		ret += "<b><u> " + data[0].Golszerzes + "</u>-<u>" + data[0].Technika + "</b></u> " +
            "<b> " + data[0].Fejjatek + "-" + data[0].Helyezkedes + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(Sz: " + data[0].Szereles + ", Á: " + data[0].Atadas + ")";
	}
	ret += " <u>Adottság:</u> " + data[0].Adottsag;
	return ret;
}

function CreateButton()
{
	button = tbody.find("td").eq(0);
	if (button.text() != "Pozíció")
		return;
	button.html(
		"<a href=\"javascript:void(0);\"><img title=\"Adat Lekérés\" src=\"http://www.pmanagertutorial.nhely.hu/images/other/arrow_down.png\"></a>"
	);
	button.click(function()
	{
		FetchPlayers();
	});
}