PA733 / 教务系统课程表修正

// ==UserScript==
// @name         教务系统课程表修正
// @namespace    http://tampermonkey.net/
// @license      MIT
// @version      0.1
// @description  修正教务系统课程表的现实问题(如课程名称被显示成开课班级)
// @author       PA733
// @match        *://jwxt.lut.edu.cn/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 监视 script 标签插入的变化
    const observer = new MutationObserver(function (mutationsList, observer) {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    if (node.tagName === 'SCRIPT' && node.src.includes('kbck.js')) {
                        // 拦截并替换目标脚本
                        interceptAndReplaceScript(node);
                    }
                });
            }
        }
    });

    observer.observe(document.head || document.documentElement, {
        childList: true,
        subtree: true
    });

    function interceptAndReplaceScript(scriptNode) {
        fetch(scriptNode.src)
            .then(response => response.text())
            .then(code => {
                // 美化教室名: kb.classroomName
                // 去除教室名前的节次
                let patchedCode = code.replace(
                    /kb\.classroomName\s*=\s*.*?;/s,
                    `kb.classroomName = (pub_param.action == "queryjaskb" ? "" : kb_data[i].JASMC);`
                );

                // 修正课程名: kb.className
                patchedCode = patchedCode.replace(
                    /kb\.className\s*=\s*.*?;/s,
                    `kb.className = (kb_data[i].KCM == null ? "" : kb_data[i].KCM);`
                );

                // 插入修改后的脚本
                const newScript = document.createElement('script');
                newScript.textContent = patchedCode;
                document.head.appendChild(newScript);

                // 移除原始 script 标签
                scriptNode.parentNode.removeChild(scriptNode);
            });
    }
})();