HSTC自动评教

自动选择评教表单,自动填充默认评语,专注于帮助大学生从评教中释放出来

// ==UserScript==
// @name         HSTC自动评教
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  自动选择评教表单,自动填充默认评语,专注于帮助大学生从评教中释放出来
// @author       Dlany-Cohhh
// @match        *://jw.hstc*
// @match        *://webvpn.hstc.edu.cn/http-80*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hstc.edu.cn
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    // 标志变量,用于记录是否已经弹出过弹窗
    let popupShown = false;

    // 自动选择"优"或分值最高的选项
    function autoSelectExcellent() {
        // 选择所有单选题
        let radios = document.querySelectorAll('input[type="radio"]');
        let grouped = {};
        radios.forEach(radio => {
            if (!grouped[radio.name]) grouped[radio.name] = [];
            grouped[radio.name].push(radio);
        });

        // 收集所有包含值为0或1的选项组
        let validGroups = Object.values(grouped).filter(group =>
            group.some(r => r.value === "0" || r.value === "1")
        );

        if (validGroups.length > 0) {
            // 随机选择一个组设为1
            let randomGroupIndex = Math.floor(Math.random() * validGroups.length);
            validGroups.forEach((group, index) => {
                // 找到组内值为0或1的选项
                let zeroOrOneOptions = group.filter(r => r.value === "0" || r.value === "1");

                if (zeroOrOneOptions.length > 0) {
                    if (index === randomGroupIndex) {
                        // 随机选中的组:找值为1的选项并选中
                        let oneOption = zeroOrOneOptions.find(r => r.value === "1");
                        if (oneOption) {
                            oneOption.checked = true;
                        } else {
                            // 如果没有值为1的选项,随机选一个
                            zeroOrOneOptions[0].checked = true;
                        }
                    } else {
                        // 其他组:找值为0的选项并选中
                        let zeroOption = zeroOrOneOptions.find(r => r.value === "0");
                        if (zeroOption) {
                            zeroOption.checked = true;
                        } else {
                            // 如果没有值为0的选项,随机选一个
                            zeroOrOneOptions[0].checked = true;
                        }
                    }
                }
            });
        }
    }

    // 自动填写评语
    function autoFillComment(comment = "老师讲课认真,教学效果优秀,受益匪浅!") {
        let textareas = document.querySelectorAll('textarea');
        textareas.forEach(area => {
            area.value = comment;
        });
        let inputs = document.querySelectorAll('input[type="text"]');
        inputs.forEach(input => {
            input.value = comment;
        });
    }

    // 自动点击提交按钮(可选,建议手动检查后再提交)
    function autoSubmit() {
        // 拦截确认弹窗并自动点击"确认"
        const originalConfirm = window.confirm;
        window.confirm = function() {
            console.log("拦截到确认弹窗,自动点击确认");
            // 恢复原始confirm方法
            window.confirm = originalConfirm;
            // 返回true表示点击"确认"
            return true;
        };

        // 查找并点击提交按钮
        let btn = document.querySelector('button[type="submit"],input[value="提交"],.btn-submit,#submit');
        if (btn) {
            console.log("找到提交按钮,准备点击");
            btn.click();
            console.log("已点击");
        }
    }

    // 创建弹窗
    function createPopup() {
        // 避免重复创建
        if (document.getElementById('auto-eval-popup') || popupShown) return;
        let popup = document.createElement('div');
        popup.id = 'auto-eval-popup';
        popup.style.position = 'fixed';
        popup.style.top = '32px';
        popup.style.left = '50%';
        popup.style.transform = 'translate(-50%, 0)';
        popup.style.background = '#fff';
        popup.style.border = '2px solid #0078d7';
        popup.style.borderRadius = '8px';
        popup.style.boxShadow = '0 2px 16px rgba(0,0,0,0.2)';
        popup.style.zIndex = '99999';
        popup.style.padding = '24px 32px 16px 32px';
        popup.style.minWidth = '320px';
        popup.innerHTML = `
            <div style="font-size:18px;font-weight:bold;margin-bottom:12px;">一键评教助手</div>
            <div style="margin-bottom:10px;">评语:</div>
            <textarea id="auto-eval-comment" style="width:100%;height:60px;resize:vertical;">老师讲课认真,教学效果优秀,受益匪浅!</textarea>
            <div style="margin-top:16px;text-align:right;">
                <button id="auto-eval-btn" style="background:#0078d7;color:#fff;border:none;padding:8px 20px;border-radius:4px;font-size:16px;cursor:pointer;">一键评教</button>
                <button id="auto-eval-close" style="margin-left:10px;background:#eee;color:#333;border:none;padding:8px 20px;border-radius:4px;font-size:16px;cursor:pointer;">关闭</button>
            </div>
        `;
        document.body.appendChild(popup);
        // 绑定按钮事件
        document.getElementById('auto-eval-btn').onclick = function() {
            let comment = document.getElementById('auto-eval-comment').value;
            autoSelectExcellent();
            autoFillComment(comment);
            autoSubmit();
        };
        document.getElementById('auto-eval-close').onclick = function() {
            popup.remove();
        };
        // 标记弹窗已显示
        popupShown = true;
    }

    // 页面加载后自动弹出弹窗
    window.onload = function() {
        createPopup();
    };

    // 兼容直接运行
    if (document.readyState === 'complete' || document.readyState === 'interactive') {
        setTimeout(createPopup, 500);
    } else {
        window.addEventListener('DOMContentLoaded', createPopup);
    }
})();