Updated Quizlet Micromatch bot

Win micromatch in less than a second! This is based off of the original script by Danielv123, but basically all of the code has been replaced by my own

目前為 2019-09-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Updated Quizlet Micromatch bot
// @namespace    BenjaminHinchliff
// @version      0.2
// @description  Win micromatch in less than a second! This is based off of the original script by Danielv123, but basically all of the code has been replaced by my own
// @author       You
// @match        https://quizlet.com/*/match*
// @grant        none
// ==/UserScript==

// a little function to allow the program to click on elements
function clickEvent(element) {
    let eve = new CustomEvent("pointerdown", { bubbles: true });
    element.dispatchEvent(eve);
}

let rawTerms = Quizlet.matchModeData.terms;
let terms = {};
// get terms from Quizlet object (a global variable because I only want to retrive it once)
for (let i = 0; i < rawTerms.length; i++) {
    terms[rawTerms[i].word] = rawTerms[i].definition;
}

// wait until user presses start
document.onclick = ()=>{
    // get all the nodes and store them in a variable to avoid re-querying nodes (I know it's live shut up)
    let nodes = document.querySelector(".MatchModeQuestionGridBoard-tiles").childNodes;
    for (let i = 0; i < nodes.length; i++) {
        // get term
        let search = nodes[i].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerHTML;
        // match with definition
        let dictionaryResult = terms[search];
        if(dictionaryResult) {
            // find term in nodes and send click events
            for (let j = 0; j < nodes.length; j++) {
                if (nodes[j].innerHTML.includes(dictionaryResult)) {
                    setTimeout(() => {
                        clickEvent(nodes[i].childNodes[0]);
                        clickEvent(nodes[j].childNodes[0]);
                    },
                    i * 50); // this is only here because quizlet doesn't allow score below 0.5 seconds and this roughly comes out to 0.5
                }
            }
        }
    }
};