Greasy Fork

Greasy Fork is available in English.

国科大人文讲座抢课脚本

用户可在网页输入框设置课程名称,自动点击对应“报名”按钮

当前为 2025-04-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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
// ==/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 = `
      <h4 style="margin:0 0 10px 0; color:#333;">抢课设置</h4>
      <input
        type="text"
        id="lectureNameInput"
        placeholder="输入课程名称(如:M1055)"
        style="width: 200px; padding: 5px; margin-bottom: 10px;"
        value="${savedName}"
      >
      <button
        id="saveSettingsBtn"
        style="padding: 5px 15px; background: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer;"
      >保存并开始</button>
      <div id="statusMsg" style="margin-top:10px; color:#666; font-size:12px;"></div>
    `;

    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();

})();