ManagerZone Training Report Checker

Check daily training report status and visually indicate readiness

目前為 2024-06-22 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ManagerZone Training Report Checker
// @namespace    http://tampermonkey.net/
// @version      1.7
// @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-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() {
        const sport = new URL(document.querySelector("#shortcut_link_thezone").href).searchParams.get("sport");
        if (sport !== "soccer") {
            console.log('sport is not soccer.');
            return;
        }
        addTrainingReportLink();
        checkTrainingReport();
    }

    init();
})();