ManagerZone Training Report Checker

Check daily training report status and visually indicate readiness

当前为 2024-06-20 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ManagerZone Training Report Checker
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Check daily training report status and visually indicate readiness
// @author       Douglas
// @match        *://www.managerzone.com/*
// @grant        none
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const dayMap = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: null};

    function getBrazilianDate() {
        const date = new Date();
        const utc = date.getTime() + (date.getTimezoneOffset() * 60000);
        const brazilTime = new Date(utc + (3600000 * -3));
        return brazilTime.toISOString().slice(0, 10); // YYYY-MM-DD format
    }

    function addTrainingReportLink() {
        const targetDiv = document.getElementById('pt-wrapper');
        if (targetDiv) {
            const link = document.createElement('a');
            link.id = 'shortcut_link_trainingreport';
            link.title = 'Training Report';
            link.href = '/?p=training_report';
            link.innerHTML = '| <i class="fa fa-unlock"></i><span> Training</span>';
            targetDiv.appendChild(link);
        } else {
            console.log('Target div not found.');
            return;
        }
    }

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

    function isWithinCheckWindow() {
        const currentHour = new Date().getHours();
        return (currentHour >= 19 || currentHour <= 0);
    }

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

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

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

        if (!dayIndex || !isWithinCheckWindow()) {
            console.log('No need to check training report. It is either Saturday or outside of the checking window (7 PM - 1 AM).');
            return;
        }

        if (hasReportBeenCheckedToday()) {
            console.log('TrainingReport has already been checked today.');
            document.getElementById('shortcut_link_trainingreport').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 && isReportReady(table)) {
                console.log('Report is ready.');
                markReportAsChecked();
            } else {
                console.log('No report yet. Will check again in 5 minutes.');
                setTimeout(checkTrainingReport, 300000); /* Will check again in 5 minutes */
            }
        } catch (error) {
            console.error('Error fetching training report:', error);
        }
    }

    function init() {
        addTrainingReportLink();
        checkTrainingReport();
    }

    init();
})();