Answer Grabber

Revel answers to questions on your Ximera homework

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Answer Grabber
// @namespace    http://tampermonkey.net/
// @version      2024-09-20
// @description  Revel answers to questions on your Ximera homework
// @author       PsychedelicPalimpsest
// @match        *://ximera.osu.edu/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=osu.edu
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    let lastInputCount = -1;

    const old_log = console.log;
    console.log = (arg) =>{
        old_log(arg);
        if (arg != "Recorded gradebook") return;
        let c = document.querySelectorAll("input[type=text]").length;
        if (c == lastInputCount) return;
        lastInputCount = c;


        document.head.innerHTML += "<style>.correct{background-color: green;}</style>";
        function stripr(str){
            let parcount = 0;
            for (let index in str){
                let c = str[index];
                if (c == "{")
                    parcount++;
                else if(c == "}")
                    parcount--;
                if (parcount == -1)
                    return str.substring(0, index);
            }
        }

        let texs = document.querySelectorAll("script[type='math/tex; mode=display']")
                .values()
                .toArray()
                .concat(
                    document.querySelectorAll("script[type='math/tex']")
                            .values()
                            .toArray()
                );
        for (let tex of texs){
            let answers = tex.innerText.matchAll(/\\answer/g).toArray();
            if (document.getElementById(tex.id + "-Frame") == undefined) continue;
            let boxes = document.getElementById(tex.id + "-Frame").querySelectorAll("input[type=text]")


            for (let i in answers){

                let astr = tex.innerText.substring(answers[i].index);
                let a = stripr(astr.substring(astr.match(/\{/).index+1));
                if (boxes[i] == undefined) break;
                boxes[i].value = a
            }
        }
    }


})();