Greasy Fork

温州大学SPOC自动答题

自动选择 SPOC 单选题的答案 只可以是单选题!! 因为没有做到多选题

目前为 2025-04-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         温州大学SPOC自动答题
// @namespace    hahahahaha
// @license MIT
// @version      1.01
// @description  自动选择 SPOC 单选题的答案 只可以是单选题!! 因为没有做到多选题
// @author       col
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
    'use strict';

    // 用于缓存从Ajax里抓到的题目数据
    let examDataCache = null;

    // 重写 jQuery.ajax 以拦截 getExamPaper 请求
    const hookAjax = () => {
        const $ = window.jQuery;
        if (!$ || !$.ajax) return;

        const originalAjax = $.ajax;
        $.ajax = function (opts) {
            const origSuccess = opts.success;
            opts.success = function (data, ...args) {
                try {
                    if (opts.url.includes("getExamPaper")) {
                        examDataCache = data;  // 缓存题目数据
                        console.log("[SPOC-Auto] Exam data captured.");
                    }
                } catch (e) {
                    console.error("[SPOC-Auto] Intercept error:", e);
                }
                return origSuccess && origSuccess.call(this, data, ...args);
            };
            return originalAjax.call(this, opts);
        };
    };

    // 自动选择单选题的答案
    const autoSelectAnswers = () => {
        if (!examDataCache) {
            alert("没有检测到试卷数据,请确保页面加载完成且试卷已经请求。");
            return;
        }

        const questions = examDataCache.paper.paperStruct;
        const quizIdOptionIdMap = new Map();

        questions.forEach(q => {
            const quizId = q.quiz.quizId;
            const optionId = q.quiz.quizResponses[0]?.optionId;
            if (quizId && optionId) {
                quizIdOptionIdMap.set(quizId, optionId);
            }
        });

        let count = 0;
        document.querySelectorAll(".practice-item").forEach(item => {
            const quizId = item.getAttribute("quiz_id");
            const targetOptionId = quizIdOptionIdMap.get(Number(quizId));

            if (targetOptionId) {
                const optionEl = item.querySelector(`.t-option[option_id="${targetOptionId}"] a.input-r`);
                if (optionEl) {
                    optionEl.click();
                    console.log(`[SPOC-Auto] Selected: quiz_id=${quizId}, option_id=${targetOptionId}`);
                    count++;
                }
            }
        });

        alert(`✅ 共选中了 ${count} 道单选题。`);
    };

    // 添加一个浮动按钮,点击后触发自动答题
    const createFloatingButton = () => {
        const btn = document.createElement("button");
        btn.innerText = "🧠 自动答题";
        btn.style.position = "fixed";
        btn.style.bottom = "20px";
        btn.style.right = "20px";
        btn.style.zIndex = "99999";
        btn.style.padding = "12px 20px";
        btn.style.background = "#28a745";
        btn.style.color = "#fff";
        btn.style.fontSize = "18px";
        btn.style.border = "none";
        btn.style.borderRadius = "6px";
        btn.style.cursor = "pointer";
        btn.onclick = autoSelectAnswers;
        document.body.appendChild(btn);
    };

    // 等待 DOM 和 jQuery 准备就绪
    const waitForReady = () => {
        const interval = setInterval(() => {
            if (window.jQuery) {
                hookAjax();
                clearInterval(interval);
            }
        }, 200);
    };

    // DOM 加载完成后插入按钮
    window.addEventListener("load", () => {
        createFloatingButton();
    });

    waitForReady();
})();