YSU教室课表标色

燕山大学教室课表标色

目前為 2021-05-08 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YSU教室课表标色
// @version      1.0.4
// @author       Haomin Kong
// @description  燕山大学教室课表标色
// @match        */zjdxgc/mycjcx/*
// @match        http://202.206.243.9/zjdxgc/mycjcx/wjskbcx.asp
// @match        http://jwc.ysu.edu.cn/*
// @icon         http://ysu.edu.cn/images/favicon.png
// @require	     https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// @grant	     GM_registerMenuCommand
// @grant	     GM_addStyle
// @license      GPL-3.0 License
// @run-at       document-end
// @namespace    https://gitee.com/a645162/ysu-web-browser-scripts
// ==/UserScript==

(function () {
    ("use strict");

    GM_registerMenuCommand("更新脚本", function () {
        window.location.href = "https://gitee.com/a645162/ysu-web-browser-scripts";
    });

    var week = 1;

    let CurrentConfig = {
        auto: true, //自动执行脚本
        week: 0, //自定义周数,0就是自动获取

        delform: true, //删除没课


        colornow: true, //标色当前周
        colorfuture: true, //标色未来周
        delnow: false, //删除当前周
        delfuture: false, //删除未来周

        ignore_selflearn: true, //忽略自习借教室
        ignore_tuanri: false,//团日
        ignore_pingxuan: false,//评选
        ignore_shuangxuanhui: false,//双选会
        ignore_zhaopin: false,//招聘
        ignore_xueshenghui: false,//学生会

        delignore: true, //删除上述忽略的
        //下面是保存上次的选择
        save_last_select: false,//是否要恢复
        last_building: -1,//教学楼
        last_room: -1,//教室
    };
    let DefaultConfig = {
        auto: true, //自动执行脚本
        week: 0, //自定义周数,0就是自动获取

        delform: true, //删除没课


        colornow: true, //标色当前周
        colorfuture: true, //标色未来周
        delnow: false, //删除当前周
        delfuture: false, //删除未来周

        ignore_selflearn: true, //忽略自习借教室
        ignore_tuanri: false,//团日
        ignore_pingxuan: false,//评选
        ignore_shuangxuanhui: false,//双选会
        ignore_zhaopin: false,//招聘
        ignore_xueshenghui: false,//学生会

        delignore: true, //删除上述忽略的
        //下面是保存上次的选择
        save_last_select: false,//是否要恢复
        last_building: -1,//教学楼
        last_room: -1,//教室
    };

    function UpdateSettings() {
        let settingString = JSON.stringify(CurrentConfig);
        GM_setValue("YSUJWConfig", settingString);
        //setCookie(("YSUKHMJSCOLOR", settingString));
    }

    Promise.all([GM_getValue("YSUJWConfig")])
        //Promise.all(getCookie("YSUKHMJSCOLOR"))
        .then(function (data) {
            if (data[0] !== undefined) {
                try {
                    CurrentConfig = JSON.parse(data[0]);
                } catch (e) {
                    CurrentConfig = data[0];
                }
            } else {
                CurrentConfig = DefaultConfig;
            }
            for (var key in DefaultConfig) {
                if (typeof CurrentConfig[key] == "undefined") {
                    CurrentConfig[key] = DefaultConfig[key];
                }
            }
        })
        .catch(function (except) {
            console.log("错误!请复制后上报bug。谢谢==>", except);
        });

    function DoJob() {
        //自动恢复上次的选择
        let sel1 = document.getElementsByTagName("select");
        if (CurrentConfig.last_building < sel1[0].length ||
            CurrentConfig.last_room < sel1[1].length) {
            CurrentConfig.last_building = 0;
            CurrentConfig.last_room = 0;
            CurrentConfig.save_last_select = false;
        }
        if (CurrentConfig.save_last_select) {
            if (CurrentConfig.last_building !== sel1[0].selectedIndex ||
                CurrentConfig.last_room !== sel1[1].selectedIndex) {
                sel1[0].selectedIndex = CurrentConfig.last_building;
                sel1[1].selectedIndex = CurrentConfig.last_room;

                document.getElementsByName("Submit")[0].click()
            }
        }

        let weekText = /本周为第[0-9]+周周[0-9]/.exec(
            document.getElementsByTagName("body")[0].textContent
        )[0];
        weekText = /第[0-9]+周/.exec(weekText);
        week = parseInt(/[0-9]+/.exec(weekText)[0]);
        let as = document.getElementsByTagName("table");
        for (let i = 0, j = as.length; i < j; ++i) {
            TraverseTable(as[i]);
        }
    }

    function TraverseTable(tableObj) {
        for (var i = 0, i1 = tableObj.rows.length; i < i1 && i1 >= 8; i++) {
            for (var j = 0, j1 = tableObj.rows[i].cells.length; j < j1; j++) {
                re(tableObj.rows[i].cells[j]);
            }
        }
    }

    function re(td) {
        let text = td.innerText.trim();
        let regExp = /[0-9]+-[0-9]+周/g;
        if (!regExp.test(text)) {
            return;
        }
        let courseSplit = text.split("\n\n");
        let now = week;
        if (CurrentConfig.week > 0) {
            now = CurrentConfig.week;
        }
        //now = 10;

        let state = 0;

        let needIgnore = false;

        for (let i = 0, j = courseSplit.length; i < j; i++) {
            let current = courseSplit[i].trim();
            //console.log(current);
            //type:1正常上课 2考试 3借用
            let type = 0;


            if (current.indexOf("正常上课") !== -1) {
                type = 1;
            } else if (current.indexOf("考试") !== -1) {
                type = 2;
            } else if (current.indexOf("借用") !== -1) {
                type = 3;
            }

            let oneLineText = current;
            while (oneLineText.indexOf("\n") !== -1) {
                oneLineText = oneLineText.replace("\n", "");
            }
            let match = /[0-9]+-[0-9]+周/g.exec(oneLineText);
            match = /[0-9]+-[0-9]+/.exec(match[0].trim())[0].trim();
            let l = match.split("-");
            if (l.length === 2) {
                let start = parseInt(l[0]);
                let end = parseInt(l[1]);

                if (start <= now && now <= end) {
                    //还在上
                    state = 1;
                }

                if (now < start && state !== 1) {
                    //未开课
                    state = 2;
                }

                if (type === 3 && now <= end) {
                    if (
                        (CurrentConfig.ignore_selflearn && current.indexOf("自习") !== -1) ||
                        (CurrentConfig.ignore_tuanri &&
                            (current.indexOf("团日") !== -1 ||
                                current.indexOf("团课") !== -1 ||
                                current.indexOf("团活") !== -1)) ||
                        (CurrentConfig.ignore_pingxuan && current.indexOf("评选") !== -1) ||
                        (CurrentConfig.ignore_shuangxuanhui && current.indexOf("双选会") !== -1) ||
                        (CurrentConfig.ignore_zhaopin && current.indexOf("招聘") !== -1) ||
                        (CurrentConfig.ignore_xueshenghui && current.indexOf("学生会") !== -1)
                    ) {
                        type = 0;
                        needIgnore = true;
                    }
                }
            }


        }

        if (courseSplit.length !== 0) {
            if (needIgnore) {
                td.bgColor = "FFFFFF";
                if (CurrentConfig.delignore) {
                    td.textContent = "";
                }
            } else if (state === 1) {
                if (CurrentConfig.colornow) {
                    td.bgColor = "FFFFBB";
                }
                if (CurrentConfig.delnow) {
                    td.textContent = "";
                }
            } else if (state === 2) {
                if (CurrentConfig.colorfuture) {
                    td.bgColor = "FFDDDD";
                }
                if (CurrentConfig.delfuture) {
                    td.textContent = "";
                }
            } else {
                td.bgColor = "FFFFFF";

                if (CurrentConfig.delform) {
                    td.textContent = "";
                }
            }
        }
    }

    function newCheckbox(parent, title, checked, change_listener, before_line, after_line) {
        let cb = document.createElement("input");
        cb.type = "checkbox";
        cb.checked = checked;
        if (change_listener != null) {
            cb.addEventListener("change", change_listener);
        }

        parent.appendChild(cb);

        for (let i = 0; i < after_line; ++i) {
            cb.insertAdjacentHTML("afterend", "<br />");
        }
        cb.insertAdjacentText("afterend", title);

        for (let i = 0; i < before_line; ++i) {
            cb.insertAdjacentHTML("beforebegin", "<br />");
        }
        return cb;
    }

    function newButton(parent, title, click_listener, before_line, after_line) {
        let btn = document.createElement("Button");
        btn.addEventListener("click", click_listener);
        btn.innerText = title;
        parent.appendChild(btn);
        for (let i = 0; i < before_line; ++i) {
            btn.insertAdjacentHTML("beforebegin", "<br />");
        }
        for (let i = 0; i < after_line; ++i) {
            btn.insertAdjacentHTML("afterend", "<br />");
        }
        return btn;
    }

    function addLink() {
        let submit = document.getElementsByName("Submit")[0];
        let parent1 = submit.parentNode;
        let parent2 = submit.parentNode.parentNode.childNodes[1];

        submit.insertAdjacentHTML("afterend", "<br />");

        newCheckbox(parent1, "自动恢复保存的选择", CurrentConfig.save_last_select,
            function (e) {
                CurrentConfig.save_last_select = e.target.checked;
                let sel1 = document.getElementsByTagName("select");

                CurrentConfig.last_building = sel1[0].selectedIndex;
                CurrentConfig.last_room = sel1[1].selectedIndex;

                UpdateSettings();
            }, 1, 0);

        newButton(parent1, "保存当前的选择",
            function () {
                let sel1 = document.getElementsByTagName("select");

                CurrentConfig.last_building = sel1[0].selectedIndex;
                CurrentConfig.last_room = sel1[1].selectedIndex;

                UpdateSettings();
            }, 0, 1);


        //let as = document.getElementsByTagName("table");


        newCheckbox(parent1, "自动执行脚本", CurrentConfig.auto,
            function (e) {
                CurrentConfig.auto = e.target.checked;
                UpdateSettings();
            }, 1, 0);

        newCheckbox(parent1, "自动删除没课的格子", CurrentConfig.delform,
            function (e) {
                CurrentConfig.delform = e.target.checked;
                UpdateSettings();
            }, 1, 2);


        newCheckbox(parent1, "忽略借教室自习(这真没意思!!!)", CurrentConfig.ignore_selflearn,
            function (e) {
                CurrentConfig.ignore_selflearn = e.target.checked;
                UpdateSettings();
            }, 1, 0);


        newCheckbox(parent1, "忽略团日/团课活动", CurrentConfig.ignore_tuanri, function (e) {
            CurrentConfig.ignore_tuanri = e.target.checked;
            UpdateSettings();
        }, 1, 0);

        newCheckbox(parent1, "忽略评选", CurrentConfig.ignore_pingxuan,
            function (e) {
                CurrentConfig.ignore_pingxuan = e.target.checked;
                UpdateSettings();
            }, 1, 0);
        newCheckbox(parent1, "忽略招聘", CurrentConfig.ignore_zhaopin,
            function (e) {
                CurrentConfig.ignore_zhaopin = e.target.checked;
                UpdateSettings();
            }, 1, 0);
        newCheckbox(parent1, "忽略双选会", CurrentConfig.ignore_shuangxuanhui,
            function (e) {
                CurrentConfig.ignore_shuangxuanhui = e.target.checked;
                UpdateSettings();
            }, 1, 0);
        newCheckbox(parent1, "忽略学生会", CurrentConfig.ignore_xueshenghui,
            function (e) {
                CurrentConfig.ignore_xueshenghui = e.target.checked;
                UpdateSettings();
            }, 1, 0);

        newCheckbox(parent1, "自动删除上述忽略的格子", CurrentConfig.delignore,
            function (e) {
                CurrentConfig.delignore = e.target.checked;
                UpdateSettings();
            }, 1, 0);


        if (!CurrentConfig.auto) {
            newButton(parent1, "执行脚本",
                function () {
                    DoJob();
                }, 1, 1);
        }

        //第二个格子

        let text1 = document.createElement("input");
        text1.id = "text_week";
        text1.type = "number";
        text1.max = 20;
        text1.min = 0;
        text1.value = CurrentConfig.week.toString();
        parent2.appendChild(text1);
        text1.insertAdjacentText("afterend", "周(0为自动获取)");
        text1.insertAdjacentHTML("beforebegin", "设置当前周数:");

        newButton(parent2, "保存",
            function () {
                CurrentConfig.week = parseInt(text1.value);
                UpdateSettings();
            }, 0, 1);


        newCheckbox(parent2, "标色当前周有课", CurrentConfig.colornow,
            function (e) {
                CurrentConfig.colornow = e.target.checked;
                UpdateSettings();
            }, 1, 0);

        newCheckbox(parent2, "删除当前周有课", CurrentConfig.delnow,
            function (e) {
                CurrentConfig.delnow = e.target.checked;
                UpdateSettings();
            }, 0, 0).insertAdjacentHTML("beforebegin", "&nbsp;&nbsp;");

        newCheckbox(parent2, "标色未来周有课", CurrentConfig.colorfuture,
            function (e) {
                CurrentConfig.colorfuture = e.target.checked;
                UpdateSettings();
            }, 1, 0);

        newCheckbox(parent2, "删除未来周有课", CurrentConfig.delfuture,
            function (e) {
                CurrentConfig.delfuture = e.target.checked;
                UpdateSettings();
            }, 0, 1).insertAdjacentHTML("beforebegin", "&nbsp;&nbsp;");


        newButton(parent2, "刷新页面",
            function () {
                location.reload();
            }, 1, 0)

        newButton(parent2, "重新从教务处网站进入本页面",
            function () {
                window.location.href = "http://jwc.ysu.edu.cn/?action=jskb";
            }, 1, 0).insertAdjacentHTML("afterend", "刷新页面并不能刷新新周数");


        newButton(parent2, "进入教务系统62",
            function () {
                window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt62";
            }, 2, 0)

        newButton(parent2, "进入教务系统6(校内)",
            function () {
                window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt9";
            }, 0, 0)

        newButton(parent2, "进入教务系统9(校内)",
            function () {
                window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt9";
            }, 0, 0)

        newButton(parent2, "恢复默认设置",
            function () {
                CurrentConfig = DefaultConfig;
                UpdateSettings();
                location.reload();
            }, 2, 0)
        newButton(parent2, "更新脚本",
            function () {
                window.location.href = 'https://gitee.com/a645162/ysu-web-browser-scripts';
            }, 2, 0)


        var lab1 = document.createElement("a");
        lab1.innerText = " 脚本作者:孔昊旻";
        lab1.setAttribute("href", "https://gitee.com/a645162");

        parent2.appendChild(lab1);
    }

    //JS操作cookies方法!
    //写cookies
    function setCookie(name, value) {
        let Days = 30;
        let exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie =
            name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    //读取cookies
    function getCookie(name) {
        var arr,
            reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
        else return null;
    }

    //删除cookies
    function delCookie(name) {
        let exp = new Date();
        exp.setTime(exp.getTime() - 1);
        let cval = getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }

    function print() {
        document.body.innerHTML = temp1.outerHTML;
    }

    const changeFavicon = link => {
        let $favicon = document.querySelector('link[rel="icon"]');
        // If a <link rel="icon"> element already exists,
        // change its href to the given link.
        if ($favicon !== null) {
            $favicon.href = link;
            // Otherwise, create a new element and append it to <head>.
        } else {
            $favicon = document.createElement("link");
            $favicon.rel = "icon";
            $favicon.href = link;
            document.head.appendChild($favicon);
        }
    };


    //DoJob();
    //addLink();
    function LoadJS() {
        let link = window.location.href.trim();
        if (link.indexOf("jwc.ysu.edu.cn") !== -1) {
            let regexStr = /\?action=.*/;
            if (regexStr.test(link)) {
                let command = regexStr.exec(link)[0].trim()
                if (/=jskb/.test(command)) {
                    window.location.href = "http://202.206.243.9/zjdxgc/mycjcx/wjskbcx.asp";
                } else if (/=jwxt/.test(command)) {
                    const url_jwct = "http://202.206.243.";
                    let num = /[0-9]+/.exec(/=jwxt.*/.exec(link)[0])
                    window.location.href = url_jwct.trim() + num;
                }
            }
        } else {
            if (CurrentConfig.auto) {
                DoJob();
            }
            addLink();
        }
    }

    window.onload = function () {
        changeFavicon("http://ysu.edu.cn/images/favicon.png");
        LoadJS();

    };
})();