lucidobservor / AutoSchool

// ==UserScript==
// @name         AutoSchool
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  Automatically complete Math/Literature problems in GooBoo
// @author       lucidobservor
// @match        https://html-classic.itch.zone/html/9100894/index.html
// @match        https://tendsty.github.io/gooboo/
// @match        https://tendsty.itch.io/gooboo
// @icon         https://tendsty.github.io/gooboo/favicon-32x32.png
// @license      MIT
// @updateURL    https://openuserjs.org/meta/lucidobservor/AutoSchool.meta.js
// @downloadURL  https://openuserjs.org/install/lucidobservor/AutoSchool.user.js
// @homepage     https://openuserjs.org/scripts/lucidobservor/AutoSchool
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Define button to enable automation
    var schoolButtonActive = -1;
    var schoolButtonName = "schoolActionButton"
    var schoolActionDiv = document.createElement('div');
    schoolActionDiv.id = 'schoolActionDiv';
    schoolActionDiv.innerHTML = `&nbsp&nbsp<button data-autoaction id="${schoolButtonName}" type="button" class="balloon-text-dynamic v-chip theme--dark v-size--small">AutoSchool OFF</button>`;
    document.getElementsByClassName("v-toolbar__content")[0].querySelector('.spacer').insertAdjacentElement("afterend", schoolActionDiv);

    var answer = ""
    function turnOff() {
        clearInterval(schoolButtonActive);
        schoolButtonActive = -1;
        answer = ""
        document.getElementById(schoolButtonName).innerHTML = "AutoSchool OFF";
    }

    function autoClicker() {
        var questionElement = document.getElementsByClassName("question-text")
        var answerElement = null
        var schoolType = -1

        if (questionElement.length === 0) {
            answer = ""
            return
        }

        if (document.getElementById('answer-input-math') != null) {
            schoolType = 0
            answerElement = document.getElementById('answer-input-math')
        } else if (document.getElementById('answer-input-history') != null) {
            return // history not supported
        } else if (document.getElementsByClassName("v-text-field__slot").length > 0) {
            schoolType = 1
            answerElement = document.getElementsByClassName("v-text-field__slot")[0].firstChild
        }

        var questionText = questionElement[0].innerText
        if (questionText.startsWith('√')) {
            questionText = questionText.replace('√', 'Math.sqrt(') + ')'
        }
        if (questionText.includes('^')) {
            questionText = 'Math.pow(' + questionText.replace('^', ',') + ')'
        }
        if (questionText.includes('\n')) {
            questionText = questionText.split('\n')[0]
        }

        try {
            if (answer === "") {
                if (schoolType === 0) {
                    answer = eval(questionText)
                } else if (schoolType === 1) {
                    answer = questionText
                }
            } else if (answerElement.value != answer) {
                answerElement.value = answer
                answerElement.dispatchEvent(new Event('input', {bubbles: true}))
                console.log(answerElement.value)
            } else {
                if (schoolType === 0) document.getElementsByClassName("ma-1 v-btn v-btn--is-elevated v-btn--has-bg v-size--default primary")[0].click()
                answer = ""
            }
        } catch (error) {
            turnOff()
        }
    }

    document.getElementById(schoolButtonName).onclick = function () {
        if (schoolButtonActive == -1) {
            schoolButtonActive = setInterval( autoClicker, 250);
            document.getElementById(schoolButtonName).innerHTML = "AutoSchool ON";
        }
        else {
            turnOff()
        }

    };
})();