and for this rows, the team name is not a hyperlink
const name = row.querySelector("a.team-name");
if (name?.href) {
// this is info row
row.teamId = extractTeamId(name.href);
row.url = getSquadSummaryLink(row.teamId);
clashAddRankElements(row, mobileView);
row.playedMatches = [];
} else {
// this is match row (in mobile view)
// expand to match the previous row
row.querySelector("td").colSpan = mobileView ? "3" : "6";
row.previousSibling.playedMatches?.push(row);
}
}
const teams = [...rows].filter((team) => team.url?.length > 0);
clashCalculateRankOfTeams(teams, mobileView);
}
/* *********************** Federation Page ********************************** */
function federationGetUsername(node) {
return node?.querySelector("a")?.innerText;
}
async function federationUpdateMemberInfo(member, username, sport) {
let values = 0;
let currency = "";
const teamXmlUrl = `https://www.managerzone.com/xml/manager_data.php?username=${username}`;
const { teamId, teamName } = await fetch(teamXmlUrl)
.then((resp) => resp.text())
.then((content) => {
const parser = new DOMParser();
return parser.parseFromString(content, "text/xml");
}).then((doc) => {
const teamId = doc.querySelector(`Team[sport="${sport}"]`).getAttribute("teamId");
const teamName = doc.querySelector(`Team[sport="${sport}"]`).getAttribute("teamName");
return { teamId, teamName };
})
.catch((error) => {
console.warn(error);
return { teamId: null, teamName: null };
});
if (teamId) {
const squadUrl = getSquadSummaryLink(teamId);
await fetch(squadUrl)
.then((resp) => resp.text())
.then((content) => {
const parser = new DOMParser();
return parser.parseFromString(content, "text/html");
}).then((doc) => {
currency = getClubCurrency(doc);
values = getClubTopPlyers(doc).values;
})
.catch((error) => {
console.warn(error);
});
}
const name = document.createElement("div");
name.style.color = "blue";
name.style.width = "100%";
name.style.marginTop = "0.5em";
name.title = teamName;
name.innerHTML = `Team: ${teamName.length > 20 ? teamName.substring(0, 16) + " >>>" : teamName}`;
member.querySelector("td").appendChild(name);
const value = document.createElement("div");
value.style.color = "blue";
value.style.width = "100%";
value.style.marginTop = "0.5em";
value.innerHTML = `Top${sport === "soccer" ? 11 : 21}: ${formatBigNumber(values, ",")} ${currency}`;
member.querySelector("td").appendChild(value);
const separator = document.createElement("hr");
separator.style.marginBottom = "-3px";
member.querySelector("td").appendChild(separator);
return {
member,
values,
currency
};
}
function federationGetTableHeader(target) {
const thead = target.querySelector("thead td");
return thead.innerText;
}
function federationSetTableHeader(target, text) {
const thead = target.querySelector("thead td");
thead.innerText = text;
}
async function federationSortTeamsByTopPlayers(target) {
const sport = getSportType();
const jobs = [];
const tbody = target?.querySelector(" tbody");
for (const member of tbody.children) {
const username = federationGetUsername(member);
if (username) {
jobs.push(federationUpdateMemberInfo(member, username, sport));
}
}
if (jobs.length > 0) {
const tableHeader = federationGetTableHeader(target);
let dots = 0;
const loadingInterval = setInterval(() => {
federationSetTableHeader(target, tableHeader + " " + ".".repeat(1 + (dots % 3)));
dots++;
}, 1000);
const members = await Promise.all(jobs);
clearInterval(loadingInterval);
federationSetTableHeader(target, tableHeader + " ▼");
members.sort((a, b) => b.values - a.values);
const newOrder = members.map((t) => t.member);
let rank = 0;
for (const row of newOrder) {
rank++;
row.className = rank % 2 ? "odd" : "even";
}
tbody.replaceChildren(...newOrder);
let totalValue = 0;
for (const member of members) {
totalValue += member.values;
}
const total = document.createElement("tr");
total.style.color = "blue";
total.style.textAlign = "center";
total.style.width = "100%";
total.innerHTML = `Total: ${formatBigNumber(totalValue, ",")} ${members[0].currency} | `;
tbody.appendChild(total);
}
}
function federationFrontPage() {
const content = document.getElementById('federation-content');
if (content) {
const target = document.getElementById("federation_clash_members_list");
if (target && !target.topPlayersInjected) {
target.topPlayersInjected = true;
federationSortTeamsByTopPlayers(target);
}
const callback = () => {
const target = document.getElementById("federation_clash_members_list");
if (target && !target.topPlayersInjected) {
target.topPlayersInjected = true;
federationSortTeamsByTopPlayers(target);
}
};
const observer = new MutationObserver(callback);
const config = { childList: true, subtree: true };
observer.observe(content, config);
}
}
/* *********************** Match ********************************** */
// ---------------- Top Players -------------
function matchCreateTopPlayersHeader(sport = "soccer") {
const thead = document.createElement("thead");
const tr = document.createElement("tr");
const title = document.createElement("th");
const count = document.createElement("th");
const tops = document.createElement("th");
const age = document.createElement("th");
const playerCount = sport === "soccer" ? 11 : 21;
title.classList.add("header");
title.innerText = "Group";
title.style.textAlign = "center";
title.style.textDecoration = "none";
count.classList.add("header");
count.innerText = "Count";
count.title = "Number of Players";
count.style.textAlign = "center";
count.style.textDecoration = "none";
tops.classList.add("header");
tops.innerHTML = `Top ${playerCount}`;
tops.title = "Value of Top 11 Players";
tops.style.textAlign = "center";
tops.style.textDecoration = "none";
age.classList.add("header");
age.innerHTML = "Average Age";
age.title = `Average Age of Top ${playerCount} Players`;
age.style.textAlign = "center";
age.style.textDecoration = "none";
tr.appendChild(title);
tr.appendChild(count);
tr.appendChild(tops);
tr.appendChild(age);
thead.appendChild(tr);
return thead;
}
function matchCreateTopPlayersBody(rows, currency = "USD", sport = "soccer") {
const tbody = document.createElement("tbody");
for (const row of rows) {
const tr = document.createElement("tr");
const title = document.createElement("td");
const count = document.createElement("td");
const top = document.createElement("td");
const age = document.createElement("td");
title.innerHTML = `${row.title}`;
title.classList.add("hitlist-compact-list-column");
count.innerHTML = `${row.count}`;
count.style.textAlign = "center";
top.style.textAlign = "end";
age.style.textAlign = "center";
if (sport === "soccer") {
top.innerText = `${formatBigNumber(row.top11)} ${currency}`;
age.innerText = `${formatAverageAge(row.top11Age)}`;
} else {
top.innerText = `${formatBigNumber(row.top21)} ${currency}`;
age.innerText = `${formatAverageAge(row.top21Age)}`;
}
tr.appendChild(title);
tr.appendChild(count);
tr.appendChild(top);
tr.appendChild(age);
tbody.appendChild(tr);
}
return tbody;
}
function matchCreateSummaryTable(rows, currency = "USD", sport = "soccer") {
const table = document.createElement("table");
const thead = matchCreateTopPlayersHeader(sport);
const tbody = matchCreateTopPlayersBody(rows, currency, sport);
table.classList.add("mazyar-table", "tablesorter", "hitlist", "marker", "hitlist-compact-list-included");
table.style.marginBottom = "10px";
table.style.marginTop = "2em";
table.appendChild(thead);
table.appendChild(tbody);
return table;
}
function matchInjectTopPlayersValues(players, team, currency, sport) {
const summary = squadGetPlayersInfo(players, sport);
const table = matchCreateSummaryTable(summary, currency, sport);
const div = document.createElement("div");
div.classList.add("mazyar-flex-container");
div.appendChild(table);
team.appendChild(div);
}
// ---------------- Lineup --------------------
function matchGetLineupPlayers(teamNode, teamPlayers, sport) {
const lineup = [];
const teamPlayerIDs = teamPlayers.map((p) => p.id);
const lineupPlayers = teamNode.querySelectorAll("tbody tr");
for (const playerNode of lineupPlayers) {
const pos = playerNode.querySelector("td:nth-child(2)");
const order = Number(pos.querySelector("span").innerText);
const pid = extractPlayerID(playerNode.querySelector("a").href);
const playerInfo = {
id: pid,
order,
exPlayer: !teamPlayerIDs.includes(pid),
starting: sport === "hockey" || order < 12,
value: teamPlayers.find((p) => p.id === pid)?.value ?? 0,
age: teamPlayers.find((p) => p.id === pid)?.age,
};
const shirtNumber = playerNode.querySelector("td");
if (playerInfo.starting) {
shirtNumber.style.background = "lightgreen";
}
if (playerInfo.exPlayer) {
shirtNumber.style.background = "#DD0000";
}
const value = document.createElement("td");
value.innerText = `${playerInfo.value ? formatBigNumber(playerInfo.value, ",") : "N/A"}`;
playerNode.appendChild(value);
const age = document.createElement("td");
age.innerText = `${playerInfo.age ?? "N/A"}`;
playerNode.appendChild(age);
lineup.push(playerInfo);
}
return lineup;
}
function matchInjectLineupValues(players, team, currency, sport) {
const valueHeader = document.createElement("td");
valueHeader.innerText = `Value`;
valueHeader.title = `Player Value (in ${currency})`;
team.querySelector("table thead tr:nth-child(2)").appendChild(valueHeader);
team.querySelector("table tfoot tr td").colSpan += 1;
const ageHeader = document.createElement("td");
ageHeader.innerText = `Age`;
ageHeader.title = `Player Age`;
team.querySelector("table thead tr:nth-child(2)").appendChild(ageHeader);
team.querySelector("table tfoot tr td").colSpan += 1;
team.querySelector("table thead tr td").colSpan += 1;
const lineupPlayers = matchGetLineupPlayers(team, players, sport).filter((player) => player.starting && !player.exPlayer);
const value = lineupPlayers?.map((player) => player.value).reduce((a, b) => a + b, 0);
let averageAge = lineupPlayers?.map((player) => player.age).reduce((a, b) => a + b, 0);
if (averageAge) {
averageAge /= lineupPlayers.length;
}
const div = document.createElement("div");
div.innerHTML =
`${sport === "soccer" ? "Starting " : ""}Lineup` +
`
Value: ${formatBigNumber(value, ",")} ${currency}` +
`
Average Age: ${formatAverageAge(averageAge)}` +
`
Note: YYY` +
` are ${sport === "soccer" ? "starting " : "current"} players and ` +
`NNN are ex-players.` +
`
ex-player's value is N/A and not included in Lineup Value calculation.`;
div.style.margin = "10px";
div.style.padding = "5px";
div.style.border = "2px solid green";
div.style.borderRadius = "10px";
const place = team.querySelector("table");
team.insertBefore(div, place);
}
async function matchAddTopPlayersValue(team, sport) {
const teamLink = team.querySelector("a").href;
const tid = extractTeamId(teamLink);
const { players, currency } = await getPlayersAndCurrency(tid, sport);
matchInjectTopPlayersValues(players, team, currency, sport);
}
async function matchAddLineupValues(team, sport) {
const teamLink = team.querySelector("a").href;
const tid = extractTeamId(teamLink);
const { players, currency } = await getPlayersAndCurrency(tid, sport);
matchInjectLineupValues(players, team, currency, sport);
}
function matchInjectTeamValues() {
const sport = getSportType();
const teams = document.querySelectorAll("div.team-table");
for (const team of teams) {
if (team.querySelector("table")) {
matchAddLineupValues(team, sport);
} else {
matchAddTopPlayersValue(team, sport);
}
}
}
/* ******************** In Progress Results ************************* */
function matchGetWinner(context) {
const h = Number(context.homeGoals);
const a = Number(context.awayGoals);
if (h > a) {
return context.homeId;
} else if (h < a) {
return context.awayId;
}
return null;
}
function matchUpdateResult(result, context) {
result.innerText = context.homeGoals + " - " + context.awayGoals;
if ([context.homeId, context.awayId].includes(context.targetId)) {
const winnerId = matchGetWinner(context);
if (winnerId) {
result.style.background = winnerId === context.targetId ? "#5D7F13" : "#930000";
result.style.color = "#fff";
} else {
// equal
result.style.background = "#F2D624";
result.style.color = "#000";
}
} else {
result.style.background = "coral";
result.style.color = "#fff";
}
}
function matchGetPossiblyInProgressMatches(section) {
let days = 0;
const possiblyInProgress = [];
for (const child of [...section.children]) {
const classes = [...child.classList];
if (classes?.includes("odd")) {
if (!child.updated) {
possiblyInProgress.push(child);
}
} else if (classes?.includes("group")) {
days += 1;
if (days == 3) {
break;
}
}
}
return possiblyInProgress;
}
function matchWaitAndInjectInProgressResults(timeout = 32000) {
const step = 500;
const interval = setInterval(() => {
const matchesSection = document.getElementById("fixtures-results-list");
if (matchesSection) {
const games = matchesSection.querySelectorAll("dd.group");
if (games.length > 0) {
matchAddInProgressResults(matchesSection);
clearInterval(interval);
}
} else {
timeout -= step;
if (timeout < 0) {
clearInterval(interval);
}
}
}, step);
}
function matchAttachChangeEventToFilterForm() {
const filterForm = document.getElementById("matchListForm");
if (filterForm && !filterForm.eventAttached) {
filterForm.eventAttached = true;
// Note: when this event is fired, it is removed from the element or element is changed somehow.
// so for now we will attach it again after each change event.
filterForm.addEventListener("change", matchWaitAndInjectInProgressResults);
}
}
function matchAddInProgressResults(section) {
matchAttachChangeEventToFilterForm();
const sport = getSportType();
const matches = matchGetPossiblyInProgressMatches(section);
// when you visit someone else fixture, this return its id
let teamId = extractTeamId(document.baseURI);
for (const match of matches) {
const result = match.querySelector("dd.teams-wrapper a.score-shown");
if (!isMatchInProgress(result.innerText)) {
match.updated = true;
continue;
}
const mid = extractMatchID(result.href);
// this always returns your id
const visitorId = extractTeamId(result.href);
const url = `http://www.managerzone.com/xml/match_info.php?sport_id=${sport === "soccer" ? 1 : 2}&match_id=${mid}`;
GM_xmlhttpRequest({
method: "GET",
url,
context: { match, result, teamId: teamId ?? visitorId },
onload: function (resp) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(resp.responseText, "text/xml");
if (!xmlDoc.querySelector("ManagerZone_Error")) {
const home = xmlDoc.querySelector(`Team[field="home"]`);
const away = xmlDoc.querySelector(`Team[field="away"]`);
const context = {
homeGoals: home.getAttribute("goals"),
homeId: home.getAttribute("id"),
awayGoals: away.getAttribute("goals"),
awayId: away.getAttribute("id"),
targetId: resp.context.teamId,
};
matchUpdateResult(resp.context.result, context);
}
resp.context.match.updated = true;
},
});
}
}
function matchInjectInProgressResults() {
if (mazyar.mustDisplayInProgressResults()) {
const fixturesLink = document.getElementById("matches_sub_nav")?.querySelector("div.flex-grow-0 span a");
if (fixturesLink) {
fixturesLink.addEventListener("click", matchWaitAndInjectInProgressResults);
if ([...fixturesLink.classList].includes("selected")) {
const matchesSection = document.getElementById("fixtures-results-list");
if (matchesSection) {
matchAddInProgressResults(matchesSection);
}
}
}
}
}
/* *********************** League ********************************** */
function tableGetAgeLimit(url) {
if (url.search("type=u23") > -1) {
return 23;
} else if (url.search("type=u21") > -1) {
return 21;
} else if (url.search("type=u18") > -1) {
return 18;
}
return 99;
}
function tableGetValeTitle(sport = "soccer", age = 99) {
const count = sport === "soccer" ? 11 : 21;
if ([18, 21, 23].includes(age)) {
return `Top ${count} - U${age}`;
}
return `Top ${count} - All`;
}
function tableGetAgeClass(age = 99) {
if ([18, 21, 23].includes(age)) {
return `values-u${age}`;
}
return "values-all";
}
function tableGetNextAge(age = 99) {
if (age === 99) {
return 23;
} else if (age === 23) {
return 21;
} else if (age === 21) {
return 18;
}
return 99;
}
function tableDisplayAgeInfo(team, ageLimit) {
const target = team ?? document;
target.querySelectorAll("td.mazyar-injected span")?.forEach((el) => {
el.style.display = "none";
});
const ageClass = tableGetAgeClass(ageLimit);
target.querySelectorAll("td.mazyar-injected span." + ageClass)?.forEach((el) => {
el.style.display = "unset";
});
}
function tableGetSquadSummaryUrl(team) {
const teamLink = team.querySelector("td:nth-child(2) a:last-child")?.href;
const tid = extractTeamId(teamLink);
return getSquadSummaryLink(tid);
}
function tableModifyTeamInBodyForPcView(team, url) {
const teamValue = document.createElement("td");
team.appendChild(teamValue);
const ageValue = document.createElement("td");
team.appendChild(ageValue);
teamValue.innerText = "loading...";
teamValue.classList.add("mazyar-injected", "team-value");
teamValue.title = "Click to see squad summary";
teamValue.style.textAlign = "center";
teamValue.style.whiteSpace = "nowrap";
teamValue.style.padding = "auto 3px";
teamValue.onclick = () => {
mazyar.displaySquadSummary(url);
};
ageValue.classList.add("mazyar-injected", "age-value");
ageValue.innerText = "...";
ageValue.style.textAlign = "center";
ageValue.style.whiteSpace = "nowrap";
ageValue.style.padding = "auto 3px";
}
function tableAddTeamToBodyForMobileView(team, url) {
const firstRow = team.cloneNode(true);
team.parentNode.insertBefore(firstRow, team.nextSibling);
const secondRow = document.createElement("tr");
firstRow.parentNode.insertBefore(secondRow, firstRow.nextSibling);
const value = document.createElement("td");
const age = document.createElement("td");
secondRow.appendChild(value);
secondRow.appendChild(age);
firstRow.children[0].rowSpan = "2";
firstRow.children[1].rowSpan = "2";
firstRow.classList.add("responsive-show", "mazyar-responsive-show");
secondRow.classList.add("responsive-show", "mazyar-responsive-show");
value.colSpan = "6";
value.innerText = "loading...";
value.classList.add("mazyar-injected", "team-value");
value.title = "Click to see squad summary";
value.style.textAlign = "center";
value.style.whiteSpace = "nowrap";
value.style.padding = "auto 3px";
value.style.backgroundColor = "aquamarine";
value.onclick = () => {
mazyar.displaySquadSummary(url);
};
age.colSpan = "2";
age.classList.add("mazyar-injected", "age-value");
age.innerText = "...";
age.style.textAlign = "center";
age.style.whiteSpace = "nowrap";
age.style.padding = "auto 3px";
age.style.backgroundColor = "aqua";
// modify borders to match the original view
firstRow.style.borderBottom = "inherit";
secondRow.style.borderBottom = team.style.borderBottom;
return secondRow;
}
async function tableAddTeamTopPlayersInfo(team, ageLimit, sport) {
const url = tableGetSquadSummaryUrl(team);
// for mobile
const mobileView = tableAddTeamToBodyForMobileView(team, url);
// for pc
team.classList.add("responsive-hide");
tableModifyTeamInBodyForPcView(team, url);
const pcView = team;
const tid = extractTeamId(url);
const { players, currency } = await getPlayersAndCurrency(tid, sport);
const playersOfSport = sport === "soccer" ? 11 : 21;
const all = filterPlayers(players, playersOfSport, 0, 99);
const u23 = filterPlayers(players, playersOfSport, 0, 23);
const u21 = filterPlayers(players, playersOfSport, 0, 21);
const u18 = filterPlayers(players, playersOfSport, 0, 18);
for (const view of [pcView, mobileView]) {
const valueElement = view.querySelector("td.team-value");
// prettier-ignore
valueElement.innerHTML =
`${formatBigNumber(all?.values)} ${currency}` +
`${formatBigNumber(u23?.values)} ${currency}` +
`${formatBigNumber(u21?.values)} ${currency}` +
`${formatBigNumber(u18?.values)} ${currency}`;
valueElement.style.textAlign = "right";
const ageElement = view.querySelector("td.age-value");
// prettier-ignore
ageElement.innerHTML =
`${formatAverageAge(all?.avgAge)}` +
`${formatAverageAge(u23?.avgAge)}` +
`${formatAverageAge(u21?.avgAge)}` +
`${formatAverageAge(u18?.avgAge)}`;
tableDisplayAgeInfo(view, ageLimit);
}
}
function tableDisplayNextAgeInfo() {
const headers = document.querySelectorAll("th.team-value-header");
let changed = false;
for (const header of headers) {
header.age = tableGetNextAge(header.age);
header.innerText = tableGetValeTitle(header.sport, header.age);
if (!changed) {
changed = true;
tableDisplayAgeInfo(null, header.age);
}
}
}
function tableModifyHeaderForPcView(header, sport = "soccer", ageLimit = 99) {
const firstRow = header.querySelector("tr");
const value = document.createElement("th");
firstRow.appendChild(value);
const age = document.createElement("th");
firstRow.appendChild(age);
value.classList.add("responsive-hide", "team-value-header");
value.style.textAlign = "center";
value.innerText = tableGetValeTitle(sport, ageLimit);
value.title = "Click to see other age limits";
value.age = ageLimit;
value.sport = sport;
value.onclick = tableDisplayNextAgeInfo;
age.classList.add("responsive-hide");
age.style.textAlign = "center";
age.innerText = `Age`;
age.title = "Average Age Of the Top Players";
}
function tableAddHeaderForMobileView(mainHeader, sport = "soccer", ageLimit = 99) {
const mobileHeader = mainHeader.cloneNode(true);
mainHeader.parentNode.insertBefore(mobileHeader, mainHeader.nextSibling);
const secondRow = document.createElement("tr");
mobileHeader.appendChild(secondRow);
const value = document.createElement("th");
const age = document.createElement("th");
secondRow.appendChild(value);
secondRow.appendChild(age);
mobileHeader.classList.add("responsive-show", "mazyar-responsive-show");
const firstRow = mobileHeader.querySelector("tr");
firstRow.children[0].rowSpan = "2";
firstRow.children[1].rowSpan = "2";
value.classList.add("team-value-header");
value.colSpan = "6";
value.style.textAlign = "center";
value.innerText = tableGetValeTitle(sport, ageLimit);
value.title = "Click to see other age limits";
value.age = ageLimit;
value.sport = sport;
value.onclick = tableDisplayNextAgeInfo;
value.style.backgroundColor = "aquamarine";
age.colSpan = "2";
age.style.textAlign = "center";
age.innerText = `Age`;
age.title = "Average Age Of the Top Players";
age.style.backgroundColor = "aqua";
}
function tableInjectInProgressResults() {
const sport = getSportType();
const matches = document.querySelectorAll("table.hitlist.marker td > a");
const inProgressMatches = [...matches].filter((match) => isMatchInProgress(match.innerText));
for (const match of inProgressMatches) {
const mid = extractMatchID(match.href);
const url = `http://www.managerzone.com/xml/match_info.php?sport_id=${sport === "soccer" ? 1 : 2}&match_id=${mid}`;
GM_xmlhttpRequest({
method: "GET",
url,
context: { match },
onload: function (resp) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(resp.responseText, "text/xml");
if (!xmlDoc.querySelector("ManagerZone_Error")) {
const home = xmlDoc.querySelector(`Team[field="home"]`).getAttribute("goals");
const away = xmlDoc.querySelector(`Team[field="away"]`).getAttribute("goals");
resp.context.match.innerText = home + " - " + away;
resp.context.match.classList.add("mazyar-in-progress-result");
}
},
});
}
}
function tableAddTopPlayersInfo(table) {
const ageLimit = tableGetAgeLimit(document.baseURI);
const sport = getSportType(document);
const mainHeader = table.querySelector("thead");
// for mobile
tableAddHeaderForMobileView(mainHeader, sport, ageLimit);
// for PC
mainHeader.classList.add("responsive-hide");
tableModifyHeaderForPcView(mainHeader, sport, ageLimit);
const teams = table.querySelectorAll("tbody tr");
for (const team of teams) {
tableAddTeamTopPlayersInfo(team, ageLimit, sport);
}
}
function tableWaitAndInjectTopPlayersInfo(timeout = 16000) {
const step = 500;
const interval = setInterval(() => {
const table = document.querySelector("table.nice_table");
if (table) {
clearInterval(interval);
if (!table.TopPlayersInfoInjected) {
table.TopPlayersInfoInjected = true;
if (mazyar.mustDisplayTopPlayersInTables()) {
tableAddTopPlayersInfo(table);
}
if (mazyar.mustDisplayInProgressResults()) {
tableInjectInProgressResults();
}
}
} else {
timeout -= step;
if (timeout < 0) {
clearInterval(interval);
}
}
}, step);
}
function tableInjectTopPlayersToOfficialLeague() {
// default sub-page (or tab) for leagues is Table. so try to inject team value after table is loaded
tableWaitAndInjectTopPlayersInfo();
// also add 'onclick' handler to Table tab
const links = document.getElementsByTagName("a");
for (const link of links) {
if (["p=league", "sub=table"].every((text) => link.href.indexOf(text) > -1)) {
link.onclick = tableWaitAndInjectTopPlayersInfo;
}
}
}
function tableInjectTopPlayersInfoToFriendlyLeague() {
const links = document.getElementsByTagName("a");
for (const link of links) {
if (["p=friendlySeries", "sub=standings"].every((text) => link.href.indexOf(text) > -1)) {
link.onclick = tableWaitAndInjectTopPlayersInfo;
}
}
}
function tableInjectTopPlayersInfoToCup() {
const links = document.getElementsByTagName("a");
for (const link of links) {
if (["p=cups", "sub=groupplay"].every((text) => link.href.indexOf(text) > -1)) {
link.onclick = tableWaitAndInjectTopPlayersInfo;
}
}
}
/* *********************** Schedule ********************************** */
function scheduleClearAllColorings(teams) {
teams.forEach((team) => {
team.style.backgroundColor = team.originalColor;
});
}
function scheduleResultColors(result) {
if (result.length < 2) {
return ["cyan", "cyan"];
}
if (result[0] === "X") {
return ["cyan", "cyan"];
}
if (result[0] < result[1]) {
return ["orangered", "lime"];
} else if (result[0] > result[1]) {
return ["lime", "orangered"];
}
return ["yellow", "yellow"];
}
function scheduleColorizeThisTeam(teams, selected) {
teams.forEach((team) => {
if (team.innerText === selected) {
const tr = team.parentNode;
const opponents = tr.querySelectorAll("td:nth-child(odd)");
const result = tr.querySelector("td:nth-child(2)").innerText.split(" - ");
const colors = scheduleResultColors(result);
if (opponents[0].innerText === selected) {
opponents[0].style.setProperty("background-color", colors[0], "important");
}
if (opponents[1].innerText === selected) {
opponents[1].style.setProperty("background-color", colors[1], "important");
}
}
});
}
function scheduleInjectColoring(tab) {
let selected = "";
const teams = tab.querySelectorAll("div.mainContent td:nth-child(odd)");
for (const team of teams) {
team.originalColor = team.style.backgroundColor;
team.onclick = null;
team.addEventListener("click", function (evt) {
if (selected && selected !== this.innerText) {
// new team is selected
scheduleClearAllColorings(teams);
selected = "";
}
if (selected === this.innerText) {
// de-colorize
selected = "";
scheduleClearAllColorings(teams);
} else {
// colorize
selected = this.innerText;
scheduleColorizeThisTeam(teams, selected);
}
});
}
}
function scheduleHasDuplicateName(round) {
const teams = round.querySelectorAll("td:nth-child(odd)");
const names = [...teams].map((t) => t.innerText);
return hasDuplicates(names);
}
function scheduleWaitAndInjectColoring(timeout = 16000) {
const step = 500;
const interval = setInterval(() => {
const firstRound = document.querySelector("div[aria-labelledby='league_tab_schedule'] div.mainContent");
if (firstRound) {
const schedule = firstRound.parentNode;
if (scheduleHasDuplicateName(firstRound)) {
const note = document.createElement("p");
note.innerHTML = `Note: Some teams have similar names. Coloring will not work as expected.`;
note.style.fontSize = "1.2em";
schedule.insertBefore(note, schedule.firstChild);
}
clearInterval(interval);
scheduleInjectColoring(schedule);
} else {
timeout -= step;
if (timeout < 0) {
clearInterval(interval);
}
}
}, step);
}
function scheduleInjectColoringToOfficialLeague() {
const link = document.getElementById("league_tab_schedule");
if (link) {
const tab = link.parentNode;
if (!tab.coloringInjected) {
tab.coloringInjected = true;
tab.onclick = scheduleWaitAndInjectColoring;
}
}
}
/* *********************** Transfer Agent ********************************** */
function transferGetInputValue(el) {
if (el.type == "checkbox") {
return el.checked ? 1 : 0;
}
return el.value;
}
function transferGetSearchParams() {
const inputs = document.getElementById("searchform")?.querySelectorAll("input,select");
return [...inputs].map((el) => `&${el.name}=${transferGetInputValue(el)}`).join("");
}
function transferGetFilterNameFromUrl() {
const url = new URL(document.URL);
const params = new URLSearchParams(url.search);
if (params.has("mzy_filter_name")) {
return params.get("mzy_filter_name");
}
return null;
}
function transferLoadFilter(filterParams) {
const url = new URL(`https://www.managerzone.com/?${filterParams}`);
const params = new URLSearchParams(url.search);
for (const param of params) {
const element = document.getElementById(param[0]);
if (element) {
if (element.type === "text") {
element.value = param[1];
element.dispatchEvent(new Event("blur"));
} else if (element.type === "checkbox") {
element.checked = param[1] === "1";
} else if (element.tagName === "SELECT") {
element.value = param[1];
}
}
}
}
function transferInjectButtons(form) {
const target = form.querySelector("div.buttons-wrapper.clearfix");
if (target) {
const filterButton = createMzStyledButton("MZY Filters", "red", "floatLeft");
filterButton.style.margin = "0";
filterButton.onclick = () => {
document.getElementById("mazyar-transfer-filter-hits")?.click();
};
const saveButton = createMzStyledButton("MZY Save Filter", "blue", "floatLeft");
saveButton.style.margin = "0";
saveButton.onclick = () => {
const params = transferGetSearchParams();
mazyar.displayFilterSaveMenu(params);
};
target.appendChild(filterButton);
target.appendChild(saveButton);
}
}
function transferCreateScoutOptions() {
const div = document.createElement("div");
div.classList.add("mazyar-flex-container-row");
div.style.justifyContent = "left";
div.style.marginTop = "6px";
const highs = document.createElement("div");
highs.classList.add("mazyar-flex-container-row");
highs.style.justifyContent = "left";
highs.style.border = "1px inset black";
highs.style.marginRight = "2rem";
const lows = document.createElement("div");
lows.classList.add("mazyar-flex-container-row");
lows.style.justifyContent = "left";
lows.style.border = "1px outset black";
lows.style.marginRight = "2rem";
const transferOptions = mazyar.getTransferOptions();
const H4 = createMenuCheckBox("H4", transferOptions.H4, { margin: "auto 4px auto 1px" });
const H3 = createMenuCheckBox("H3", transferOptions.H3, { margin: "auto 4px auto 1px" });
const L2 = createMenuCheckBox("L2", transferOptions.L2, { margin: "auto 4px auto 1px" });
const L1 = createMenuCheckBox("L1", transferOptions.L1, { margin: "auto 4px auto 1px" });
const hide = createMenuCheckBox("Hide Others", transferOptions.hide, { margin: "auto 4px auto 1px" });
H4.onclick = () => {
mazyar.updateTransferOptions("H4", H4.querySelector("input[type=checkbox]").checked);
mazyar.updateDisplayForTransferSearchResults(true);
};
H3.onclick = () => {
mazyar.updateTransferOptions("H3", H3.querySelector("input[type=checkbox]").checked);
mazyar.updateDisplayForTransferSearchResults(true);
};
L2.onclick = () => {
mazyar.updateTransferOptions("L2", L2.querySelector("input[type=checkbox]").checked);
mazyar.updateDisplayForTransferSearchResults(true);
};
L1.onclick = () => {
mazyar.updateTransferOptions("L1", L1.querySelector("input[type=checkbox]").checked);
mazyar.updateDisplayForTransferSearchResults(true);
};
hide.onclick = () => {
mazyar.updateTransferOptions("hide", hide.querySelector("input[type=checkbox]").checked);
mazyar.updateDisplayForTransferSearchResults(true);
};
highs.appendChild(H4);
highs.appendChild(H3);
lows.appendChild(L2);
lows.appendChild(L1);
div.appendChild(highs);
div.appendChild(lows);
div.appendChild(hide);
div.appendChild(hide);
return div;
}
function transferInject() {
const form = document.getElementById("searchform");
if (form) {
const scoutOptions = transferCreateScoutOptions();
form.parentNode.appendChild(scoutOptions);
transferInjectButtons(form);
const filterName = transferGetFilterNameFromUrl();
if (filterName) {
const filterParams = mazyar.getFilterParams(filterName);
if (filterParams) {
setTimeout(() => {
transferLoadFilter(filterParams);
document.getElementById("searchb")?.click();
}, 5000);
}
}
}
}
/* *********************** Predictor ********************************** */
async function getNationalRankings() {
const rankings = [];
const url = 'https://www.managerzone.com/?p=rank&sub=countryrank';
const resp = await fetch(url).catch((error) => {
console.warn(error);
});
if (resp) {
const parser = new DOMParser();
const doc = parser.parseFromString(await resp.text(), "text/html");
const teams = doc.querySelectorAll("table#countryRankTable tbody tr");
for (const team of teams) {
const columns = team.querySelectorAll("td");
let name = columns[2].innerText;
if (name === 'United States') {
name = 'USA';
}
rankings.push({
name,
rank: Number(columns[0].innerText),
})
}
}
return rankings;
}
function predictorAttachInfo(match, teams, rankings) {
teams[0].rank = rankings.find((t) => t.name.trim() === teams[0].name.trim())?.rank;
teams[1].rank = rankings.find((t) => t.name.trim() === teams[1].name.trim())?.rank;
teams[0].valueColor = (teams[0].value > teams[1].value) ? "blue" : "unset";
teams[1].valueColor = (teams[1].value > teams[0].value) ? "blue" : "unset";
teams[0].rankColor = (teams[0].rank < teams[1].rank) ? "blue" : "unset";
teams[1].rankColor = (teams[1].rank < teams[0].rank) ? "blue" : "unset";
const tbody = document.createElement("tbody");
for (const team of teams) {
tbody.innerHTML += `
${team.name} |
[Rank: ${team.rank ?? 'xx'}] |
[Value: ${formatBigNumber(team.value)} ${team.currency}] |
`;
}
const table = document.createElement("table");
table.appendChild(tbody);
table.classList.add("mazyar-table");
const div = document.createElement("div");
div.appendChild(table);
const target = match.querySelector("td");
target.appendChild(div);
}
async function predictorInjectRecommendation(match, rankings) {
const href = match.querySelector("td > a")?.href;
const url = href.replace("&play=2d", "").replace("&type=2d", "");
const sport = getSportType();
const resp = await fetch(url).catch((error) => {
console.warn(error);
});
if (resp) {
const results = [];
const parser = new DOMParser();
const doc = parser.parseFromString(await resp.text(), "text/html");
const teams = doc.querySelectorAll("div.team-table");
for (const team of teams) {
const link = team.querySelector("a");
const name = link.innerText;
const tid = extractTeamId(link.href);
const { players, currency } = await getPlayersAndCurrency(tid, sport);
const playersOfSport = sport === "soccer" ? 11 : 21;
const all = filterPlayers(players, playersOfSport, 0, 99);
results.push({
name,
value: all?.values,
currency: currency,
})
}
predictorAttachInfo(match, results, rankings);
}
}
async function predictorInject() {
const rankings = await getNationalRankings();
const matches = document.querySelectorAll("#match-predictor-container table.match-list tbody tr");
const jobs = [];
for (const match of matches) {
jobs.push(predictorInjectRecommendation(match, rankings));
}
Promise.all(jobs);
}
/* *********************** Club Page ********************************** */
function countTrophies(doc) {
const trophies = doc.querySelectorAll("div.trophy-wrapper:not(.icon)");
return [...trophies].map((el) => {
const text = el.innerText.trim();
return text ? Number(text) : 1;
}).reduce((a, b) => a + b, 0);
}
async function getTrophiesCount(url) {
const resp = await fetch(url).catch((error) => {
console.warn(error);
return 'N/A';
});
if (resp?.status === 200) {
const parser = new DOMParser();
const doc = parser.parseFromString(await resp.text(), "text/html");
return countTrophies(doc);
}
return 'N/A'
}
function getTrophiesSection() {
const items = document.querySelectorAll("dl#trophies-achievements-lists dd");
for (const item of items) {
const link = item.querySelector("a");
if (link?.href.search("awards") > -1) {
return { trophies: item, url: link.href };
}
}
return { trophies: null, url: null };
}
async function addTrophyCountToClubPage() {
const { trophies, url } = getTrophiesSection();
if (trophies) {
const el = document.createTextNode(` (Total: ?)`);
trophies.querySelector("span")?.appendChild(el);
const count = await getTrophiesCount(url);
el.nodeValue = ` (Total: ${count})`;
}
}
/* *********************** Trainers ********************************** */
function trainersAddColumns(table) {
const headerRow = table.querySelector('thead tr');
const thSalary = document.createElement('th');
thSalary.textContent = 'Salary Range';
thSalary.style.textAlign = 'center';
thSalary.style.textDecoration = 'none';
headerRow?.appendChild(thSalary);
const thBonus = document.createElement('th');
thBonus.textContent = 'Bonus';
thBonus.style.textAlign = 'center';
thBonus.style.textDecoration = 'none';
headerRow?.appendChild(thBonus);
const thWeeks = document.createElement('th');
thWeeks.textContent = 'Weeks';
thWeeks.style.textAlign = 'center';
thWeeks.style.textDecoration = 'none';
headerRow?.appendChild(thWeeks);
const rows = table.querySelectorAll('tbody tr:not(.minified-view)');
rows.forEach(row => {
const salaryCell = row.insertCell(-1);
salaryCell.style.textAlign = 'center';
salaryCell.replaceChildren(createLoadingIcon2());
const bonusCell = row.insertCell(-1);
bonusCell.style.textAlign = 'center';
bonusCell.replaceChildren(createLoadingIcon2());
const weeksCell = row.insertCell(-1);
weeksCell.style.textAlign = 'center';
weeksCell.replaceChildren(createLoadingIcon2());
});
}
function trainersFetchSalaryAndWeeks(coachId, salaryCell, bonusCell, weeksCell) {
GM_xmlhttpRequest({
method: "GET",
url: `https://www.managerzone.com/?p=trainers&sub=offer&extra=freeagent&cid=${coachId}`,
onload: function (response) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, "text/html");
const salaryElement = doc.querySelector('td#salary_range nobr');
const weeksElement = doc.querySelector('td#weeks_range nobr');
const bonusElement = doc.querySelector("div#paper-content-wrapper > table td.contract_paper:nth-child(2)");
const salaryText = salaryElement?.innerText?.trim()?.match(/\d+(.*?\d+)* -(.*?\d+)+/g)?.[0];
salaryCell.textContent = salaryText ?? 'N/A';
salaryCell.style.fontStyle = 'normal';
const weeksText = weeksElement?.innerText?.trim()?.match(/\d+(.*?\d+)* -(.*?\d+)+/g)?.[0];
weeksCell.textContent = weeksText ?? 'N/A';
weeksCell.style.fontStyle = 'normal';
const bonusText = bonusElement?.innerText?.trim()?.match(/\d+(.*?\d+)*/g)?.[0];
bonusCell.textContent = bonusText ?? 'N/A';
bonusCell.style.fontStyle = 'normal';
}
});
}
function trainersUpdateSalariesAndWeeks(table) {
trainersAddColumns(table);
const rows = table?.querySelectorAll('tbody tr:not(.minified-view)');
rows.forEach(row => {
const linkElement = row.querySelector('td a[href*="cid="]');
if (linkElement) {
const urlParams = new URLSearchParams(linkElement.search);
const coachId = urlParams.get('cid');
const salaryCell = row.cells[row.cells.length - 3];
const bonusCell = row.cells[row.cells.length - 2];
const weeksCell = row.cells[row.cells.length - 1];
trainersFetchSalaryAndWeeks(coachId, salaryCell, bonusCell, weeksCell);
}
});
}
function trainersAddRequestedSalaries() {
const callback = () => {
const table = document.getElementById("coaches_list");
if (table && !table.injecting) {
table.injecting = true;
trainersUpdateSalariesAndWeeks(table);
}
};
const target = document.querySelector('div.in_page_navigation_top');
if (target) {
const observer = new MutationObserver(callback);
const config = { childList: true, subtree: true };
observer.observe(target, config);
}
}
/* *********************** Training Report ********************************** */
function trainingOpenPlayerTrainingCamp(player) {
player.querySelector('td.playerColumn a')?.click();
const interval = setInterval(() => {
const modal = document.querySelector("#lightbox_player_profile #players_container div.playerContainer");
if (modal) {
clearInterval(interval);
modal.querySelector('div.box_dark.player_tc_package_information a')?.click();
}
}, 300);
}
function trainingInjectCampOpener(table) {
const players = table.querySelectorAll("tbody tr");
for (const player of players) {
const campIcon = player.querySelector("td.dailyReportRightColumn img.content_middle");
if (campIcon) {
campIcon.style.cursor = "pointer";
campIcon.addEventListener("click", () => {
trainingOpenPlayerTrainingCamp(player);
});
}
}
}
function trainingAddCampOpenerToReport() {
const callback = () => {
const tables = document.querySelectorAll('#training_report > table');
for (const table of tables) {
if (!table.injecting) {
table.injecting = true;
trainingInjectCampOpener(table);
}
}
};
const target = document.getElementById('training_report');
if (target) {
const observer = new MutationObserver(callback);
const config = { childList: true, subtree: true };
observer.observe(target, config);
}
}
function trainingAddDays() {
const cssSelector = '#lightboxContent_player_profile #players_container';
const callback = () => {
const profile = document.querySelector(cssSelector);
if (profile && !profile.daysInjected) {
profile.daysInjected = true;
mazyar.trainingAddDaysAtThisClubToPlayerProfile(profile);
}
}
const target = document.body;
const config = { childList: true, subtree: true };
const observer = new MutationObserver(callback);
observer.observe(target, config);
}
/* *********************** Class ********************************** */
class Mazyar {
#modal = null; // dom element
#content = null; // dom element
#settings = {
in_progress_results: true,
top_players_in_tables: true,
transfer: false,
transfer_maxed: false,
mz_predictor: false,
player_comment: false,
coach_salary: false,
deadline: false,
days: {
display_in_profiles: false,
display_in_transfer: false,
display_in_training: false,
display_for_one_clubs: false,
}
};
#transferOptions = {
hide: true,
H4: false,
H3: false,
L2: false,
L1: false,
};
#filters = { soccer: [], hockey: [] }; // each key is like [{id, name, params, scout, interval}]
#sport = "soccer";
#deadlines = {}; // {pid1: {name, deadline}, ...}
#db;
#deadlineLockAcquired;
constructor() {
this.#initializeIndexedDb("Mazyar");
this.#fetchSettings();
this.#fetchTransferOptions();
this.#fetchFilters();
this.#sport = getSportType();
this.#addToolbar();
this.#createModal();
this.#showChangelog();
if (!this.isTransferFiltersEnabled()) {
this.#resetTransferOptions();
}
}
// -------------------------------- Settings -------------------------------------
#fetchSettings() {
this.#settings.in_progress_results = GM_getValue("display_in_progress_results", true);
this.#settings.top_players_in_tables = GM_getValue("display_top_players_in_tables", true);
this.#settings.transfer = GM_getValue("enable_transfer_filters", false);
this.#settings.transfer_maxed = GM_getValue("display_maxed_in_transfer", false);
this.#settings.mz_predictor = GM_getValue("mz_predictor", false);
this.#settings.player_comment = GM_getValue("player_comment", false);
this.#settings.coach_salary = GM_getValue("coach_salary", true);
this.#settings.deadline = GM_getValue("deadline", true);
this.#settings.days.display_in_profiles = GM_getValue("display_days_in_profiles", false);
this.#settings.days.display_in_transfer = GM_getValue("display_days_in_transfer", false);
this.#settings.days.display_in_training = GM_getValue("display_days_in_training", false);
this.#settings.days.display_for_one_clubs = GM_getValue("display_days_for_one_clubs", false);
}
#saveSettings() {
GM_setValue("display_in_progress_results", this.#settings.in_progress_results);
GM_setValue("display_top_players_in_tables", this.#settings.top_players_in_tables);
GM_setValue("enable_transfer_filters", this.#settings.transfer);
GM_setValue("display_maxed_in_transfer", this.#settings.transfer_maxed);
GM_setValue("mz_predictor", this.#settings.mz_predictor);
GM_setValue("player_comment", this.#settings.player_comment);
GM_setValue("coach_salary", this.#settings.coach_salary);
GM_setValue("deadline", this.#settings.deadline);
GM_setValue("display_days_in_profiles", this.#settings.days.display_in_profiles);
GM_setValue("display_days_in_transfer", this.#settings.days.display_in_transfer);
GM_setValue("display_days_in_training", this.#settings.days.display_in_training);
GM_setValue("display_days_for_one_clubs", this.#settings.days.display_for_one_clubs);
}
updateSettings(settings) {
this.#settings = settings;
this.#saveSettings();
this.#resetTransferOptions();
if (this.isTransferFiltersEnabled()) {
this.#checkAllFilters(true);
} else {
this.#setFilterHitsInToolbar(0);
}
}
mustDisplayInProgressResults() {
return this.#settings.in_progress_results;
}
mustDisplayTopPlayersInTables() {
return this.#settings.top_players_in_tables;
}
isTransferFiltersEnabled() {
return this.#settings.transfer;
}
#isTransferMaxedSkillsEnabled() {
return this.#settings.transfer_maxed;
}
isDaysAtThisClubEnabledForPlayerProfiles() {
return this.#settings.days.display_in_profiles;
}
#isDaysAtThisClubEnabledForTraining() {
return this.#settings.days.display_in_training;
}
#isDaysAtThisClubEnabledForTransferMarket() {
return this.#settings.days.display_in_transfer;
}
isDaysAtThisClubEnabledForOneClubPlayers() {
return this.#settings.days.display_for_one_clubs;
}
mustHelpWithPredictor() {
return this.#settings.mz_predictor;
}
#mustAddPlayerComment() {
return this.#settings.player_comment;
}
mustAddCoachSalaries() {
return this.#settings.coach_salary;
}
#isTransferDeadlineAlertEnabled() {
return this.#settings.deadline;
}
#mustMarkMaxedSkills() {
return this.#settings.transfer_maxed;
}
// -------------------------------- Database -------------------------------------
async #initializeIndexedDb(name = "db") {
this.#db = new Dexie(name);
this.#db.version(1).stores({
comment: "[sport+pid]",
scout: "[sport+pid]",
hit: "[fid+pid],deadline",
filter: "&fid,totalHits,scoutHits,lastCheck",
});
this.#db.version(2).stores({
scout: "[sport+pid],ts",
player: "[sport+pid],ts,maxed,days",
hide: "[sport+pid],ts",
deadline: "[sport+pid],ts,deadline, name",
}).upgrade(trans => {
return trans.table("scout").toCollection().modify(report => {
report.ts = 0;
});
});
this.#db.open();
await this.#cleanOutdatedDataFromIndexedDb();
const info = await navigator?.storage?.estimate();
if (info) {
console.log("ManagerZone IndexedDB size: " + formatFileSize(info.usage));
}
}
async #cleanOutdatedDataFromIndexedDb() {
const scoutOutdate = Date.now() - 7 * 24 * 60 * 60 * 1000;
await this.#db.scout.where("ts").below(scoutOutdate).delete().then((deleteCount) => {
console.log("Deleted " + deleteCount + " outdated scout reports");
}).catch((error) => {
console.warn(error);
});
const startOfDay = Date.now() - Date.now() % (24 * 60 * 60 * 1000);
await this.#db.player.where("ts").below(startOfDay).delete().then((deleteCount) => {
console.log("Deleted " + deleteCount + " outdated player profile.");
}).catch((error) => {
console.warn(error);
});
}
async #clearIndexedDb() {
// TODO: clear comments?
await this.#db.scout.clear();
await this.#db.hit.clear();
await this.#db.filter.clear();
await this.#db.player.clear();
await this.#db.hide.clear();
}
async #isPlayerInHideListInIndexDb(pid) {
const player = await this.#db.hide.get({ pid, sport: this.#sport });
return player?.ts > 0;
}
async #addPlayerToHideListInIndexDb(pid) {
if (pid) {
await this.#db.hide.put({ pid, sport: this.#sport, ts: Date.now() });
}
}
async #addPlayerToDeadlineListInIndexDb(player = { pid: "", deadline: 0, name: "" }) {
if (player?.pid) {
this.#deadlines[player.pid] = player;
await this.#db.deadline.put({
sport: this.#sport,
ts: Date.now(),
pid: player.pid,
deadline: player.deadline,
name: player.name
});
}
}
async #removePlayerFromDeadlineList(pid) {
if (pid) {
delete this.#deadlines[pid];
await this.#db.deadline.delete([this.#sport, pid])
.catch((err) => console.warn(err));
}
}
async #fetchDeadlinePlayersFromIndexedDb() {
if (!this.#isTransferDeadlineAlertEnabled()) {
return [];
}
return await this.#db.deadline.toArray()
.then((players) => {
return players?.map(({ pid, deadline, name }) => ({ pid, deadline, name }));
}
).catch((err) => {
console.warn(err);
return [];
});
}
async #deletePlayersFromHideListInIndexDb(days = 0) {
const outdate = Date.now() - days * 24 * 60 * 60 * 1000;
return await this.#db.hide.where("ts").below(outdate).delete().then((deleteCount) => {
return deleteCount;
}).catch((error) => {
console.warn(error);
return 0;
});
}
async #countPlayersOfHideListInIndexDb() {
return await this.#db.hide.where("ts").above(0).count();
}
async #fetchPlayerCommentFromIndexedDb(pid) {
const player = await this.#db.comment.get({ pid, sport: this.#sport });
return player?.comment ?? "";
}
async #setPlayerCommentInIndexedDb(pid, comment) {
if (pid) {
await this.#db.comment.put({ pid, sport: this.#sport, comment: comment ?? "" });
}
}
async #fetchScoutReportFromIndexedDb(pid) {
return await this.#db.scout.get({ pid, sport: this.#sport });
}
async #setScoutReportInIndexedDb(report) {
if (report) {
report.sport = this.#sport;
report.ts = Date.now();
await this.#db.scout.put(report);
}
}
async #setHitInIndexedDb(filterId = "", playerId = "") {
if (filterId && playerId) {
await this.#db.hit.put({ fid: filterId, pid: playerId });
}
}
async #removeHitFromIndexedDb(filterId = "", playerId = "") {
if (filterId && playerId) {
await this.#db.hit.delete([filterId, playerId]);
}
}
async #removeAllHitsOfFilterFromIndexedDb(filterId = "") {
if (filterId) {
await this.#db.hit.where("fid").equals(filterId).delete();
}
}
async #getHitsFromIndexedDb(filterId = "") {
if (filterId) {
return this.#db.hit.filter((hit) => hit.fid === filterId).toArray();
}
return [];
}
async #setFilterHitsInIndexedDb(filterId = "", totalHits = 0, scoutHits = -1) {
if (filterId) {
const item = {
fid: filterId,
totalHits,
scoutHits,
lastCheck: Date.now(),
};
await this.#db.filter.put(item);
}
}
async getFilterHitsFromIndexedDb(filterId = "") {
if (filterId) {
const filter = await this.#db.filter.get(filterId);
if (filter) {
return { totalHits: filter.totalHits, scoutHits: filter.scoutHits, lastCheck: filter.lastCheck };
}
}
return { totalHits: -1, scoutHits: -1, lastCheck: Date.now() };
}
async #removeFilterFromIndexedDb(filterId = "") {
await this.#db.filter.delete(filterId);
await this.#removeAllHitsOfFilterFromIndexedDb(filterId);
}
async #removeAllFilterFromIndexedDb() {
await this.#db.filter.clear();
await this.#db.hit.clear();
}
#extractSkillNamesFromPlayerInfo(player) {
const skills = player?.querySelectorAll("table.player_skills > tbody > tr > td > span.clippable");
return [...skills].map((el) => el.innerText);
}
#extractStarsFromScoutReport(section) {
return section.querySelectorAll(".stars i.lit")?.length;
}
#extractSkillsFromScoutReport(section, skillList) {
const skills = section.querySelectorAll("div.flex-grow-1 ul li.blurred span");
return [...skills].map((el) => skillList.indexOf(el.innerText));
}
async #fetchPlayerProfileFromIndexedDb(pid) {
return await this.#db.player.get({ pid, sport: this.#sport });
}
async #setPlayerProfileInIndexedDb(profile) {
if (profile) {
profile.sport = this.#sport;
profile.ts = Date.now();
await this.#db.player.put(profile);
}
}
async #extractPlayerProfile(playerId) {
const doc = await fetchPlayerProfileDocument(playerId);
if (doc) {
const days = squadExtractResidencyDays(doc);
const skills = doc.querySelectorAll("#thePlayers_0 table.player_skills tbody tr");
let i = 0;
const maxed = [];
for (const skill of skills) {
if (skill.querySelector(".maxed")) {
maxed.push(i);
}
i++;
}
return {
pid: playerId,
maxed,
days,
};
}
return null;
}
async #fetchOrExtractPlayerProfile(playerId) {
let profile = await this.#fetchPlayerProfileFromIndexedDb(playerId);
if (!profile) {
profile = await this.#extractPlayerProfile(playerId);
this.#setPlayerProfileInIndexedDb(profile);
}
return profile;
}
async #extractPlayerScoutReport(pid, skills) {
const url = `https://www.managerzone.com/ajax.php?p=players&sub=scout_report&pid=${pid}&sport=${this.#sport}`;
const resp = await fetch(url).catch((error) => {
console.warn(error);
});
if (resp) {
const parser = new DOMParser();
const doc = parser.parseFromString(await resp.text(), "text/html");
const report = doc.querySelectorAll(".paper-content.clearfix dl dd");
if (report.length == 3) {
const high = this.#extractStarsFromScoutReport(report[0]);
const highSkills = this.#extractSkillsFromScoutReport(report[0], skills);
const low = this.#extractStarsFromScoutReport(report[1]);
const lowSkills = this.#extractSkillsFromScoutReport(report[1], skills);
const trainingSpeed = this.#extractStarsFromScoutReport(report[2]);
return {
pid,
H: high,
HS: highSkills,
L: low,
LS: lowSkills,
S: trainingSpeed,
};
}
}
return null;
}
#colorizeMaxedSkills(player, maxed = []) {
if (maxed) {
const playerSkills = player.querySelectorAll("table.player_skills tr td.skillval span");
for (const skill of maxed) {
playerSkills[skill].classList.add("maxed");
}
}
}
#colorizeSkills(player, report) {
if (report) {
const playerSkills = player.querySelectorAll("table.player_skills tr td:nth-child(1)");
playerSkills[report.HS[0]]?.classList.add("mazyar-scout-h", `mazyar-scout-${report.H}`);
playerSkills[report.HS[1]]?.classList.add("mazyar-scout-h", `mazyar-scout-${report.H}`);
playerSkills[report.LS[0]]?.classList.add(`mazyar-scout-${report.L}`);
playerSkills[report.LS[1]]?.classList.add(`mazyar-scout-${report.L}`);
}
}
async #fetchOrExtractPlayerScoutReport(player) {
const playerId = player.querySelector("h2 a")?.href?.match(/pid=(\d+)/)?.[1];
let report = await this.#fetchScoutReportFromIndexedDb(playerId);
if (!report) {
const skills = this.#extractSkillNamesFromPlayerInfo(player);
report = await this.#extractPlayerScoutReport(playerId, skills);
if (report) {
this.#setScoutReportInIndexedDb(report);
}
}
return report;
}
async #getPlayerScoutReportForSearchResult(players) {
const jobs = [];
for (const player of [...players.children]) {
if (player.classList.contains("playerContainer") && player.querySelector("span.scout_report > a")) {
jobs.push(this.#fetchOrExtractPlayerScoutReport(player));
}
}
const reports = await Promise.all(jobs);
return reports.filter((report) => report != null);
}
#isQualifiedForTransferScoutFilter(report, lows = [], highs = []) {
return highs.includes(report.H) && lows.includes(report.L);
}
#areTransferLowOptionsSelected() {
return this.#transferOptions.L2 || this.#transferOptions.L1;
}
#areTransferHighOptionsSelected() {
return this.#transferOptions.H4 || this.#transferOptions.H3;
}
#areTransferScoutOptionsSelected() {
return this.#areTransferLowOptionsSelected() || this.#areTransferHighOptionsSelected();
}
#getAcceptableHighsAndLows() {
let lows = [];
let highs = [];
if (this.#areTransferLowOptionsSelected()) {
if (this.#transferOptions.L2) {
lows.push(2);
}
if (this.#transferOptions.L1) {
lows.push(1);
}
} else {
lows = [1, 2, 3];
}
if (this.#areTransferHighOptionsSelected()) {
if (this.#transferOptions.H4) {
highs.push(4);
}
if (this.#transferOptions.H3) {
highs.push(3);
}
} else {
highs = [1, 2, 3, 4];
}
return { lows, highs };
}
#applyTransferFilters(report, lows, highs) {
if (!this.#isQualifiedForTransferScoutFilter(report, lows, highs)) {
const player = document.getElementById(`player_id_${report.pid}`);
if (player) {
const playerContainer = player.parentNode.parentNode.parentNode;
if (this.#transferOptionsMustHide()) {
playerContainer.classList.add("mazyar-hide");
} else {
playerContainer.classList.add("mazyar-dim-60");
}
}
}
}
#clearTransferFilters() {
document.querySelectorAll(".mazyar-hide")?.forEach((el) => {
el.classList.remove("mazyar-hide");
});
document.querySelectorAll(".mazyar-dim-50")?.forEach((el) => el.classList.remove("mazyar-dim-50"));
document.querySelectorAll(".mazyar-dim-60")?.forEach((el) => el.classList.remove("mazyar-dim-60"));
}
#isMaxedOrDaysEnabledForTransfer() {
return this.#isDaysAtThisClubEnabledForTransferMarket() || this.#isTransferMaxedSkillsEnabled()
}
async #updateMaxedAndDaysInTransfer(player) {
const playerId = getPlayerIdFromContainer(player);
await this.#fetchOrExtractPlayerProfile(playerId).then((profile) => {
if (this.#isDaysAtThisClubEnabledForTransferMarket()) {
this.#squadAddDaysAtThisClubForSinglePlayer(player, profile);
}
if (this.#mustMarkMaxedSkills()) {
this.#colorizeMaxedSkills(player, profile?.maxed);
}
});
};
async #hidePlayerAccordingToHideList(player) {
const playerId = getPlayerIdFromContainer(player);
if (await this.#isPlayerInHideListInIndexDb(playerId)) {
player.style.display = 'none';
}
}
#addHideButtonToPlayerInTransferMarket(player) {
if (player.hideButtonInjected) {
return;
}
player.hideButtonInjected = true;
const playerId = getPlayerIdFromContainer(player);
const hideIcon = createDeleteIcon("Hide player from search result.");
hideIcon.classList.add("floatRight");
hideIcon.style.marginTop = "0.2rem";
player.querySelector("h2.clearfix div")?.appendChild(hideIcon);
hideIcon.addEventListener("click", () => {
this.#addPlayerToHideListInIndexDb(playerId);
player.style.display = 'none';
});
}
async #processTransferSearchResults(results) {
const { lows, highs } = this.#getAcceptableHighsAndLows();
const players = [...results.children].filter((player) => player.classList.contains("playerContainer"));
const deadlines = await this.#fetchDeadlinePlayersFromIndexedDb();
const jobs = [];
for (const player of players) {
this.#addHideButtonToPlayerInTransferMarket(player);
if (this.#isTransferDeadlineAlertEnabled()) {
this.#addDeadlineButtonToPlayerInTransferMarket(player, deadlines);
}
jobs.push(this.#hidePlayerAccordingToHideList(player));
if (this.#isMaxedOrDaysEnabledForTransfer()) {
jobs.push(this.#updateMaxedAndDaysInTransfer(player));
}
if (this.#areTransferScoutOptionsSelected()) {
if (player.querySelector("span.scout_report > a")) {
jobs.push(this.#fetchOrExtractPlayerScoutReport(player).then((report) => {
this.#colorizeSkills(player, report);
this.#applyTransferFilters(report, lows, highs);
}));
} else {
const className = this.#transferOptionsMustHide() ? "mazyar-hide" : "mazyar-dim-50";
player.classList.add(className);
}
}
}
await Promise.all(jobs);
}
#mustUpdateDisplayForTransferSearchResults() {
return this.#areTransferScoutOptionsSelected()
|| this.#isTransferMaxedSkillsEnabled()
|| this.#isDaysAtThisClubEnabledForTransferMarket();
}
async updateDisplayForTransferSearchResults(clear = false) {
if (clear) {
this.#clearTransferFilters();
}
if (this.#mustUpdateDisplayForTransferSearchResults()) {
const results = document.getElementById("players_container");
if (results) {
await this.#processTransferSearchResults(results);
}
}
}
#injectHideButtonToTransferMarket() {
const target = document.querySelector("#searchform div.buttons-wrapper.clearfix");
if (target) {
const hideButton = createMzStyledButton("MZY Hide", "red", "floatLeft");
hideButton.style.margin = "0";
hideButton.onclick = () => {
this.#displayTransferHideMenu();
};
target.appendChild(hideButton);
}
}
async executeTransferTasks() {
this.#injectHideButtonToTransferMarket();
this.updateDisplayForTransferSearchResults();
const callback = (mutationsList) => {
if (mutationsList.find(mutation => mutation.type == "childList")) {
this.updateDisplayForTransferSearchResults();
}
};
const target = document.getElementById("players_container");
if (target) {
const observer = new MutationObserver(callback);
const config = { childList: true };
observer.observe(target, config);
}
}
#getSelectedScoutsOptionText() {
const texts = [];
const options = this.getTransferOptions();
if (options.H4) {
texts.push('H4');
}
if (options.H3) {
texts.push('H3');
}
if (options.L2) {
texts.push('L2');
}
if (options.L1) {
texts.push('L1');
}
return texts.join(", ");
}
#getSelectedScoutsOptionTextForFilter(scout) {
const texts = [];
if (scout.high.includes(4)) {
texts.push('H4');
}
if (scout.high.includes(3)) {
texts.push('H3');
}
if (scout.low.includes(2)) {
texts.push('L2');
}
if (scout.low.includes(1)) {
texts.push('L1');
}
return texts.join(", ");
}
// -------------------------------- Player Options -------------------------------------
async #addPlayerCommentIcon(player) {
const playerId = getPlayerIdFromContainer(player);
const commentIcon = createCommentIcon("MZY Comment");
if (await this.#fetchPlayerCommentFromIndexedDb(playerId)) {
commentIcon.classList.add("mazyar-player-comment-icon-active");
} else {
commentIcon.classList.add("mazyar-player-comment-icon-inactive");
}
player.querySelector(".p_links")?.appendChild(commentIcon);
commentIcon.addEventListener("click", async (event) => {
this.#displayPlayerComment(event?.target, playerId);
})
}
async addPlayerComment() {
if (this.#mustAddPlayerComment()) {
const jobs = [];
const players = document.querySelectorAll(".playerContainer");
for (const player of players) {
jobs.push(this.#addPlayerCommentIcon(player));
}
Promise.all(jobs);
}
}
async #squadAddDaysAtThisClubForSinglePlayer(player, profile) {
if (player.daysInjected) {
return;
}
player.daysInjected = true;
const daysDiv = document.createElement("div");
if (profile?.days >= 0) {
const text = profile?.days === 0 ? 'N/A' : `≤ ${profile?.days}`;
daysDiv.innerHTML = `Days at this club: ${text}`;
daysDiv.classList.add("mazyar-days-at-this-club");
} else if (this.isDaysAtThisClubEnabledForOneClubPlayers()) {
const text = 'Entire Career';
daysDiv.innerHTML = `Days at this club: ${text}`;
daysDiv.classList.add("mazyar-days-at-this-club-entire");
}
player.querySelector("div.mainContent")?.appendChild(daysDiv);
}
async squadAddDaysAtThisClubToAllPlayers(container) {
if (this.isDaysAtThisClubEnabledForPlayerProfiles()) {
const jobs = [];
const players = container.querySelectorAll("div.playerContainer");
for (const player of players) {
jobs.push((async (player) => {
const playerId = getPlayerIdFromContainer(player);
await this.#fetchOrExtractPlayerProfile(playerId).then((profile) => {
this.#squadAddDaysAtThisClubForSinglePlayer(player, profile);
});
})(player));
}
Promise.all(jobs);
}
}
async trainingAddDaysAtThisClubToPlayerProfile(container) {
if (this.#isDaysAtThisClubEnabledForTraining()) {
const player = container.querySelector("div.playerContainer");
const playerId = getPlayerIdFromContainer(player);
await this.#fetchOrExtractPlayerProfile(playerId).then((profile) => {
this.#squadAddDaysAtThisClubForSinglePlayer(player, profile);
});
}
}
// -------------------------------- Transfer Options -------------------------------------
#fetchTransferOptions() {
this.#transferOptions.hide = GM_getValue("transfer_options_hide", true);
this.#transferOptions.H4 = GM_getValue("transfer_options_H4", false);
this.#transferOptions.H3 = GM_getValue("transfer_options_H3", false);
this.#transferOptions.L2 = GM_getValue("transfer_options_L2", false);
this.#transferOptions.L1 = GM_getValue("transfer_options_L1", false);
}
#saveTransferOptions() {
GM_setValue("transfer_options_hide", this.#transferOptions.hide);
GM_setValue("transfer_options_H4", this.#transferOptions.H4);
GM_setValue("transfer_options_H3", this.#transferOptions.H3);
GM_setValue("transfer_options_L2", this.#transferOptions.L2);
GM_setValue("transfer_options_L1", this.#transferOptions.L1);
}
#transferOptionsMustHide() {
return this.#transferOptions.hide;
}
getTransferOptions() {
return this.#transferOptions;
}
updateTransferOptions(key, value) {
this.#transferOptions[key] = value;
this.#saveTransferOptions();
}
#resetTransferOptions() {
this.#transferOptions.hide = false;
this.#transferOptions.H4 = false;
this.#transferOptions.H3 = false;
this.#transferOptions.L2 = false;
this.#transferOptions.L1 = false;
this.#saveTransferOptions();
}
// -------------------------------- Filters -------------------------------------
#fetchFilters() {
this.#filters = GM_getValue("transfer_filters", { soccer: [], hockey: [] });
}
#saveFilters() {
GM_setValue("transfer_filters", {
soccer: this.#filters.soccer.map(({ id, name, params, scout, interval }) => ({
id,
name,
params,
scout,
interval,
})),
hockey: this.#filters.hockey.map(({ id, name, params, scout, interval }) => ({
id,
name,
params,
scout,
interval,
})),
});
}
#getCurrentFilters() {
return this.#filters[this.#sport];
}
deleteFilter(id = "") {
const filterIndex = this.#getCurrentFilters().findIndex((f) => f.id === id);
if (filterIndex > -1) {
this.#removeFilterFromIndexedDb(id);
this.#getCurrentFilters().splice(filterIndex, 1);
this.#checkAllFilters(true);
this.#saveFilters();
}
}
deleteAllFilters() {
this.#removeAllFilterFromIndexedDb();
this.#getCurrentFilters().length = 0;
this.#checkAllFilters(true);
this.#saveFilters();
}
getFilterParams(name = "") {
return this.#getCurrentFilters().find((f) => f.name === name)?.params;
}
async updateFilterDetails(name, params, scout, interval) {
const filters = this.#getCurrentFilters();
let filter = filters.find((f) => f.name === name);
if (filter) {
filter.params = params;
filter.scout = scout;
filter.interval = interval;
} else {
// create a new filter if name does not exist
filter = {
name,
params,
scout,
interval,
id: generateUuidV4(),
};
filters.push(filter);
}
this.#saveFilters();
const { totalHits, scoutHits } = await this.#getFilterTotalHits(filter);
if (totalHits >= 0) {
await this.#setFilterHitsInIndexedDb(filter.id, totalHits, scoutHits);
this.#checkAllFilters(true);
}
}
#itsTimeToCheckFilter(filter, lastCheck) {
const passed = Date.now() - lastCheck;
if (filter.interval === TRANSFER_INTERVALS.never.value) {
return false;
} else if (filter.interval === TRANSFER_INTERVALS.onceDay.value) {
return passed > 24 * 60 * 60 * 1000;
} else if (filter.interval === TRANSFER_INTERVALS.onceHour.value) {
return passed > 1 * 60 * 60 * 1000;
} else if (filter.interval === TRANSFER_INTERVALS.onceMinute.value) {
return passed > 1 * 60 * 1000;
}
// 'always' or any invalid value means always
return true;
}
#getAcceptableHighsAndLowsForFilter(scout) {
const lows = scout.low.length > 0 ? scout.low : [1, 2, 3];
const highs = scout.high.length > 0 ? scout.high : [1, 2, 3, 4];
return { lows, highs };
}
async #getFilterHitsByOffset(filter, offset = 0) {
let totalHits = -1;
let scoutHits = -1;
const url = `https://www.managerzone.com/ajax.php?p=transfer&sub=transfer-search&sport=${this.#sport}${filter.params}&o=${offset}`;
const response = await fetch(url).catch((error) => {
console.warn(error);
});
if (response) {
const data = await response.json();
totalHits = Number(data?.totalHits);
const searchResults = document.createElement("div");
searchResults.innerHTML = data.players;
if (filter.scout) {
const playersReport = await this.#getPlayerScoutReportForSearchResult(searchResults);
const { lows, highs } = this.#getAcceptableHighsAndLowsForFilter(filter.scout);
const scouted = playersReport.filter((report) => this.#isQualifiedForTransferScoutFilter(report, lows, highs));
scoutHits = scouted.length;
// save scout hits in indexed db
const jobs = [];
for (const player of scouted) {
jobs.push(this.#setHitInIndexedDb(filter.id, player.pid));
}
await Promise.all(jobs);
}
}
return { totalHits, scoutHits };
}
async #getFilterTotalHits(filter) {
// get first page of the results
await this.#removeAllHitsOfFilterFromIndexedDb(filter.id);
let { totalHits, scoutHits } = await this.#getFilterHitsByOffset(filter);
if (totalHits >= 0) {
if (filter.scout) {
const pages = Math.min(Math.ceil(totalHits / 20), 26);
const jobs = [];
// when filter uses scout report, get next pages too
for (let i = 1; i < pages; i++) {
jobs.push(this.#getFilterHitsByOffset(filter, 20 * i));
}
const results = await Promise.all(jobs);
for (const result of results) {
scoutHits += result.scoutHits;
}
}
}
return { totalHits, scoutHits };
}
async refreshFilterHits(id = "") {
const filter = this.#getCurrentFilters().find((filter) => filter.id === id);
if (filter) {
const { totalHits, scoutHits } = await this.#getFilterTotalHits(filter);
if (totalHits >= 0) {
this.#setFilterHitsInIndexedDb(filter.id, totalHits, scoutHits);
this.#checkAllFilters(false);
return { totalHits, scoutHits };
}
}
return { totalHits: -1, scoutHits: -1 };
}
// -------------------------------- Display -------------------------------------
#createModal() {
this.#modal = document.createElement("div");
this.#modal.id = "mazyar-modal";
this.#modal.classList.add("mazyar-flex-container");
this.#content = document.createElement("div");
this.#content.id = "mazyar-modal-content";
this.#content.classList.add("mazyar-flex-container");
this.#modal.appendChild(this.#content);
this.hideModal();
document.body?.appendChild(this.#modal);
}
#addToolbar() {
const that = this;
const { toolbar, menu, transfer } = createToolbar();
menu.onclick = () => {
that.displaySettingsMenu();
};
transfer.onclick = () => {
that.displayTransferFilters();
};
document.body?.appendChild(toolbar);
}
#displayModal() {
this.#modal.style.display = "flex";
}
#clearModalContent() {
this.#content.innerText = "";
}
#replaceModalContent(elements = []) {
this.#clearModalContent();
for (const element of elements) {
if (element) {
this.#content.appendChild(element);
}
}
this.#displayModal();
}
#displayLoading(title = "MZY") {
const header = createMzStyledTitle(title);
const loading = document.createElement("p");
loading.innerText = "Loading...";
loading.style.padding = "1rem";
this.#replaceModalContent([header, loading]);
}
async cleanInstall() {
this.updateSettings({
in_progress_results: false,
top_players_in_tables: false,
transfer: false,
transfer_maxed: false,
mz_predictor: false,
player_comment: false,
coach_salary: false,
deadline: false,
days: {
display_in_profiles: false,
display_in_transfer: false,
display_for_training: false,
display_for_one_clubs: false,
}
});
this.deleteAllFilters();
await this.#clearIndexedDb();
this.#resetTransferOptions();
}
displayCleanMenu() {
const that = this;
const div = document.createElement("div");
const title = createMzStyledTitle("MZY Settings");
const notice = document.createElement("div");
const buttons = document.createElement("div");
const cancel = createMzStyledButton("Cancel", "red");
const clean = createMzStyledButton("Clean", "blue");
div.classList.add("mazyar-flex-container");
buttons.classList.add("mazyar-flex-container-row");
notice.innerHTML = "All Settings, Filters, Scout Reports and ... will be deleted.
Are you sure?";
notice.style.padding = "1rem";
clean.onclick = async () => {
await that.cleanInstall();
that.hideModal();
that.displaySettingsMenu();
};
cancel.onclick = () => {
that.hideModal();
that.displaySettingsMenu();
};
div.appendChild(title);
div.appendChild(notice);
buttons.appendChild(cancel);
buttons.appendChild(clean);
div.appendChild(buttons);
this.#replaceModalContent([div]);
}
displaySettingsMenu() {
const submenuStyle = { margin: "0.1rem 1.2rem" };
const div = document.createElement("div");
const title = createMzStyledTitle("MZY Settings");
const miscellaneousGroup = createMenuGroup("Miscellaneous:");
const playerComment = createMenuCheckBox("Enable player comment", this.#settings.player_comment, submenuStyle);
const inProgress = createMenuCheckBox("Display in progress results", this.#settings.in_progress_results, submenuStyle);
const tableInjection = createMenuCheckBox("Display teams' top players in tables", this.#settings.top_players_in_tables, submenuStyle);
const mzPredictor = createMenuCheckBox("Help with World Cup Predictor", this.#settings.mz_predictor, submenuStyle);
mzPredictor.style.display = 'none';
miscellaneousGroup.appendChild(playerComment);
miscellaneousGroup.appendChild(inProgress);
miscellaneousGroup.appendChild(tableInjection);
miscellaneousGroup.appendChild(mzPredictor);
const coachesGroup = createMenuGroup("Coaches:");
const coachSalaries = createMenuCheckBox("Display salaries in search results", this.#settings.coach_salary, submenuStyle);
coachesGroup.appendChild(coachSalaries);
const transferGroup = createMenuGroup("Transfer Market:");
const transferFilters = createMenuCheckBox("Enable transfer filters", this.#settings.transfer, submenuStyle);
const transferMaxed = createMenuCheckBox("Mark maxed skills", this.#settings.transfer_maxed, submenuStyle);
const transferDeadline = createMenuCheckBox("Enable deadline alert", this.#settings.deadline, submenuStyle);
transferGroup.appendChild(transferFilters);
transferGroup.appendChild(transferFilters);
transferGroup.appendChild(transferDeadline);
const daysGroup = createMenuGroup("Days at this club:");
const daysInProfiles = createMenuCheckBox("Display in player profiles", this.#settings.days.display_in_profiles, submenuStyle);
const daysInTransfer = createMenuCheckBox("Display in transfer market", this.#settings.days.display_in_transfer, submenuStyle);
const daysInTraining = createMenuCheckBox("Display in training report", this.#settings.days.display_in_training, submenuStyle);
const daysForOneClubs = createMenuCheckBox("Display for One-Club players", this.#settings.days.display_for_one_clubs, submenuStyle);
daysGroup.appendChild(daysInProfiles);
daysGroup.appendChild(daysInTransfer);
daysGroup.appendChild(daysInTraining);
daysGroup.appendChild(daysForOneClubs);
const buttons = document.createElement("div");
const clean = createMzStyledButton(` Clean Install`, "blue");
const cancel = createMzStyledButton("Cancel", "red");
const save = createMzStyledButton("Save", "green");
div.classList.add("mazyar-flex-container");
buttons.classList.add("mazyar-flex-container-row");
cancel.onclick = () => {
this.hideModal();
};
save.onclick = () => {
this.updateSettings({
in_progress_results: inProgress.querySelector("input[type=checkbox]").checked,
top_players_in_tables: tableInjection.querySelector("input[type=checkbox]").checked,
transfer: transferFilters.querySelector("input[type=checkbox]").checked,
transfer_maxed: transferMaxed.querySelector("input[type=checkbox]").checked,
deadline: transferDeadline.querySelector("input[type=checkbox]").checked,
mz_predictor: mzPredictor.querySelector("input[type=checkbox]").checked,
player_comment: playerComment.querySelector("input[type=checkbox]").checked,
coach_salary: coachSalaries.querySelector("input[type=checkbox]").checked,
days: {
display_in_profiles: daysInProfiles.querySelector("input[type=checkbox]").checked,
display_in_transfer: daysInTransfer.querySelector("input[type=checkbox]").checked,
display_in_training: daysInTraining.querySelector("input[type=checkbox]").checked,
display_for_one_clubs: daysForOneClubs.querySelector("input[type=checkbox]").checked,
}
});
this.hideModal();
};
clean.style.marginBottom = "0";
clean.onclick = () => {
this.hideModal();
this.displayCleanMenu();
};
div.appendChild(title);
div.appendChild(transferGroup);
div.appendChild(daysGroup);
div.appendChild(coachesGroup);
div.appendChild(miscellaneousGroup);
div.appendChild(clean);
buttons.appendChild(cancel);
buttons.appendChild(save);
div.appendChild(buttons);
this.#replaceModalContent([div]);
}
displayFilterSaveMenu(params) {
const filters = this.#getCurrentFilters();
const that = this; // used in onclick event listener
const scoutText = this.#getSelectedScoutsOptionText();
const title = createMzStyledTitle("MZY Transfer Filter");
const div = document.createElement("div");
const datalist = createSuggestionList(filters.map((f) => f.name));
const filterName = createTextInput("Filter Name", "U21 K-10 ST-10", datalist.id);
const useScout = createMenuCheckBox(`Use scout reports too (${scoutText})`);
const checkInterval = createMenuDropDown("Check Interval", TRANSFER_INTERVALS, TRANSFER_INTERVALS.onceHour.value);
const validation = document.createElement("div");
const buttons = document.createElement("div");
const cancel = createMzStyledButton("Cancel", "red");
const save = createMzStyledButton("Save", "green");
div.classList.add("mazyar-flex-container");
validation.innerText = "Error: Name is empty.";
validation.style.color = "red";
validation.style.display = "none";
validation.style.alignSelf = "flex-start";
validation.style.margin = "auto 0.5rem";
filterName.oninput = () => {
const name = filterName.querySelector("input[type='text']").value;
if (name) {
validation.style.display = "none";
save.classList.remove(getMzButtonColorClass("grey"));
} else {
validation.style.display = "unset";
save.classList.add(getMzButtonColorClass("grey"));
}
};
buttons.classList.add("mazyar-flex-container-row");
cancel.onclick = () => {
that.hideModal();
};
save.onclick = () => {
// save then close
const name = filterName.querySelector("input[type=text]").value;
if (name) {
const high = [];
const low = [];
if (useScout.querySelector("input[type=checkbox]").checked) {
const options = that.getTransferOptions();
if (options.H4) {
high.push(4);
}
if (options.H3) {
high.push(3);
}
if (options.L2) {
low.push(2);
}
if (options.L1) {
low.push(1);
}
}
const scout = high.length === 0 && low.length === 0 ? null : { high, low };
const interval = checkInterval.querySelector("select").value;
that.updateFilterDetails(name, params, scout, interval);
that.hideModal();
} else {
validation.style.display = "unset";
save.classList.add(getMzButtonColorClass("grey"));
}
};
buttons.appendChild(cancel);
buttons.appendChild(save);
div.appendChild(title);
div.appendChild(filterName);
div.appendChild(checkInterval);
if (scoutText) {
div.appendChild(useScout);
}
div.appendChild(datalist);
div.appendChild(validation);
div.appendChild(buttons);
this.#replaceModalContent([div]);
}
#setFilterHitsInToolbar(total = -1) {
// pass negative to create loading animation.
const hits = document.getElementById("mazyar-transfer-filter-hits");
if (hits) {
if (total < 0) {
hits.replaceChildren(createLoadingIcon());
} else {
hits.innerText = total > 100 ? "+100" : total.toString();
hits.style.color = total > 0 ? "cyan" : "white";
}
}
}
async #checkAllFilters(forced = false) {
this.#setFilterHitsInToolbar(-1);
const filters = this.#getCurrentFilters();
let total = 0;
for (const filter of filters) {
let { totalHits: hits, lastCheck } = await this.getFilterHitsFromIndexedDb(filter.id);
const needRefresh = this.#itsTimeToCheckFilter(filter, lastCheck);
if (!filterHitsIsValid(hits) || forced || needRefresh) {
const { totalHits, scoutHits } = await this.#getFilterTotalHits(filter);
if (totalHits >= 0) {
hits = filter.scout ? scoutHits : totalHits;
await this.#setFilterHitsInIndexedDb(filter.id, totalHits, scoutHits);
}
}
total += hits;
}
this.#setFilterHitsInToolbar(total);
return total;
}
async setInitialFiltersHitInToolbar() {
await this.#checkAllFilters(false);
}
async #displayTransferHideMenu() {
const div = document.createElement("div");
div.classList.add("mazyar-flex-container");
const title = createMzStyledTitle("MZY Transfer Hide List");
await this.#countPlayersOfHideListInIndexDb();
const body = document.createElement("div");
const description = document.createElement("div");
const dayClearDiv = document.createElement("div");
const daysInput = createTextInput("Days", "0", "");
const clear = createMzStyledButton("Remove", "red", "floatRight");
const validation = document.createElement("div");
const result = document.createElement("div");
dayClearDiv.appendChild(daysInput);
dayClearDiv.appendChild(clear);
body.appendChild(description);
body.appendChild(dayClearDiv);
body.appendChild(validation);
body.appendChild(result);
body.classList.add("mazyar-flex-container");
body.style.maxWidth = "320px";
dayClearDiv.classList.add("mazyar-flex-container-row");
daysInput.querySelector("input[type='text']").style.width = "2rem";
const close = createMzStyledButton("Close", "green");
description.innerHTML = `There are ${await this.#countPlayersOfHideListInIndexDb()} player(s) hidden from transfer market.
You can remove players from hide list.
Enter the number of days to remove players hidden before this period.
Enter 0 to remove all.
`;
description.style.paddingLeft = "0.7rem";
validation.innerText = "Error: Invalid value for days.";
validation.style.color = "red";
validation.style.display = "none";
validation.style.margin = "auto 0.5rem";
result.style.display = "none";
result.style.margin = "auto 0.5rem";
result.innerHTML = `Removed 0 player(s) from hide list.`
daysInput.addEventListener("input", () => {
result.style.display = "none";
const days = daysInput.querySelector("input[type='text']").value;
if (days.match(/^\d+$/)) {
validation.style.display = "none";
clear.classList.remove(getMzButtonColorClass("grey"));
} else {
validation.style.display = "unset";
clear.classList.add(getMzButtonColorClass("grey"));
}
});
clear.addEventListener("click", async () => {
const days = daysInput.querySelector("input[type='text']").value
if (days.match(/\d+/)) {
const deleteCount = await this.#deletePlayersFromHideListInIndexDb(days);
result.querySelector("strong").innerText = deleteCount;
result.style.display = "unset";
description.querySelector("strong").innerText = await this.#countPlayersOfHideListInIndexDb();
} else {
validation.style.display = "unset";
clear.classList.add(getMzButtonColorClass("grey"));
}
});
close.addEventListener("click", () => {
this.hideModal();
});
div.appendChild(title);
div.appendChild(body);
div.appendChild(close);
this.#replaceModalContent([div]);
}
async displayTransferFilters() {
const that = this; // used in onclick event listener
const div = document.createElement("div");
div.classList.add("mazyar-flex-container");
const title = createMzStyledTitle("MZY Transfer Filters");
const filtersView = document.createElement("div");
filtersView.classList.add("mazyar-flex-container");
const noFilterView = document.createElement("span");
noFilterView.innerText = "There is no filter to display";
noFilterView.style.display = "none";
noFilterView.style.margin = "1rem";
const filters = this.#getCurrentFilters();
if (filters.length > 0) {
const deleteAll = createDeleteButtonWithTrashIcon("Delete all filters");
deleteAll.onclick = () => {
that.deleteAllFilters();
filtersView.style.display = "none";
noFilterView.style.display = "unset";
};
const table = filtersViewCreateTable(filters);
table.addEventListener("destroy", () => {
// remove 'delete all' button if no filter is left
filtersView.style.display = "none";
noFilterView.style.display = "unset";
});
filtersView.appendChild(deleteAll);
filtersView.appendChild(table);
} else {
noFilterView.style.display = "unset";
}
const close = createMzStyledButton("Close", "green");
close.onclick = () => {
that.hideModal();
};
div.appendChild(title);
div.appendChild(filtersView);
div.appendChild(noFilterView);
div.appendChild(close);
this.#replaceModalContent([div]);
}
async displaySquadSummary(url) {
this.#displayLoading("MZY Squad Summary");
await fetch(url)
.then((resp) => resp.text())
.then((content) => {
const parser = new DOMParser();
const doc = parser.parseFromString(content, "text/html");
const sport = getSportType(doc);
const currency = getClubCurrency(doc);
const players = getClubPlayers(doc, currency);
const summary = squadGetPlayersInfo(players, sport);
const topPlayers = squadCreateTopPlayersTable(summary, currency, sport);
topPlayers.style.margin = "2px 5px";
topPlayers.style.padding = "0";
const header = createMzStyledTitle("MZY Squad Summary");
const button = createMzStyledButton("Close", "red");
button.onclick = () => {
mazyar.hideModal();
};
this.#replaceModalContent([header, topPlayers, button]);
})
.catch((error) => {
console.warn(error);
});
}
async #displayTransferDeadlines() {
const div = document.createElement("div");
div.classList.add("mazyar-flex-container");
const title = createMzStyledTitle("MZY Transfer Deadlines");
const middle = document.createElement("div");
middle.style.flex = "1";
middle.style.overflowY = "auto"; // make it scrollable
middle.style.margin = "5px 2px";
const bids = document.createElement("table");
bids.style.margin = "5px 2px";
middle.appendChild(bids);
const thead = document.createElement("thead");
thead.innerHTML = ` | Player | Deadline |
`;
bids.appendChild(thead);
const sortedBids = Object.values(this.#deadlines)?.sort((a, b) => a.deadline - b.deadline);
const tbody = document.createElement("tbody");
bids.appendChild(tbody);
for (const bid of sortedBids) {
const row = document.createElement("tr");
const deleteButton = document.createElement("td");
deleteButton.addEventListener("click", async () => {
await this.#removePlayerFromDeadlineList(bid.pid);
row.remove();
if (tbody.childElementCount === 0) {
this.#deadlineUpdateIconStyle();
this.hideModal();
}
});
const trashIcon = createTrashIcon("Remove from deadline list");
deleteButton.appendChild(trashIcon);
const name = document.createElement("td");
name.innerHTML = `${bid.name}`;
name.style.paddingRight = "15px";
const deadline = document.createElement("td");
deadline.innerHTML = `${bid.deadline} minutes`;
deadline.style.paddingRight = "15px";
row.appendChild(deleteButton);
row.appendChild(name);
row.appendChild(deadline);
tbody.appendChild(row);
}
const close = createMzStyledButton("Close", "green");
close.addEventListener("click", () => {
this.hideModal();
});
div.appendChild(title);
div.appendChild(middle);
div.appendChild(close);
this.#replaceModalContent([div]);
}
#addDeadlineIndicator() {
const deadline = createDeadlineIndicator();
deadline.id = "mazyar-deadline";
deadline.style.display = "none";
deadline.style.border = "1px solid black";
deadline.style.borderRadius = "50%";
deadline.style.padding = "0.5rem";
document.body?.appendChild(deadline);
deadline.addEventListener("click", () => {
this.#displayTransferDeadlines();
});
return deadline;
}
async #deadlineFetchAndProcessMonitor() {
const response = await fetchTransferMonitorData();
if (response) {
const yourBids = document.createElement("div");
yourBids.innerHTML = response.content;
const bids = yourBids.querySelectorAll(`table[cellpadding="0"][border="0"] table a:not([class="player_icon"])`);
const deadlines = yourBids.querySelectorAll(`table[cellpadding="0"][border="0"] table img[src="img/icon_deadline.gif"]`);
const players = [...Array(bids.length).keys()].map((n) => ({
name: bids[n].innerText,
pid: extractPlayerIDFromTransferMonitor(bids[n].href),
deadline: 1 + Math.ceil((parseMzDateTime(deadlines[n]?.parentNode?.parentNode?.innerText?.trim()) - new Date()) / 60_000)
}));
for (const player of players) {
if (player.deadline > 0) {
await this.#addPlayerToDeadlineListInIndexDb(player);
} else {
await this.#removePlayerFromDeadlineList(player.pid);
}
}
}
}
async #updatePlayerDeadlineFromMarket(pid) {
const url = `https://www.managerzone.com/ajax.php?p=transfer&sub=transfer-search&sport=${this.#sport}&u=${pid}`;
const result = await fetch(url)
.then((resp) => resp.json())
.catch((err) => {
console.warn(err);
return null;
});
if (result) {
if (result.totalHits > 0) {
const parser = new DOMParser();
const playerDiv = parser.parseFromString(result?.players, "text/html").body.firstChild;
const deadline = playerDiv.querySelector(".transfer-control-area div.box_dark:nth-child(1) table:nth-child(1) tr:nth-child(3) strong")?.innerText;
const player = {
name: playerDiv.querySelector(".player_name")?.innerText,
pid,
deadline: 1 + Math.ceil((parseMzDateTime(deadline.trim()) - new Date()) / 60_000),
};
await this.#addPlayerToDeadlineListInIndexDb(player);
} else {
await this.#removePlayerFromDeadlineList(pid);
}
}
}
async #deadlineProcessPlayersInIndexedDb() {
const players = await this.#fetchDeadlinePlayersFromIndexedDb();
const jobs = [];
for (const player of players) {
if (this.#deadlineLockAcquired) {
jobs.push(this.#updatePlayerDeadlineFromMarket(player.pid));
} else if (player.deadline > 0) {
this.#deadlines[player.pid] = player;
} else {
delete this.#deadlines[player.pid];
}
}
await Promise.all(jobs);
}
#deadlineIsLockerTabDead() {
return Date.now() - Number(GM_getValue("deadline_locker_last_call", 0)) > (2 * DEADLINE_INTERVAL_SECONDS * 1000);
}
#deadlineUpdateLockStatus() {
if (this.#deadlineLockAcquired) {
// we have the lock
GM_setValue("deadline_locker_last_call", Date.now());
} else {
// check whether if lock is available to acquire
const isLocked = GM_getValue("deadline_is_locked", false);
if (isLocked) {
// other instance of mazyar is already required the lock
this.#deadlineLockAcquired = false;
if (this.#deadlineIsLockerTabDead()) {
// it seems locker tab is not alive. make it available for other tabs
console.warn("locker tab is dead. release the lock.");
GM_setValue("deadline_is_locked", false);
}
} else {
// we can acquire the lock
GM_setValue("deadline_is_locked", true);
GM_setValue("deadline_locker_last_call", Date.now());
this.#deadlineLockAcquired = true;
window.onbeforeunload = () => {
// release lock
GM_setValue("deadline_is_locked", false);
this.#deadlineLockAcquired = false;
}
}
}
}
#deadlineUpdateIconStyle() {
const deadlineIcon = document.getElementById("mazyar-deadline");
const strobe = Object.values(this.#deadlines).filter((player) => player.deadline <= DEADLINE_MINUTES).length > 0;
if (strobe && deadlineIcon) {
deadlineIcon.style.display = 'unset';
deadlineIcon.classList.add("mazyar-deadline-throb-lightgreen");
} else {
deadlineIcon.style.display = 'none';
deadlineIcon.classList.remove("mazyar-deadline-throb-lightgreen");
}
}
async #updateDeadlines() {
this.#deadlineUpdateLockStatus();
// remove old data
this.#deadlines = {};
await this.#deadlineProcessPlayersInIndexedDb();
if (this.#deadlineLockAcquired) {
await this.#deadlineFetchAndProcessMonitor();
}
this.#deadlineUpdateIconStyle();
}
async injectTransferDeadlineAlert() {
if (this.#isTransferDeadlineAlertEnabled()) {
this.#addDeadlineIndicator();
await this.#updateDeadlines();
setInterval(() => {
this.#updateDeadlines();
}, 1000 * DEADLINE_INTERVAL_SECONDS);
}
}
#addDeadlineButtonToPlayerInTransferMarket(player, deadlines) {
if (player.deadlineIconInjected) {
return;
}
player.deadlineIconInjected = true;
const playerId = getPlayerIdFromContainer(player);
let alreadyAdded = deadlines.find((player) => player.pid == playerId);
const addButton = createAddToDeadlineIcon("Deadline Monitor Add/Remove", alreadyAdded ? "red" : "green")
player.querySelector("h2.clearfix div")?.appendChild(addButton);
addButton.addEventListener("click", async () => {
if (alreadyAdded) {
alreadyAdded = false;
addButton.style.color = "green";
await this.#removePlayerFromDeadlineList(playerId);
} else {
alreadyAdded = true;
addButton.style.color = "red";
await this.#updatePlayerDeadlineFromMarket(playerId);
}
this.#deadlineUpdateIconStyle();
});
}
#createFilterInfo(data = { name: "", scout: { high: [], low: [] }, count: "" }) {
const info = document.createElement("div");
const nameSpan = document.createElement("span");
const scoutSpan = document.createElement("span");
const countSpan = document.createElement("span");
info.style.margin = "3px";
info.style.padding = "3px";
info.style.alignSelf = "flex-start";
nameSpan.innerHTML = `Filter Name: ${data.name}
`;
scoutSpan.innerHTML = `Selected: ${this.#getSelectedScoutsOptionTextForFilter(data.scout)}
`;
countSpan.innerHTML = `Hit Count: ${data.count}`;
info.appendChild(nameSpan);
info.appendChild(scoutSpan);
info.appendChild(countSpan);
return info;
}
async #appendFilterResultToModal(middle, searchResults) {
for (const result of searchResults) {
const parser = new DOMParser();
const player = parser.parseFromString(result.content.players, "text/html").body.firstChild;
if (this.#isMaxedOrDaysEnabledForTransfer()) {
this.#updateMaxedAndDaysInTransfer(player);
}
player.id = "";
this.#fetchOrExtractPlayerScoutReport(player).then(report => {
this.#colorizeSkills(player, report);
});
const a = player.querySelector("h2>div>a.subheader");
if (a) {
a.href = `https://www.managerzone.com/?p=transfer&u=${result.playerId}`;
a.target = "_blank";
const tools = player.querySelector("td span.player_icon_placeholder.bid_button")?.parentNode;
if (tools) {
tools.style.display = "none";
}
player.querySelector("div.floatRight.transfer-control-area")?.classList.add("mazyar-transfer-control-area");
middle.appendChild(player);
} else {
this.#removeHitFromIndexedDb(filterId, result.playerId);
}
}
}
async displayFilterResults(filterId, filterInfo) {
const div = document.createElement("div");
const header = createMzStyledTitle("MZY Filter Results");
const info = this.#createFilterInfo(filterInfo);
const middle = document.createElement("div");
const close = createMzStyledButton("Close", "red");
div.classList.add("mazyar-flex-container");
middle.style.flex = "1";
middle.style.overflowY = "auto"; // make it scrollable
middle.style.margin = "5px 2px";
close.style.marginBottom = "1px";
close.onclick = () => {
this.hideModal();
this.displayTransferFilters();
};
const players = await this.#getHitsFromIndexedDb(filterId);
this.#displayLoading("MZY Filter Results");
const jobs = [];
for (const player of players) {
const url = `https://www.managerzone.com/ajax.php?p=transfer&sub=transfer-search&sport=${this.#sport}&u=${player.pid}`;
jobs.push(
fetch(url)
.then((resp) => resp.json())
.then((content) => {
return { playerId: player.pid, content };
})
);
}
const searchResults = await Promise.all(jobs);
this.#appendFilterResultToModal(middle, searchResults);
const noResult = middle.childNodes.length === 0;
if (noResult) {
middle.innerHTML = "No Players To Display
Please refresh the filter to update hits.";
middle.style.padding = "10px";
middle.style.textAlign = "center";
} else {
info.querySelector(".filter-count-span").innerText = middle.childNodes.length.toString();
}
div.appendChild(header);
if (noResult) {
div.appendChild(middle);
div.appendChild(close);
} else {
div.appendChild(close);
div.appendChild(info);
div.appendChild(middle);
}
this.#replaceModalContent([div]);
}
async #displayPlayerComment(target, playerId) {
this.#displayLoading("MZY Player Note");
const header = createMzStyledTitle("MZY Player Note");
const text = document.createElement("textarea");
const cancel = createMzStyledButton("Cancel", "red");
const save = createMzStyledButton("Save", "green");
const buttons = document.createElement("div");
buttons.classList.add("mazyar-flex-container-row");
text.value = await this.#fetchPlayerCommentFromIndexedDb(playerId);
text.classList.add("mazyar-player-comment-textarea");
cancel.addEventListener("click", async () => {
this.hideModal();
});
save.addEventListener("click", async () => {
await this.#setPlayerCommentInIndexedDb(playerId, text.value);
if (text.value) {
target?.classList.remove("mazyar-player-comment-icon-inactive");
target?.classList.add("mazyar-player-comment-icon-active");
} else {
target?.classList.add("mazyar-player-comment-icon-inactive");
target?.classList.remove("mazyar-player-comment-icon-active");
}
this.hideModal();
});
buttons.appendChild(cancel);
buttons.appendChild(save);
this.#replaceModalContent([header, text, buttons]);
}
#getVersionNumbers(v) {
return v.split('.').map((i) => Number(i));
}
#getVersionsOfChangelog(changelogs) {
return Object.keys(changelogs).map((v) => this.#getVersionNumbers(v));
}
#isVersionLesserThan(version = [0, 0], base = [0, 0]) {
return version[0] < base[0] ||
(version[0] === base[0] && version[1] < base[1]);
}
#isVersionGreaterThan(version = [0, 0], base = [0, 0]) {
return version[0] > base[0] ||
(version[0] === base[0] && version[1] > base[1]);
}
#showChangelog() {
const previousVersion = GM_getValue("previous_version", "");
if (!previousVersion) {
GM_setValue("previous_version", currentVersion);
return;
}
const previous = this.#getVersionNumbers(previousVersion);
const current = this.#getVersionNumbers(currentVersion);
if (!this.#isVersionLesserThan(previous, current)) {
return;
}
const headHTML = `Mazyar is updated
` +
`from v${previousVersion} to v${currentVersion}`;
const head = document.createElement("div");
head.innerHTML = headHTML;
head.style.textAlign = "center";
const changesTitle = document.createElement("div");
changesTitle.innerHTML = `Changelog:`;
changesTitle.style.margin = "5px";
changesTitle.style.width = "100%";
changesTitle.style.width = "100%";
changesTitle.style.textAlign = "left";
let changesHTML = '';
const versions = this.#getVersionsOfChangelog(changelogs);
for (const version of versions) {
if (this.#isVersionGreaterThan(version, current)) {
continue;
}
if (this.#isVersionGreaterThan(version, previous)) {
const v = version.join('.');
changesHTML += `v${v}- `
+ changelogs[v]?.join("
- ") + "
";
}
}
const changes = document.createElement("div");
changes.innerHTML = changesHTML;
changes.style.maxHeight = "320px";
changes.style.maxWidth = "320px";
changes.style.paddingRight = "30px";
changes.style.backgroundColor = "khaki";
changes.style.padding = "5px";
changes.style.flex = "1";
changes.style.overflowY = "scroll"; // make it scrollable
const text = document.createElement("div");
text.classList.add("mazyar-flex-container");
text.style.margin = "10px";
text.style.padding = "5px";
text.appendChild(head);
text.appendChild(changesTitle);
text.appendChild(changes);
const header = createMzStyledTitle("MZY Notice");
const close = createMzStyledButton("close", "green");
close.addEventListener("click", async () => {
GM_setValue("previous_version", currentVersion);
this.hideModal();
});
this.#replaceModalContent([header, text, close]);
}
showPlayerInModal(playerView) {
const player = document.createElement("div");
player.style.margin = "5px";
player.style.padding = "0px";
player.style.backgroundColor = "wheat";
player.appendChild(playerView);
const header = createMzStyledTitle("MZY Player View");
const close = createMzStyledButton("close", "green");
close.addEventListener("click", async () => {
this.hideModal();
});
this.#replaceModalContent([header, player, close]);
}
hideModal() {
this.#modal.style.display = "none";
this.#clearModalContent();
}
}
/* *********************** Inject ********************************** */
function isVisitingTeamPage() {
const regex = /^\?((p=team)|(p=team&tid=\d+))$/g;
return regex.test(document.location.search);
}
async function inject() {
GM_addStyle(styles);
mazyar = new Mazyar();
if (mazyar.isTransferFiltersEnabled()) {
mazyar.setInitialFiltersHitInToolbar();
}
mazyar.injectTransferDeadlineAlert();
const uri = document.baseURI;
if (uri.search("/?p=federations") > -1) {
if (uri.search("&sub=clash") > -1) {
clashInjectRanks();
} else if (uri.search("&fid=") > -1 || uri.endsWith("p=federations")) {
federationFrontPage();
}
} else if (uri.search("/?p=players") > -1) {
squadInjectInformationToSummary();
squadInjectInformationToProfiles();
} else if (uri.search("mid=") > -1) {
matchInjectTeamValues();
} else if (uri.search("/?p=match") > -1 && !uri.search("&sub=result") > -1) {
matchInjectInProgressResults();
} else if (uri.search("/?p=league") > -1) {
tableInjectTopPlayersToOfficialLeague();
scheduleInjectColoringToOfficialLeague();
} else if (uri.search("/?p=friendlyseries") > -1) {
tableInjectTopPlayersInfoToFriendlyLeague();
} else if (uri.search("/?p=cup&") > -1 || uri.search("/?p=private_cup&") > -1) {
tableInjectTopPlayersInfoToCup();
} else if (uri.search("/?p=transfer") > -1) {
if (mazyar.isTransferFiltersEnabled()) {
transferInject();
}
mazyar.executeTransferTasks();
} else if (uri.search("/?p=clubhouse") > -1) {
if (mazyar.mustHelpWithPredictor()) {
predictorInject();
}
} else if (isVisitingTeamPage()) {
addTrophyCountToClubPage();
} else if (uri.search("/?p=trainers") > -1) {
if (mazyar.mustAddCoachSalaries()) {
trainersAddRequestedSalaries();
}
} else if (uri.search("/?p=training_report") > -1) {
trainingAddCampOpenerToReport();
trainingAddDays();
}
}
if (document.readyState === "loading") {
// Loading hasn't finished yet
document.addEventListener("DOMContentLoaded", () => {
inject();
});
} else {
// `DOMContentLoaded` has already fired
inject();
}
})();