計算時數小工具

計算累計時數

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

// ==UserScript==
// @name         計算時數小工具
// @namespace    amltbXlfd3U=
// @version      0.8
// @description  計算累計時數
// @author       Jimmy
// @match        https://cy.iwerp.net/portal/page/new_home.xhtml
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function main() {

        const tableContainer = document.querySelector('#formTemplate\\:attend_rec_datatable');

        if (tableContainer) {
            const panelContainer = document.querySelector('#formTemplate\\:attend_rec_panel_content');
            const container = document.createElement('div');
            container.setAttribute('id', 'formTemplate:result');
            container.style.paddingTop = '25px';
            container.style.textAlign = 'center';

            if (!document.querySelector('#formTemplate\\:result')) {
                panelContainer.appendChild(container);
            }

            const table = tableContainer.querySelector('table[role="grid"]')
            const tableObj = tableToObj(table);
            let breakFlag = false;
            let totalTime = 0;
            let time = '';

            Object.keys(tableObj).map((key, i) => {
                if (tableObj[key].班表日.match(/ \(日\)/)) {
                    breakFlag = true;
                }

                if (!breakFlag) {
                    let signIn = timeToDate(tableObj[key].簽到);
                    const signOut = timeToDate(tableObj[key].簽退);
                    let nextSharp = 30;

                    if (!tableObj[key].簽到) {
                        return;
                    }

                    if (i === 0) {
                        if (signIn.getHours() >= 10) {
                            nextSharp = 30;

                            if (signIn.getMinutes() > 30) {
                                nextSharp = 60;
                            }

                            totalTime += nextSharp - signIn.getMinutes();
                        }

                        return;
                    }

                    // 週三維護
                    if (signIn.getHours() < 6) {
                        return;
                    }

                    // 請假
                    if (signIn.getHours() >= 10) {
                        nextSharp = 30;

                        if (signIn.getMinutes() > 30) {
                            nextSharp = 60;
                        }

                        totalTime += nextSharp - signIn.getMinutes();

                        // 十二點半到一點之間來 時數要加半小時
                        if (signIn.getHours() == 12 && signIn.getMinutes() < 30) {
                            totalTime += 30;
                        }

                        signIn = timeToDate('09:00');
                    }

                    const timeDiff = new Date(signOut - signIn);

                    // 正常時數
                    if (timeDiff.getHours() == 17) {
                        totalTime += timeDiff.getMinutes();
                    }

                    // 超過正常時數
                    if (timeDiff.getHours() > 17) {
                        totalTime += timeDiff.getMinutes() + (timeDiff.getHours() - 17) * 60;
                    }

                    // 少時數
                    if (timeDiff.getHours() < 17 && signIn.getHours() < 10) {
                        const diffTime = new Date(signIn - signOut);

                        totalTime -= diffTime.getMinutes();

                        if (diffTime == 0 || diffTime.getHours() == 0) {
                            totalTime -= 60;
                        }
                    }
                }
            })

            let todaySignTime = timeToDate(tableObj[0].簽到);
            let supposeOffTime = new Date(new Date(todaySignTime).setMinutes(todaySignTime.getMinutes() + 540));

            // 今天請假,下班時間改為6點
            if (todaySignTime.getHours() >= 10) {
                supposeOffTime = timeToDate('18:00');
            }

            let signOutTime = new Date(supposeOffTime.setMinutes(supposeOffTime.getMinutes() - totalTime));


            const five = new Date()
            five.setHours('17');
            five.setMinutes('00');
            five.setSeconds('00');

            // 時數爆掉五點前就可以走,下班時間改為5點
            if (signOutTime < five) {
                signOutTime = five;
            }

            time = `${signOutTime.getHours()}:${('0' + signOutTime.getMinutes()).slice(-2)}`;

            // if (new Date() > timeToDate(time)) {
            //     totalTime += new Date(new Date() - timeToDate(time)).getMinutes();
            // }

            document.querySelector('#formTemplate\\:result').innerHTML = `已累計時數:${totalTime}, 今天${time}可以下班`;
        }
    }

    function timeToDate(time) {
        var timeParts = time.split(":");
        var hours = parseInt(timeParts[0], 10);
        var minutes = parseInt(timeParts[1], 10);

        var date = new Date();

        if (!isNaN(hours) && !isNaN(minutes)) {
            date.setHours(hours);
            date.setMinutes(minutes);
        }

        return date;
    }

    function tableToObj(table) {
        var rows = table.rows;
        var propCells = rows[0].cells;
        var propNames = [];
        var results = [];
        var obj, row, cells;

        for (var i = 0, iLen = propCells.length; i < iLen; i++) {
            propNames.push(propCells[i].textContent || propCells[i].innerText);
        }

        for (var j = 1, jLen = rows.length; j < jLen; j++) {
            cells = rows[j].cells;
            obj = {};

            for (var k = 0; k < iLen; k++) {
                obj[propNames[k]] = cells[k].textContent || cells[k].innerText;
            }
            results.push(obj)
        }
        return results;
    }

    function debounce(fn, delay) {
        var timeout = null;
        return function () {
            if (timeout) {
                return;
            } else {
                timeout = setTimeout(function () {
                    fn();
                    timeout = null;
                }, delay);
            }
        }
    }

    main();
    var el = document.documentElement;
    el.addEventListener('DOMSubtreeModified', debounce(main, 2000));
})();