Soccerway Match Data Exporter

Exports match data from Soccerway match reports

当前为 2025-04-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Soccerway Match Data Exporter
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Exports match data from Soccerway match reports
// @author       Hendrik Steinmetz
// @match        https://*.soccerway.com/matches/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=soccerway.com
// @grant        GM_addStyle
// @grant        GM_setClipboard
// @license      GPLv3
// ==/UserScript==

(function () {
  "use strict";

  let btn = document.createElement("button");
  btn.id = "floatingButton";
  btn.textContent = "Export";
  btn.onclick = async function () {
    await generateMatchData();
    const report = {
      source: "soccerway",
      players,
      events,
    };
    GM_setClipboard(JSON.stringify(report), "json");
    if (report.players.length > 0) {
      const originalText = btn.textContent;
      btn.style.backgroundColor = "#28a745";
      btn.textContent = "Copied!";
      setTimeout(() => {
        btn.textContent = originalText;
        btn.style.backgroundColor = "#007BFF";
      }, 1500);
    }
  };

  const players = [];
  const events = {
    goals: [],
    substitutions: [],
    cards: [],
  };

  function handlePlayerRow(row, isHome, isSub) {
    console.log(row);

    const bookings = row.querySelector(".bookings");
    const subEvent =
      row.querySelector(".substitute-in") &&
      row.querySelector(".substitute-out");
    let playerName = row
      .querySelector(".player")
      .innerText.split("for")[0]
      .trim();
    const playerObj = {
      id: row.querySelectorAll("a")[0].href,
      name: playerName,
      number: row.querySelector(".shirtnumber").innerText.trim(),
      home: isHome,
      sub: isSub,
    };
    players.push(playerObj);

    if (isSub && subEvent) {
      const subIn = row.querySelector(".substitute-in a");
      const subOut = row.querySelector(".substitute-out a");
      let minuteText = row
        .querySelector(".substitute-out")
        .innerText.trim()
        .split(" ")
        .pop();
      minuteText = minuteText.replace("'", "");
      const minute = minuteText.split("+")[0];
      const added = minuteText.includes("+") ? minuteText.split("+")[1] : null;

      events.substitutions.push({
        home: isHome,
        minute,
        added,
        on: subIn.href,
        off: subOut.href,
      });
    }

    if (bookings) {
      const arr = Array.from(bookings.querySelectorAll("span"));
      arr.forEach((booking) => {
        const imgUrl = booking.querySelector("img").src;
        const time = booking.innerText.trim().replace("'", "");
        const added = time.includes("+") ? time.split("+")[1] : null;
        const minute = time.split("+")[0];

        const filename = imgUrl.split("/").pop();

        if (filename.includes("G.png")) {
          events.goals.push({
            home: isHome,
            minute,
            added,
            player: playerObj.id,
          });
        } else if (filename.includes("YC.png")) {
          events.cards.push({
            home: isHome,
            minute,
            added,
            player: playerObj.id,
            type: "yellow",
          });
        } else if (filename.includes("RC.png")) {
          events.cards.push({
            home: isHome,
            minute,
            added,
            player: playerObj.id,
            type: "red",
          });
        } else if (filename.includes("Y2C.png")) {
          events.cards.push({
            home: isHome,
            minute,
            added,
            player: playerObj.id,
            type: "yellow-red",
          });
        }
      });
    }
  }

  async function generateMatchData() {
    players.length = 0;
    events.cards.length = 0;
    events.goals.length = 0;
    events.substitutions.length = 0;

    const homePlayersStarting = document.querySelector(
      "div.combined-lineups-container:nth-child(16) > div:nth-child(1) > table:nth-child(1)"
    ).rows;
    const awayPlayersStarting = document.querySelector(
      "div.right:nth-child(2) > table:nth-child(1)"
    ).rows;
    const homePlayersSubs = document.querySelector(
      "div.left:nth-child(2) > table:nth-child(1)"
    ).rows;
    const awayPlayersSubs = document.querySelector(
      "div.combined-lineups-container:nth-child(17) > div:nth-child(3) > table:nth-child(1)"
    ).rows;

    Array.from(homePlayersStarting)
      .slice(1, -1)
      .forEach((row) => handlePlayerRow(row, true, false));

    Array.from(awayPlayersStarting)
      .slice(1, -1)
      .forEach((row) => handlePlayerRow(row, false, false));

    Array.from(homePlayersSubs)
      .slice(1)
      .forEach((row) => handlePlayerRow(row, true, true));

    Array.from(awayPlayersSubs)
      .slice(1)
      .forEach((row) => handlePlayerRow(row, false, true));
  }

  GM_addStyle(`
    #floatingButton {
        position: fixed;
        bottom: 20px;
        right: 20px;
        padding: 10px 20px;
        background-color: #007BFF;
        color: white;
        font-size: 16px;
        font-weight: bold;
        border: none;
        border-radius: 10px;
        box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
        cursor: pointer;
        z-index: 9999;
        transition: background-color 0.3s ease, transform 0.2s ease;
    }
    #floatingButton:hover {
        background-color: #0056b3;
        transform: scale(1.1);
    }`);
  document.body.appendChild(btn);
})();