mzFetchFriendlyMatchesInfo

mz: amount of friendlies played by each player during the current week

目前為 2023-06-28 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         mzFetchFriendlyMatchesInfo
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  mz: amount of friendlies played by each player during the current week
// @author       Douglas
// @match        *://www.managerzone.com/?p=challenges*
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  let teamId;
  let appearances = new Map();

  const sport = new URL(
    document.querySelector("#shortcut_link_thezone").href
  ).searchParams.get("sport");
  const challengeTemplateUrl = `https://www.managerzone.com/ajax.php?p=challenge&sub=personal-challenge-template&sport=${sport}`;

  const topPlacement = document.getElementById("top-placement");

  const playerMatchesDiv = document.createElement("div");
  playerMatchesDiv.style.display = "flex";
  playerMatchesDiv.style.justifyContent = "center";
  playerMatchesDiv.style.margin = "16px";
  playerMatchesDiv.style.display = "none";

  const playerMatchesTable = document.createElement("table");
  playerMatchesTable.style.borderCollapse = "collapse";
  playerMatchesTable.style.margin = "0 auto";
  playerMatchesTable.style.border = "1px solid white";
  playerMatchesTable.style.backgroundColor = "black";
  playerMatchesTable.style.fontFamily = "Montserrat";
  playerMatchesDiv.appendChild(playerMatchesTable);

  topPlacement.parentNode.insertBefore(
    playerMatchesDiv,
    topPlacement.nextSibling
  );

  const loadingMessageDiv = document.createElement("div");
  loadingMessageDiv.style.display = "none";
  loadingMessageDiv.style.display = "flex";
  loadingMessageDiv.style.justifyContent = "center";
  loadingMessageDiv.style.alignItems = "center";
  loadingMessageDiv.style.width = "100%";
  loadingMessageDiv.style.height = "100%";
  playerMatchesDiv.appendChild(loadingMessageDiv);

  const loadingMessage = document.createElement("p");
  loadingMessage.textContent = "Loading…";
  loadingMessage.style.color = "#d6204e";
  loadingMessage.style.fontFamily = "Montserrat";
  loadingMessage.style.fontSize = "16px";
  loadingMessageDiv.appendChild(loadingMessage);

  const toggleButton = document.createElement("button");
  toggleButton.style.width = "50px";
  toggleButton.style.height = "50px";
  toggleButton.style.borderRadius = "50%";
  toggleButton.style.padding = "8px";
  toggleButton.style.border = "none";
  toggleButton.style.cursor = "pointer";
  toggleButton.style.color = "white";
  toggleButton.style.background = "navy";
  toggleButton.style.transition = "background 0.5s";
  toggleButton.onclick = function () {
    if (playerMatchesDiv.style.display === "none") {
      if (appearances.size === 0) {
        loadingMessageDiv.style.display = "flex";
      }
      playerMatchesDiv.style.display = "flex";
      playerMatchesDiv.style.animation = "fade-in 0.5s";
    } else {
      playerMatchesDiv.style.animation = "fade-out 0.5s";
      setTimeout(() => {
        playerMatchesDiv.style.display = "none";
        loadingMessageDiv.style.display = "none";
      }, 500);
    }
  };
  toggleButton.style.margin = "16px";

  topPlacement.parentNode.insertBefore(toggleButton, topPlacement.nextSibling);

  fetch(window.location.href)
    .then((response) => response.text())
    .then((data) => {
      const parser = new DOMParser();
      const doc = parser.parseFromString(data, "text/html");

      const username = doc.getElementById("header-username").textContent;
      return fetch(
        `https://www.managerzone.com/xml/manager_data.php?sport_id=1&username=${username}`
      )
        .then((response) => response.text())
        .then((xmlData) => {
          const xmlDoc = parser.parseFromString(xmlData, "text/xml");

          const teamElement = Array.from(
            xmlDoc.getElementsByTagName("Team")
          ).find((teamElement) => teamElement.getAttribute("sport") === sport);
          teamId = teamElement.getAttribute("teamId");

          return fetch(challengeTemplateUrl);
        })
        .then((response) => response.text())
        .then((data) => {
          const parser = new DOMParser();
          const doc = parser.parseFromString(data, "text/html");

          const scheduleDiv = doc.getElementById("friendly_series_schedule");
          const calendarDiv = scheduleDiv.querySelector(".calendar");
          const calendarForm = calendarDiv.querySelector("#saveMatchTactics");

          const currentAndNextWeekfriendlyMatchesDivs = Array.from(
            calendarForm.querySelectorAll(".fss-has-matches:not(.row-hidden)")
          );

          if (currentAndNextWeekfriendlyMatchesDivs.length === 0) {
            loadingMessage.textContent =
              "No friendly matches have been played this week!";
            return;
          }

          const currentWeekFriendlyMatchesDiv =
            currentAndNextWeekfriendlyMatchesDivs[0];

          const matchDivs = ["d1", "d3", "d4", "d5", "d7"].map((className) =>
            currentWeekFriendlyMatchesDiv.querySelector(`.${className}`)
          );

          const matchIds = matchDivs.flatMap((div) => {
            const matchScoresLinks = Array.from(
              div.querySelectorAll("a.score-shown:not(.gray)")
            );
            return matchScoresLinks.map((link) => {
              const matchUrl = link.getAttribute("href");
              const matchId = matchUrl.split("mid=")[1].split("&")[0];
              return matchId;
            });
          });

          if (matchIds.length === 0) {
            loadingMessage.textContent =
              "No friendly matches have been played this week!";
            return;
          }

          matchIds.forEach((matchId) => {
            fetch(
              `https://www.managerzone.com/xml/match_info.php?sport_id=1&match_id=${matchId}`
            )
              .then((response) => response.text())
              .then((xmlData) => {
                const xmlDoc = parser.parseFromString(xmlData, "text/xml");

                const teamElements = Array.from(
                  xmlDoc.getElementsByTagName("Team")
                );
                const ourTeamElement = teamElements.find(
                  (teamElement) => teamElement.getAttribute("id") === teamId
                );

                const playerElements = Array.from(
                  ourTeamElement.getElementsByTagName("Player")
                );

                playerElements.forEach((playerElement) => {
                  const playerId = playerElement.getAttribute("id");
                  const playerName = playerElement.getAttribute("name");

                  if (appearances.has(playerId)) {
                    const playerInfo = appearances.get(playerId);
                    playerInfo.appearances += 1;
                    appearances.set(playerId, playerInfo);
                  } else {
                    appearances.set(playerId, {
                      name: playerName,
                      appearances: 1,
                    });
                  }
                });

                loadingMessageDiv.style.display = "none";
                playerMatchesTable.innerHTML = "";

                const headerRow = document.createElement("tr");
                const playerNameHeader = document.createElement("th");
                playerNameHeader.textContent = "Player";
                playerNameHeader.style.color = "#d6204e";
                playerNameHeader.style.border = "1px solid white";
                playerNameHeader.style.padding = "4px";
                const appearancesHeader = document.createElement("th");
                appearancesHeader.textContent = "Games";
                appearancesHeader.style.color = "#d6204e";
                appearancesHeader.style.border = "1px solid white";
                appearancesHeader.style.padding = "4px";
                headerRow.appendChild(playerNameHeader);
                headerRow.appendChild(appearancesHeader);
                playerMatchesTable.appendChild(headerRow);

                let sortedAppearances = Array.from(appearances).sort(
                  (a, b) => b[1].appearances - a[1].appearances
                );

                sortedAppearances.forEach(([playerId, playerInfo]) => {
                  const row = document.createElement("tr");
                  const nameCell = document.createElement("td");
                  nameCell.style.border = "1px solid white";
                  nameCell.style.padding = "4px";
                  const link = document.createElement("a");
                  link.href = `https://www.managerzone.com/?p=players&pid=${playerId}`;
                  link.textContent = playerInfo.name;
                  link.style.color = "white";
                  nameCell.appendChild(link);
                  const appearancesCell = document.createElement("td");
                  appearancesCell.textContent = playerInfo.appearances;
                  appearancesCell.style.color = "white";
                  appearancesCell.style.border = "1px solid white";
                  appearancesCell.style.padding = "4px";
                  row.appendChild(nameCell);
                  row.appendChild(appearancesCell);
                  playerMatchesTable.appendChild(row);
                });
              });
          });
        });
    })
    .catch((e) => {
      console.warn("Error: ", e);
    });
})();

const style = document.createElement("style");
style.textContent = `
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap');

@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}

@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
`;
document.head.appendChild(style);