// ==UserScript==
// @name 国科大人文讲座抢课脚本
// @namespace http://tampermonkey.net/
// @version 2025.04.22
// @description 用户可在网页输入框设置课程名称,自动点击对应“报名”按钮
// @author You
// @match https://xkcts.ucas.ac.cn:8443/subject/humanityLecture
// @icon https://www.google.com/s2/favicons?sz=64&domain=ai1.bar
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @license MIT
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// 创建输入框和保存按钮的UI
function createSettingsUI() {
const ui = document.createElement('div');
ui.style = `
position: fixed;
top: 20px;
right: 20px;
background: white;
padding: 15px;
border: 2px solid #4CAF50;
border-radius: 5px;
z-index: 9999;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
`;
const savedName = localStorage.getItem('grasemonkey_lecture_name') || '';
ui.innerHTML = `
抢课设置
`;
document.body.appendChild(ui);
// 保存按钮点击事件
document.getElementById('saveSettingsBtn').addEventListener('click', () => {
const lectureName = document.getElementById('lectureNameInput').value.trim();
if (lectureName) {
localStorage.setItem('grasemonkey_lecture_name', lectureName);
showStatus('设置已保存,开始抢课!', '#4CAF50');
} else {
showStatus('课程名称不能为空!', 'red');
}
});
}
// 显示操作状态
function showStatus(msg, color) {
const statusDiv = document.getElementById('statusMsg');
statusDiv.textContent = msg;
statusDiv.style.color = color;
setTimeout(() => statusDiv.textContent = '', 3000);
}
// 查找报名按钮的核心逻辑
function getEnrollmentButton() {
const targetName = localStorage.getItem('grasemonkey_lecture_name');
if (!targetName) {
console.log('未设置课程名称,请先在输入框中设置!');
return null;
}
const rows = document.querySelectorAll("tr");
for (const row of rows) {
if (row.textContent.includes(targetName)) {
const buttons = row.querySelectorAll("a, button");
for (const btn of buttons) {
if (btn.textContent.includes("报名")) {
return btn;
}
}
}
}
return null;
}
// 主逻辑:初始化UI并启动监控
function main() {
createSettingsUI();
setInterval(() => {
const enrollBtn = getEnrollmentButton();
if (enrollBtn && !enrollBtn.disabled) {
console.log(`检测到课程报名按钮,立即点击!时间: ${new Date().toLocaleString()}`);
enrollBtn.click();
setTimeout(() => location.reload(), 2000);
}
}, 60000); // 每60秒检查一次
}
// 启动脚本
main();
})();