FMP Player Stats Summary (Accurate Placement)

Add total and average row to correct stats table

目前為 2025-05-31 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         FMP Player Stats Summary (Accurate Placement)
// @namespace    https://osama.dev
// @version      1.2
// @description  Add total and average row to correct stats table
// @match        https://footballmanagerproject.com/Team/Player?id=*
// @grant        none
// ==/UserScript==

(function () {
    function parseNum(txt) {
        const n = parseFloat(txt);
        return isNaN(n) ? 0 : n;
    }

    function getCorrectTable() {
        const tables = document.querySelectorAll('table');
        for (let table of tables) {
            const headers = Array.from(table.querySelectorAll('th')).map(th => th.innerText.trim().toLowerCase());
            if (headers.includes('matches') && headers.includes('goals') && headers.includes('rating')) {
                return table;
            }
        }
        return null;
    }

    function addStatsSummary() {
        const table = getCorrectTable();
        if (!table) return;

        const rows = Array.from(table.querySelectorAll('tbody tr')).filter(r => r.children.length >= 9);
        if (!rows.length) return;

        let totals = { matches: 0, goals: 0, assists: 0, yellow: 0, red: 0, rating: 0 };
        let ratingCount = 0;

        rows.forEach(row => {
            const cells = row.querySelectorAll('td');
            totals.matches += parseNum(cells[3].innerText);
            totals.goals += parseNum(cells[4].innerText);
            totals.assists += parseNum(cells[5].innerText);
            totals.yellow += parseNum(cells[6].innerText);
            totals.red += parseNum(cells[7].innerText);
            const rate = parseNum(cells[8].innerText);
            if (rate > 0) {
                totals.rating += rate;
                ratingCount++;
            }
        });

        const avgRating = ratingCount ? (totals.rating / ratingCount).toFixed(2) : '0.00';

        const summaryRow = document.createElement('tr');
        summaryRow.style.backgroundColor = '#334400';
        summaryRow.style.color = 'yellow';
        summaryRow.style.fontWeight = 'bold';

        summaryRow.innerHTML = `
            <td colspan="3">📊 Summary</td>
            <td>${totals.matches}</td>
            <td>${totals.goals}</td>
            <td>${totals.assists}</td>
            <td>${totals.yellow}</td>
            <td>${totals.red}</td>
            <td>${avgRating}</td>
            <td></td>
        `;

        table.querySelector('tbody').appendChild(summaryRow);
    }

    window.addEventListener('load', () => {
        setTimeout(addStatsSummary, 1000);
    });
})();