// ==UserScript== // @name 重邮学评教助手 // @namespace cqupt-evaluation-helper // @version 1.0.1 // @description 教务在线入口、每项少一星、课程评价批量补齐、局部倒计时加速 // @homepageURL https://github.com/DTX-QwQ/cqupt-evaluation-helper // @supportURL https://github.com/DTX-QwQ/cqupt-evaluation-helper/issues // @downloadURL https://raw.githubusercontent.com/DTX-QwQ/cqupt-evaluation-helper/main/cqupt-evaluation.user.js // @updateURL https://raw.githubusercontent.com/DTX-QwQ/cqupt-evaluation-helper/main/cqupt-evaluation.user.js // @match https://jw.cqupt.edu.cn/* // @match http://jw.cqupt.edu.cn/* // @match http://jwzxm.cqupt.edu.cn/mjwzx2022/xpj2025/html/* // @match https://jwzxm.cqupt.edu.cn/mjwzx2022/xpj2025/html/* // @run-at document-start // @grant none // @sandbox raw // @inject-into page // @noframes // ==/UserScript== (() => { 'use strict'; const URL = 'http://jwzxm.cqupt.edu.cn/mjwzx2022/xpj2025/html/index.php#/student/course'; const KEY = 'cqupt-eval-options-v1'; const onPortal = location.hostname === 'jw.cqupt.edu.cn'; let options; try { options = JSON.parse(localStorage.getItem(KEY) || '{}'); } catch { options = {}; } options = { target: 2, fast: true, memo: '', ...options }; const nativeInterval = window.setInterval.bind(window); // 只识别已核对过的 EvaluationPopup 回调,不改变系统时间、其他计时器或网络请求。 if (!onPortal) window.setInterval = function (callback, delay, ...args) { const source = typeof callback === 'function' ? Function.prototype.toString.call(callback) : ''; const countdown = /^\(\)=>\{(\w+)\.value--,\1\.value<=0&&clearInterval\(\w+\.value\)\}$/.test(source); return nativeInterval(callback, options.fast && Number(delay) === 1000 && countdown ? 10 : delay, ...args); }; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); const visible = el => !!el && el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden'; const all = (selector, root = document) => [...root.querySelectorAll(selector)].filter(visible); const popup = () => all('.van-popup').find(el => el.querySelector('.popup-course-name') && el.querySelector('.submit-btn')); let running = false, filling = false, panel, status, start, stop, target, fast, memo; const say = text => { status.textContent = text; }; function save() { options.target = Number(target.value); options.fast = fast.checked; options.memo = memo.value.trim(); localStorage.setItem(KEY, JSON.stringify(options)); } function ensureRunning() { if (!running) throw new Error('已停止。已发送的提交请求仍可能完成,请检查次数。'); } const handledMessages = new WeakMap(); class WeeklyLimitError extends Error {} function isWeeklyLimit(text) { return /(?:一周|每周|本周|这周|同一周|7\s*[天日]|七[天日]|一星期)/.test(text) && /(?:只能|仅能|仅限|限评|一次|1\s*次|已.*评|不[能可]|无法|重复|限制|下周)/.test(text); } function checkMessages() { const messages = all('.el-message--error, .el-message--warning, .van-toast, .van-dialog__message') .map(el => ({ el, text: el.textContent.trim() })) .filter(({ el, text }) => text && handledMessages.get(el) !== text); // 优先保留真正的异常;普通提示和提交确认文案不作为错误。 const error = messages.find(({ el, text }) => !isWeeklyLimit(text) && el.matches('.el-message--error, .el-message--warning')); if (error) throw new Error(error.text); const weekly = messages.find(({ text }) => isWeeklyLimit(text)); if (weekly) { for (const { el, text } of messages) if (isWeeklyLimit(text)) handledMessages.set(el, text); throw new WeeklyLimitError(weekly.text); } } async function waitFor(check, timeout, active = true) { const until = Date.now() + timeout; while (Date.now() < until) { if (active) ensureRunning(); checkMessages(); const result = check(); if (result) return result; await sleep(150); } throw new Error('等待超时,已暂停。请检查页面及评价次数后再开始。'); } function count(card) { const match = card.textContent.match(/评价总次数[::]\s*(\d+)\s*次/); return match ? Number(match[1]) : null; } function cardKey(card) { return [...document.querySelectorAll('.course-card')].indexOf(card); } function currentCard(index, name) { const card = document.querySelectorAll('.course-card')[index]; if (!card || card.querySelector('.course-name')?.textContent !== name) { throw new Error('课程列表顺序发生变化,请检查后重新开始。'); } return card; } async function fill(form) { if (filling) throw new Error('正在填写,请稍候。'); filling = true; try { const groups = all('.van-rate', form); if (!groups.length) throw new Error('没有找到评分项,请等待表单加载。'); for (const group of groups) { if (!visible(form) || !form.isConnected) throw new Error('评价表单已关闭。'); if (group.getAttribute('aria-readonly') === 'true' || group.getAttribute('aria-disabled') === 'true') { throw new Error('当前是只读评价记录。'); } const stars = [...group.querySelectorAll('.van-rate__item')]; if (stars.length < 2) throw new Error('评分控件结构不符合预期。'); const score = stars.length - 1; stars[score - 1].click(); await sleep(30); if (group.querySelectorAll('[aria-checked="true"]').length !== score) { throw new Error('评分未生效,已停止。'); } } const field = form.querySelector('textarea'); if (field && options.memo) { Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set.call(field, options.memo); field.dispatchEvent(new Event('input', { bubbles: true })); await sleep(50); } return groups.length; } finally { filling = false; } } async function run() { if (running) return; save(); if (!location.hash.startsWith('#/student/course')) return say('请先切换到课程评价。'); if (popup()) return say('请先关闭当前表单;也可以点击“仅填写当前表单”。'); if (!all('.course-card').length) return say('没有找到课程,请确认校园网及教务登录状态。'); running = true; start.disabled = true; target.disabled = fast.disabled = memo.disabled = true; let submitted = 0; const skipped = []; const skipSummary = () => skipped.length ? `\n本周跳过 ${skipped.length} 门:\n${skipped.join('\n')}` : ''; try { // 在开始时固定课程索引;同名课与不同教师不会合并。 const tasks = all('.course-card').map(card => ({ index: cardKey(card), name: card.querySelector('.course-name')?.textContent, label: [card.querySelector('.course-name')?.textContent, card.querySelector('.course-teacher')?.textContent].filter(Boolean).join(' / ') })); for (const task of tasks) { ensureRunning(); try { let card = currentCard(task.index, task.name); let before = count(card); if (before === null) throw new Error('无法读取课程评价次数。'); while (before < options.target) { ensureRunning(); say(`${task.name}:正在填写第 ${before + 1} 次评价…`); const button = card.querySelector('.new-eval-btn'); if (!button || button.disabled) throw new Error('新评价按钮不可用。'); button.click(); const form = await waitFor(() => { const p = popup(); return p && all('.van-rate', p).length && p; }, 15000); await fill(form); ensureRunning(); say(`${task.name}:等待提交按钮(局部加速${options.fast ? '已开启' : '已关闭'})…`); const submit = await waitFor(() => { if (!visible(form)) throw new Error('表单被关闭,已暂停。'); const b = form.querySelector('.submit-btn'); return b && !b.disabled && b; }, 75000); ensureRunning(); submit.click(); const confirm = await waitFor(() => all('.van-dialog').find(el => el.textContent.includes('确认提交')), 5000); const confirmButton = confirm.querySelector('.van-dialog__confirm'); if (!confirmButton || confirmButton.disabled) throw new Error('无法识别确认按钮。'); ensureRunning(); confirmButton.click(); say(`${task.name}:已发送,正在核对次数…`); // 弹窗关闭不是成功证据;只在服务器刷新后的次数确实增加时继续。 await waitFor(() => count(currentCard(task.index, task.name)) > before, 30000); submitted++; card = currentCard(task.index, task.name); before = count(card); await sleep(500); // 每门课每周仅一次;本轮成功后不立即尝试第二次。 if (before < options.target) skipped.push(`${task.label}:本轮已评价一次,剩余次数下周继续。`); break; } } catch (error) { if (!(error instanceof WeeklyLimitError)) throw error; skipped.push(`${task.label}:${error.message}`); say(`已跳过 ${task.label}(本周限制),继续其他课程…`); // 限制可能在加载表单或提交时触发,先收起残留弹窗。 for (const dialog of all('.van-dialog')) { if (isWeeklyLimit(dialog.textContent)) dialog.querySelector('.van-dialog__confirm')?.click(); } const form = popup(); if (form) { const close = form.querySelector('.van-popup__close-icon'); if (!close) throw new Error('受限课程的表单无法关闭,请手动关闭后重新开始。'); close.click(); await waitFor(() => !popup(), 5000); } } } say(skipped.length ? `本轮处理完成:成功提交 ${submitted} 次。${skipSummary()}` : `已完成:本轮提交 ${submitted} 次,所有课程均达到 ${options.target} 次。`); } catch (error) { say(`已暂停(本轮成功 ${submitted} 次):${error.message}${skipSummary()}`); } finally { running = false; start.disabled = false; target.disabled = fast.disabled = memo.disabled = false; } } function mount() { if (document.getElementById('cqupt-eval-helper')) return; const style = document.createElement('style'); style.textContent = `#cqupt-eval-helper{position:fixed;right:16px;top:16px;z-index:2147483000;width:270px;box-sizing:border-box;padding:14px;background:#fff;color:#21372c;border:1px solid #b7cdbf;border-radius:12px;box-shadow:0 5px 24px #0002;font:14px/1.6 system-ui,sans-serif}#cqupt-eval-helper *{box-sizing:border-box}#cqupt-eval-helper button,#cqupt-eval-helper a{cursor:pointer;margin:5px 5px 5px 0;padding:6px 10px;border:0;border-radius:6px;background:#006633;color:white;text-decoration:none;display:inline-block}#cqupt-eval-helper button:disabled{opacity:.5;cursor:wait}#cqupt-eval-helper textarea{width:100%;height:56px;margin-top:6px}#cqupt-eval-helper p{margin:7px 0;white-space:pre-wrap}#cqupt-eval-helper summary{cursor:pointer;font-weight:700}#cqupt-eval-helper label{display:block;margin:6px 0}@media(max-width:700px){#cqupt-eval-helper{top:6px;right:6px;width:240px;max-height:70vh;overflow:auto}}`; document.head.append(style); panel = document.createElement('details'); panel.id = 'cqupt-eval-helper'; panel.open = true; const summary = document.createElement('summary'); summary.textContent = '重邮学评教助手(点此折叠)'; panel.append(summary); const link = document.createElement('a'); link.href = URL; link.textContent = '进入学评教'; link.target = '_blank'; link.rel = 'noopener'; panel.append(link); document.body.append(panel); if (onPortal) { const note = document.createElement('p'); note.textContent = '先登录教务在线,再打开学评教。需要连接校园网。'; panel.append(note); return; } const controls = document.createElement('div'); controls.innerHTML = '

每项默认少一颗星(课程 9/10)。

'; panel.append(controls); target = controls.querySelector('select'); target.value = String(options.target === 1 ? 1 : 2); fast = controls.querySelector('input'); fast.checked = !!options.fast; memo = controls.querySelector('textarea'); memo.value = options.memo; controls.addEventListener('change', save); function button(text, handler) { const b = document.createElement('button'); b.textContent = text; b.addEventListener('click', handler); panel.append(b); return b; } button('仅填写当前表单', async () => { if (running || filling) return; save(); try { const form = popup(); if (!form) throw new Error('请先打开“新评价”。'); say(`已填写 ${await fill(form)} 项,请核对后提交。`); } catch (e) { say(e.message); } }); start = button('开始自动评价', run); stop = button('停止', () => { running = false; say('正在停止;已发送的提交仍可能完成。'); }); status = document.createElement('p'); status.setAttribute('role', 'status'); status.textContent = '点击开始后将自动填写、确认并提交课程评价。可随时停止。'; panel.append(status); } if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', mount, { once: true }); else mount(); })();