Khan Academy Problem Solver

Here is a Khan Academy Solver!

目前為 2021-11-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Khan Academy Problem Solver
// @version      2.0
// @description  Here is a Khan Academy Solver!
// @author       Logzilla6
// @match        https://www.khanacademy.org/*
// @grant        none
// @namespace https://greasyfork.org/users/783447
// ==/UserScript==

(function () {
  let overlayHTML = ` <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<div id="box">
<button class="ou" id="accordian">Toggle</button>
    <div class="ou" id="box2">
        <p style="color:white;"> KhanHack </p>
        <section><label>Answer: [<label id="ans1">...</label>]</label></section>
        <section><label> </label></section>
        <section><label>Next And Last Answer: [<label id="ans2"></label>]</label></section>


    </div>
    
</div>
<style>
#box {
    z-index: 1090;
    position: fixed;
    top: 0;
    right: 0;}
#box2 {
    padding: 15px;
    margin-bottom: 5px;
    display: none};
section {
    display: flex;
    justify-content: space-between;margin:5px;}
.ou {
    background-color: #072357;
    letter-spacing: 2px;

    font-weight: none;
    font-size: 11px;
    font-family: 'Roboto', sans-serif;
    color:white;}
p { text-align: center;border-bottom:1px solid white;}
#ytlink { border:0;}
#ytlink a{ color:lime;}
#accordian {
    width: 100%;
    border:0;
    cursor: pointer;}
label { font-weight: bold;}
input {
    margin-top: auto;
    margin-bottom: auto;
    transform: scale(1.3);}
input:hover { cursor: pointer;}
input:focus { box-shadow: 0 0 10px #9ecaed;}
input[type=checkbox] { transform: scale(2.2);outline=none;}
input[type=radio] { border-top: auto;}
input[type=color] { width: 50px;}
.checked {
    height: 20px;
    width: 20px;
    box-shadow: -1px -2px 5px gray;
    border: 3px solid gray;
    background-color: #17e34d;
    margin-top: auto;
    margin-bottom: auto;
    box-shadow: 2 2 2px #a0a0a0;
    width:20px;
    transform: scale(1.5);}
.notchecked { background-color: red;}





._eqjto6h {
    -webkit-box-pack: center !important;
    -ms-flex-pack: center !important;
    -webkit-box-align: center !important;
    -ms-flex-align: center !important;
    display: -webkit-box !important;
    display: -moz-box !important;
    display: -ms-flexbox !important;
    display: -webkit-flex !important;
    display: flex !important;
    -webkit-align-items: center !important;
    align-items: center !important;
    -webkit-justify-content: center !important;
    justify-content: center !important;
    position: absolute !important;
    max-width: 5% !important;
    width: 48
px
 !important;
    top: 0
px
 !important;
    height: 0% !important;
    color: white !important;
    -webkit-user-select: none !important;
    -moz-user-select: none !important;
    -ms-user-select: none !important;
    user-select: none !important;
    -webkit-tap-highlight-color: transparent !important;
    background: none !important;
    border: none !important;
    right: 0
px
 !important;
}


</style>
`


function get(x)            { return document.getElementById(x); }

let overlay             = document.createElement("div");
    overlay.innerHTML   = overlayHTML;
    document.body.appendChild(overlay);

let acc                 = get("accordian"),
    aimbot              = get("aimbot");

acc.onclick = function() {
    let panel = get("box2");
    if (panel.style.display == "grid") panel.style.display = "none";
    else { panel.style.display = "grid"; }
}



    'use strict';
    window.loaded = false;
    alert("Look at top right for 'Toggle'")
    class Answer {
        constructor(answer, type) {
            this.body = answer;
            this.type = type;
        }

        get isMultiChoice() {
            return this.type == "multiple_choice";
        }

        get isFreeResponse() {
            return this.type == "free_response";
        }

        get isExpression() {
            return this.type == "expression";
        }

        get isDropdown() {
            return this.type == "dropdown";
        }

        log() {
            const answer = this.body;
            const style = "color: coral; -webkit-text-stroke: .5px black; font-size:24px; font-weight:bold;";

            answer.map(ans => {
                if (typeof ans == "string") {
                    if (ans.includes("web+graphie")) {
                        this.body[this.body.indexOf(ans)] = "";
                        this.printImage(ans);
                    } else {
                        answer[answer.indexOf(ans)] = ans.replaceAll("$", "");
                    }
                }
            });

            const text = answer.join("\n");
            if (text) {
                console.log(`${text.trim()}`, style);
            }
        }

        printImage(ans) {
            const url = ans.replace("![](web+graphie", "https").replace(")", ".svg");
            const image = new Image();

            image.src = url;
            image.onload = () => {
                const imageStyle = [
                    'font-size: 1px;',
                    'line-height: ', this.height % 2, 'px;',
                    'padding: ', this.height * .5, 'px ', this.width * .5, 'px;',
                    'background-size: ', this.width, 'px ', this.height, 'px;',
                    'background: url(', url, ');'
                ].join(' ');
                console.log('', imageStyle);
            };
        }
    }

    const originalFetch = window.fetch;
    window.fetch = function () {
        return originalFetch.apply(this, arguments).then((res) => {
            if (res.url.includes("/getAssessmentItem")) {
                const clone = res.clone();
                clone.json().then(json => {
                    let item, question;

                    try {
                        item = json.data.assessmentItem.item.itemData;
                        question = JSON.parse(item).question;
                    } catch {
                        let errorIteration = () => { return localStorage.getItem("error_iter") || 0; }
                        localStorage.setItem("error_iter", errorIteration() + 1);

                        if (errorIteration() < 4) {
                            return location.reload();
                        } else {
                            return alert("An error occurred");
                        }
                    }

                    if (!question) return;

                    Object.keys(question.widgets).map(widgetName => {
                        switch (widgetName.split(" ")[0]) {
                            case "numeric-input":
                                return freeResponseAnswerFrom(question).log();
                            case "radio":
                                return multipleChoiceAnswerFrom(question).log();
                            case "expression":
                                return expressionAnswerFrom(question).log();
                            case "dropdown":
                                return dropdownAnswerFrom(question).log();
                        }
                    });
                });
            }

            return res;
        })
    }

    function freeResponseAnswerFrom(question) {
        const answer = Object.values(question.widgets).map((widget) => {
            if (widget.options?.answers) {
                return widget.options.answers.map(answer => {
                    if (answer.status == "correct") {
                      //alert('freeresponse')
                       var ans1 = document.getElementById('ans1').innerHTML
                       var ans2 = document.getElementById('ans2').innerHTML
                      
                      document.getElementById('ans2').innerHTML = (answer.value)
                      
                       if (ans2 == "") {
                        document.getElementById('ans1').innerHTML = (answer.value)                        
                      }
                      else {
                        document.getElementById('ans1').innerHTML = (ans2)
                      }
                      
                      
                      
                      
                      
                      
                      
                      
                      
            
                    }
                 });
                 
            }
        }).flat().filter((val) => { return val !== undefined; });

        return new Answer(answer, "free_response");
    }

    function multipleChoiceAnswerFrom(question) {
        const answer = Object.values(question.widgets).map((widget) => {
            if (widget.options?.choices) {
                return widget.options.choices.map(choice => {
                    if (choice.correct) {
  						//alert('multichoice')
                      var ans1 = document.getElementById('ans1').innerHTML
                       var ans2 = document.getElementById('ans2').innerHTML
                      
                      document.getElementById('ans2').innerHTML = (choice.content)
                      
                       if (ans2 == "") {
                        document.getElementById('ans1').innerHTML = (choice.content)                        
                      }
                      else {
                        document.getElementById('ans1').innerHTML = (ans2)
                      }
                    }
                });
            }
        }).flat().filter((val) => { return val !== undefined; });


        return new Answer(answer, "multiple_choice");
    }

    function expressionAnswerFrom(question) {
        const answer = Object.values(question.widgets).map((widget) => {
            if (widget.options?.answerForms) {
                return widget.options.answerForms.map(answer => {
                    if (Object.values(answer).includes("correct")) {
                        //alert('expression')
                      var ans1 = document.getElementById('ans1').innerHTML
                       var ans2 = document.getElementById('ans2').innerHTML
                      
                      document.getElementById('ans2').innerHTML = (answer.value)
                      
                       if (ans2 == "") {
                        document.getElementById('ans1').innerHTML = (answer.value)                        
                      }
                      else {
                        document.getElementById('ans1').innerHTML = (ans2)
                      }
                    }
                });
            }
        }).flat();

        return new Answer(answer, "expression");
    }

    function dropdownAnswerFrom(question) {
        const answer = Object.values(question.widgets).map((widget) => {
            if (widget.options?.choices) {
                return widget.options.choices.map(choice => {
                    if (choice.correct) {
                        //alert('dropdown')
                      var ans1 = document.getElementById('ans1').innerHTML
                       var ans2 = document.getElementById('ans2').innerHTML
                      
                      document.getElementById('ans2').innerHTML = (choice.content)
                      
                       if (ans2 == "") {
                        document.getElementById('ans1').innerHTML = (choice.content)                        
                      }
                      else {
                        document.getElementById('ans1').innerHTML = (ans2)
                      }
                    }
                });
            }

        }).flat();

        return new Answer(answer, "dropdown");

    }
})();