anon12 / DrillsterTestMaker

// ==UserScript==
// @name         DrillsterTestMaker
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @licence      MIT
// @author       Anon
// @match        https://www.drillster.com/*
// @grant        unsafeWindow
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==

var Dict = {}

function doCopy(c){
    let all = $("p");
    all.each((i,v) => {
        let current = $(v).text();
        let togo = Dict[current];
        if (togo){
            let now = $("textarea").val();
            let newText = c == "Enter" ? togo : togo.slice(0,now.length+1);
            $("textarea").val(newText);
            window.setTimeout(() => $("textarea").val(newText),0)
        }
    });
}




function singleDrill(id){

    let myDict = {}

    let i = 30;

    function doAnswer(q){
        let asked = q.question.ask.term.value
        let ref = q.question.reference
        console.log(asked)

        function recieveAnswer(a){
            //console.log(a)
            i-=1;

            if (a.evaluation.result !== "CORRECT"){
                let answ = a.evaluation.termEvaluations[1].value
                myDict[asked] = answ
                Dict[asked] = answ
                console.log(asked + " = " + answ);
                i = 0;
            } else {
                if (i > 0) {
                    console.log(i)
                    $.get("https://www.drillster.com/api/2.1/question/"+id,doAnswer);
                }
            }
        }

        let myans = myDict[asked] || "a";
        $.put("https://www.drillster.com/api/2.1/answer/"+id,{reference: ref,answer: myans},recieveAnswer);

    }

    function getResponses(r){
        console.log(r)
        r.easy.forEach(v => {
            let q = v.known.alternatives[0].value
            let a = v.unknown.alternatives[0].value

            console.log(q+" = "+a)
            Dict[q] = a
            myDict[q] = a
        })

        r.difficult.forEach(v => {
            let q = v.known.alternatives[0].value
            let a = v.unknown.alternatives[0].value

            console.log(q+" = "+a)
            Dict[q] = a
            myDict[q] = a
        })
        $.get("https://www.drillster.com/api/2.1/question/"+id,doAnswer);
    }

    $.get(`https://www.drillster.com/api/2.1/drillable/${id}/easy-and-difficult-entries`,getResponses);
}



function setupKeyPress(){
    $("body").on("keypress",v => {
            console.log("gogo");
            if (unsafeWindow.fakeType ||v.key == "Enter" /*(Set to True for faketyping)*/){
                doCopy(v.key);
            }
        })
}

function getDrills(){
    let testID = location.href.match("#/(.*)\\?")[1]

    $.get("https://www.drillster.com/api/2.1/test/"+testID,v => {for (let drill of v.drills){singleDrill(drill)};})

    unsafeWindow.Dict = Dict

}




















(function() {
    'use strict';


    console.log("ACTIVE")

    $.put = function(url, data, callback, type){
        if ( $.isFunction(data) ){
            type = type || callback,
                callback = data,
                data = {}
        }
        return $.ajax({
            url: url,
            type: 'PUT',
            success: callback,
            data: data,
            contentType: type
        });
    }

    window.setTimeout(getDrills,100);
    window.setTimeout(setupKeyPress,1000);

    unsafeWindow.fakeType = true

    // Your code here...
})();