考勤数据增强插件

统计迟到次数及加班次数

目前为 2024-11-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         考勤数据增强插件
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  统计迟到次数及加班次数
// @author       luc
// @match        http://maxvision.eicp.net:52800/maxhome/KQ/UserKq
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const originalFetch = window.fetch;
    window.fetch = function(url, options) {
        if (options && options.method === 'POST' && url.includes('getdataByuser')) {
            return originalFetch(url, options).then(response => {
                response.clone().json().then(data => {
                    processData(data);
                });
                return response;
            });
        }
        return originalFetch(url, options);
    };

    const originalXHR = window.XMLHttpRequest;
    window.XMLHttpRequest = function() {
        const xhr = new originalXHR();
        const originalOpen = xhr.open;
        const originalSend = xhr.send;

        xhr.open = function(method, url) {
            this._url = url;
            this._method = method;
            return originalOpen.apply(this, arguments);
        };

        xhr.send = function(data) {
            if (this._method === 'POST' && this._url.includes('getdataByuser')) {
                xhr.addEventListener('load', function() {
                    const responseData = JSON.parse(xhr.responseText);
                    processData(responseData);
                });
            }
            return originalSend.apply(this, arguments);
        };

        return xhr;
    };

    function processData(data) {
        let countFirstTimeAfter0830 = 0;
        let countAfter1930 = 0;
        let countAfter2030 = 0;

        const kqData = data.KQ;

        kqData.forEach(item => {
            const firstTime = item.firstTime;
            const lastTime = item.lastTime;

            if (firstTime) {
                const [firstHour, firstMinute] = firstTime.split(':').map(Number);
                if (firstHour > 8 || (firstHour === 8 && firstMinute > 30)) {
                    countFirstTimeAfter0830++;
                }
            }

            if (lastTime) {
                const [lastHour, lastMinute] = lastTime.split(':').map(Number);
                if (lastHour > 19 || (lastHour === 19 && lastMinute > 30)) {
                    countAfter1930++;
                }

                if (lastHour > 20 || (lastHour === 20 && lastMinute > 30)) {
                    countAfter2030++;
                    // 如果lastTime大于20:30,则不再计入19:30之后的统计
                    countAfter1930--;
                }
            }
        });

        displayStats(countFirstTimeAfter0830, countAfter1930, countAfter2030);
    }


    function displayStats(firstTimeCount, after1930Count, after2030Count) {
        const layuiRowElement = document.querySelector('.layui-row');

        const statsHTML = `
            <span style="margin-left: 50px; margin-right: 10px;">迟到:<strong style="font-weight: bold; color: red;">${firstTimeCount}</strong> 次</span>
            <span style="margin-left: 10px; margin-right: 10px;">加班至19:30以后:<strong style="font-weight: bold; color: red;">${after1930Count}</strong> 次</span>
            <span style="margin-left: 10px; margin-right: 10px;">加班至20:30以后:<strong style="font-weight: bold; color: red;">${after2030Count}</strong> 次</span>
        `;
        if (layuiRowElement) {
            layuiRowElement.insertAdjacentHTML('beforebegin', statsHTML);
        } else {
            console.log('未找到 class="layui-row" 元素');
        }
    }

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址