MZ - Training Report Checker

Checks training report status and visually indicate readiness on a daily basis (except for saturday)

当前为 2024-12-10 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MZ - Training Report Checker
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Checks training report status and visually indicate readiness on a daily basis (except for saturday)
// @author       Douglas
// @match        *://www.managerzone.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const CONFIG = {
        START_HOUR: 19,
        START_MINUTE: 1,
        END_HOUR: 23,
        END_MINUTE: 59,
        CHECK_INTERVAL: 300000, // 5 minutes in milliseconds
        TIMEZONE_OFFSET: -3     // Brazil timezone (can be adjusted)
    };

    const DAY_MAP = {
        0: 1, // Sunday
        1: 2, // Monday
        2: 3, // Tuesday
        3: 4, // Wednesday
        4: 5, // Thursday
        5: 6, // Friday
        6: null // Saturday
    };

    class TrainingReportChecker {
        constructor() {
            this.linkId = 'shortcut_link_trainingreport';
        }

        getBrazilianDate() {
            const date = new Date();
            const utc = date.getTime() + (date.getTimezoneOffset() * 60000);
            const brazilTime = new Date(utc + (3600000 * CONFIG.TIMEZONE_OFFSET));
            return brazilTime.toISOString().slice(0, 10);
        }

        getBrazilianTime() {
            const date = new Date();
            const utc = date.getTime() + (date.getTimezoneOffset() * 60000);
            return new Date(utc + (3600000 * CONFIG.TIMEZONE_OFFSET));
        }

        isWithinCheckWindow() {
            const now = this.getBrazilianTime();
            const hour = now.getHours();
            const minute = now.getMinutes();

            const startTime = hour * 60 + minute;
            const checkStartTime = CONFIG.START_HOUR * 60 + CONFIG.START_MINUTE;
            const checkEndTime = CONFIG.END_HOUR * 60 + CONFIG.END_MINUTE;

            return startTime >= checkStartTime && startTime <= checkEndTime;
        }

        addTrainingReportLink() {
            const targetDiv = document.getElementById('pt-wrapper');
            if (!targetDiv) {
                console.log('Target div not found.');
                return;
            }

            const link = document.createElement('a');
            link.id = this.linkId;
            link.title = 'Training Report';
            link.href = '/?p=training_report';
            link.innerHTML = '| <i class="fa fa-unlock"></i>';
            targetDiv.appendChild(link);
        }

        isReportReady(table) {
            return Array.from(table.querySelectorAll('tr'))
                .some(row => row.querySelector('img[src*="training_camp.png"]'));
        }

        markReportAsChecked() {
            const today = this.getBrazilianDate();
            localStorage.setItem('reportCheckedDate', today);
            document.getElementById(this.linkId).style.color = 'green';
        }

        hasReportBeenCheckedToday() {
            const today = this.getBrazilianDate();
            return localStorage.getItem('reportCheckedDate') === today;
        }

        async checkTrainingReport() {
            const today = new Date();
            const dayIndex = DAY_MAP[today.getDay()];

            if (!dayIndex) {
                console.log('No check needed: It is Saturday.');
                return;
            }

            if (!this.isWithinCheckWindow()) {
                console.log('Outside check window (19:01-23:59).');
                return;
            }

            if (this.hasReportBeenCheckedToday()) {
                console.log('Training report already checked today.');
                document.getElementById(this.linkId).style.color = 'green';
                return;
            }

            try {
                const response = await fetch(
                    `https://www.managerzone.com/ajax.php?p=trainingReport&sub=daily&sport=soccer&day=${dayIndex}&sort_order=desc&sort_key=modification&player_sort=all`
                );
                const text = await response.text();
                const parser = new DOMParser();
                const doc = parser.parseFromString(text, "text/html");
                const table = doc.querySelector("body > table:nth-child(3)");

                if (table && this.isReportReady(table)) {
                    console.log('Report is ready.');
                    this.markReportAsChecked();
                } else if (this.isWithinCheckWindow()) {
                    console.log('No report yet. Will check again in 5 minutes.');
                    setTimeout(() => this.checkTrainingReport(), CONFIG.CHECK_INTERVAL);
                }
            } catch (error) {
                console.error('Error fetching training report:', error);
            }
        }

        init() {
            const sport = new URL(document.querySelector("#shortcut_link_thezone").href)
                .searchParams.get("sport");

            if (sport !== "soccer") {
                console.log('Script not running: sport is not soccer.');
                return;
            }

            this.addTrainingReportLink();
            this.checkTrainingReport();
        }
    }

    const checker = new TrainingReportChecker();
    checker.init();
})();