Greasy Fork is available in English.

IVE出席率及缺席率計算器

幫助IVE學生計算出席率及缺席率

27.09.2017 itibariyledir. En son verisyonu görün.

// ==UserScript==
// @name         IVE出席率及缺席率計算器
// @namespace    undefined
// @version      1.0.4
// @description  幫助IVE學生計算出席率及缺席率
// @author       Miklet
// @match        *://myportal.vtc.edu.hk/*
// @require      https://code.jquery.com/jquery-latest.js
// @require      https://greasyfork.org/scripts/33537-grant-none-shim-js/code/grant-none-shimjs.js?version=220694
// @run-at       document-start
// @grant        unsafeWindow
// ==/UserScript==
(function() {
    'use strict';
    $(function() {
        // Create Elements
        var btn_div = document.createElement("div");
        btn_div.setAttribute("style",";margin:10px;position:absolute;right:0;bottom:0;border:0;");
        btn_div.setAttribute("id","btnDiv");
        var calc_btn = document.createElement("button");
        calc_btn.setAttribute("style",";padding:10px;background-color:#555;color:#fff;border:0;");
        calc_btn.setAttribute ("id","btnCalc");
        calc_btn.innerHTML = "計算出缺率";
        var setting_btn = document.createElement("button");
        setting_btn.setAttribute("style",";margin-left:10px;padding:10px;background-color:#555;color:#fff;border:0;");
        setting_btn.setAttribute("id","btnSet");
        // Check Table Exist
        if($('table.hkvtcsp_wording').length){
            document.body.insertBefore(btn_div, document.body.firstChild);
            document.getElementById("btnDiv").append(calc_btn);
            var sbjCls = document.getElementsByClassName("hkvtcsp_textInput");
            var sbj = sbjCls[0].options[sbjCls[0].selectedIndex].text;
            sbjChk(sbj);
            document.getElementById ("btnCalc").addEventListener (
                "click", function(){
                    if(GM_getValue(sbj)===null){
                        var sbjTime = prompt("請輸入該單元的總時數");
                        if(sbjTime !== "" && sbjTime == parseInt(sbjTime, 10)){
                            calculate(sbjTime);
                            GM_setValue(sbj, sbjTime);
                            sbjChk(sbj);
                        }else{
                            alert('發生錯誤');
                        }
                    }else{
                        calculate(GM_getValue(sbj));
                    }
                }, false
            );
        }
        // Check Setting Button Status
        function sbjChk(sbj){
            if(GM_getValue(sbj)!==null && $("#btnSet").length===0){
                setting_btn.innerHTML = "編輯[" + GM_getValue(sbj) +"]小時";
                document.getElementById("btnDiv").appendChild(setting_btn);
                document.getElementById ("btnSet").addEventListener (
                    "click", function(){
                        var editSbjTime = prompt("編輯該單元的總時數",GM_getValue(sbj));
                        if(editSbjTime !== "" && editSbjTime == parseInt(editSbjTime, 10)){
                            GM_setValue(sbj, editSbjTime);
                            document.getElementById("btnSet").innerHTML = "編輯[" + editSbjTime +"]小時";
                            alert("編輯成功");
                        }else{
                            alert("發生錯誤");
                        }
                    }, false);
            }   
        }
        // Time Convert
        function time_convert(input_min) {
            var output_hr = Math.floor( input_min / 60);          
            var output_min = input_min % 60;
            if (output_min !== 0) {
                return output_hr + "小時" + output_min + "分鐘";
            } else {
                return output_hr + "小時";
            }
        }
        // Calculate Output
        function calculate(sbjTime) {
            var tb_array = [], headers = [];
            $('table.hkvtcsp_wording th').each(function(index, item) {
                headers[index] = $(item).text();
            });
            $('table.hkvtcsp_wording tr').has('td').each(function() {
                var arrayItem = {};
                $('td', $(this)).each(function(index, item) {
                    arrayItem[headers[index]] = $(item).text();
                });
                tb_array.push(arrayItem);
            });
            var att_lesson = 0, abs_lesson = 0, late_lesson = 0, tt_lesson_time = 0, tt_att_time = 0, tt_abs_time = 0, att_time, abs_time, i=0;
            for (i = 0; i < tb_array.length; i++) {
                var lesson_time_array = tb_array[i]['課堂時間'].split("-");
                //Lesson Count
                switch (tb_array[i]['']) {
                    case 'Present':
                        att_lesson++;
                        break;
                    case 'Late':
                        late_lesson++;
                        break;
                    case 'Absent':
                        abs_lesson++;
                        break;
                }
                //ABS & ATT Caculate
                var arrived_time = tb_array[i]['出席時間'];
                var row_lesson_time_start = new Date();
                var lesson_time_array_0 = lesson_time_array[0].split(':');
                row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                var row_lesson_time_end = new Date();
                var lesson_time_array_1 = lesson_time_array[1].split(':');
                row_lesson_time_end.setHours(lesson_time_array_1[0], lesson_time_array_1[1]);
                var row_lesson_time_arrived = new Date();
                if (arrived_time != '-') {
                    arrived_time = arrived_time.split(':');
                    row_lesson_time_arrived.setHours(arrived_time[0], arrived_time[1]);
                    if (row_lesson_time_arrived > row_lesson_time_start.setMinutes(row_lesson_time_start.getMinutes() + 10)) {
                        row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                        att_time = (row_lesson_time_end - row_lesson_time_arrived) / 1000 / 60;
                        abs_time = (row_lesson_time_arrived - row_lesson_time_start) / 1000 / 60;
                    } else {
                        row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                        att_time = (row_lesson_time_end - row_lesson_time_start) / 1000 / 60;
                        abs_time = 0;
                    }
                } else {
                    att_time = 0;
                    abs_time = (row_lesson_time_end - row_lesson_time_start) / 1000 / 60;
                }
                tt_lesson_time = tt_lesson_time + ((row_lesson_time_end - row_lesson_time_start) / 1000 / 60);
                tt_att_time = tt_att_time + att_time;
                tt_abs_time = tt_abs_time + abs_time;
            }
            var time_remain;
            if(sbjTime*60*0.3>=tt_abs_time){
                time_remain = (sbjTime*60*0.3)-tt_abs_time;
                time_remain = time_convert(time_remain);
            }else{
                time_remain = "/";
            }
            alert("已上課堂時數 : "+time_convert(tt_lesson_time)+"\n\n"+
                  "總出席時數 : "+time_convert(tt_att_time)+"\n"+
                  "出席率 : "+(tt_att_time / (sbjTime*60) * 100).toFixed(2)+"%\n\n"+
                  "總缺席時數 : "+time_convert(tt_abs_time)+"\n"+
                  "缺席率 : "+(tt_abs_time / (sbjTime*60) * 100).toFixed(2)+"%\n\n"+
                  "每小時缺席率 : "+(60 / (sbjTime*60) * 100).toFixed(2)+"%\n"+
                  "剩餘可缺席時數 : "+time_remain
                 );
        }
    });
})();