// ==UserScript== // @name mz amount of friendlies played by each player during the week // @namespace http://tampermonkey.net/ // @version 0.1 // @description … // @author Son Heung-min // @match *://www.managerzone.com/?p=challenges* // @grant none // @downloadURL none // ==/UserScript== (function () { "use strict"; // team id will be needed let teamId; // will store players and how many matches they have played let appearances = new Map(); // here it is creating a new div and table for displaying the players and matches played const topPlacement = document.getElementById("top-placement"); const playerMatchesDiv = document.createElement("div"); playerMatchesDiv.style.display = "flex"; playerMatchesDiv.style.justifyContent = "center"; playerMatchesDiv.style.marginTop = "20px"; const playerMatchesTable = document.createElement("table"); playerMatchesTable.style.borderCollapse = "collapse"; playerMatchesTable.style.margin = "0 auto"; playerMatchesDiv.appendChild(playerMatchesTable); topPlacement.parentNode.insertBefore( playerMatchesDiv, topPlacement.nextSibling ); // fetch teamId fetch(window.location.href) .then((response) => response.text()) .then((data) => { const parser = new DOMParser(); const doc = parser.parseFromString(data, "text/html"); const username = doc.getElementById("header-username").textContent; return fetch( `https://www.managerzone.com/xml/manager_data.php?sport_id=1&username=${username}` ) .then((response) => response.text()) .then((xmlData) => { const xmlDoc = parser.parseFromString(xmlData, "text/xml"); const teamElements = Array.from(xmlDoc.getElementsByTagName("Team")); const soccerTeamElement = teamElements.find( (teamElement) => teamElement.getAttribute("sport") === "soccer" ); teamId = soccerTeamElement.getAttribute("teamId"); console.log("teamId: " + teamId); // debug borussia dortmund // Fetch friendlies information after teamId is retrieved return fetch( "https://www.managerzone.com/ajax.php?p=challenge&sub=personal-challenge-template&sport=soccer" ); }) .then((response) => response.text()) .then((data) => { const parser = new DOMParser(); const doc = parser.parseFromString(data, "text/html"); // div with id "friendly_series_schedule" const scheduleDiv = doc.getElementById("friendly_series_schedule"); // div with class "calendar" inside the scheduleDiv const calendarDiv = scheduleDiv.querySelector(".calendar"); // form with id "saveMatchTactics" inside the calendarDiv const calendarForm = calendarDiv.querySelector("#saveMatchTactics"); // divs with class "fss-has-matches" that do not have class "row-hidden" inside the form const currentAndNextWeekfriendlyMatchesDivs = Array.from( calendarForm.querySelectorAll(".fss-has-matches:not(.row-hidden)") ); // grab current week const currentWeekFriendlyMatchesDiv = currentAndNextWeekfriendlyMatchesDivs[0]; // grab divs with class d1, d3, d4, d5, and d7 const matchDivs = ["d1", "d3", "d4", "d5", "d7"].map((className) => currentWeekFriendlyMatchesDiv.querySelector(`.${className}`) ); // for each div, find the a elements with class "score-shown green" and extract the match ID from href, store them in array const matchIds = matchDivs.flatMap((div) => { const matchScoresLinks = Array.from( div.querySelectorAll("a.score-shown:not(.gray)") ); return matchScoresLinks.map((link) => { const matchUrl = link.getAttribute("href"); const matchId = matchUrl.split("mid=")[1].split("&")[0]; return matchId; }); }); // debug jeonnam dragons console.log(matchIds); // fetch match info for each matchId matchIds.forEach((matchId) => { fetch( `https://www.managerzone.com/xml/match_info.php?sport_id=1&match_id=${matchId}` ) .then((response) => response.text()) .then((xmlData) => { const xmlDoc = parser.parseFromString(xmlData, "text/xml"); // get our team element const teamElements = Array.from( xmlDoc.getElementsByTagName("Team") ); const ourTeamElement = teamElements.find( (teamElement) => teamElement.getAttribute("id") === teamId ); // get the players const playerElements = Array.from( ourTeamElement.getElementsByTagName("Player") ); // extract the IDs and names from players, store in map and update their appearances playerElements.forEach((playerElement) => { const playerId = playerElement.getAttribute("id"); const playerName = playerElement.getAttribute("name"); // increment their count, or set to 1 if no entry yet if (appearances.has(playerId)) { const playerInfo = appearances.get(playerId); playerInfo.appearances += 1; appearances.set(playerId, playerInfo); } else { appearances.set(playerId, { name: playerName, appearances: 1, }); } // debug monstro yukika console.log( `Player ID: ${playerId}, Player Name: ${playerName}, Appearances: ${ appearances.get(playerId).appearances }` ); }); // fill table with the player appearances playerMatchesTable.innerHTML = ""; appearances.forEach((playerInfo, playerId) => { const row = document.createElement("tr"); const nameCell = document.createElement("td"); const link = document.createElement("a"); link.href = `https://www.managerzone.com/?p=players&pid=${playerId}`; link.textContent = playerInfo.name; link.style.color = "white"; nameCell.appendChild(link); const appearancesCell = document.createElement("td"); appearancesCell.textContent = playerInfo.appearances; appearancesCell.style.color = "white"; row.appendChild(nameCell); row.appendChild(appearancesCell); playerMatchesTable.appendChild(row); }); }); }); }); }) .catch((e) => { console.warn("Error: ", e); }); })();