Greasy Fork

ManagerZone Training Report Checker

Check daily training report status and visually indicate readiness

目前为 2024-06-20 提交的版本。查看 最新版本

// ==UserScript==
// @name         ManagerZone Training Report Checker
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  Check daily training report status and visually indicate readiness
// @author       You
// @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-lock"></i><span> Training</span>';
            targetDiv.appendChild(link);
        } else {
            console.log('Target div not found.');
            return;
        }
    }

    function updateTrainingReportIcon(isReady) {
        const icon = document.querySelector('#shortcut_link_trainingreport i');
        if (icon) {
            icon.className = isReady ? 'fa fa-unlock' : 'fa fa-lock';
        }
    }

    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);
        updateTrainingReportIcon(true);
    }

    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('Training Report has already been checked today.');
            updateTrainingReportIcon(true);
            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();
        if (hasReportBeenCheckedToday()) {
            updateTrainingReportIcon(true);
        }
        checkTrainingReport();
    }

    init();
})();