Ssieth / FunTrivia Answer Bot

// ==UserScript==
// @name         FunTrivia Answer Bot
// @namespace    http://gaxx.co.uk/funTrviaBot
// @version      1.0
// @description  Select a random answer or educated guess to each question
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @author       Gaxx
// @include      http://www.funtrivia.com/private/play.cfm
// @include      http://www.funtrivia.com/private/play.cfm?tid=86650
// @include      https://www.funtrivia.com/private/play.cfm
// @include      https://www.funtrivia.com/private/play.cfm?tid=86650
// @grant       GM_xmlhttpRequest
// @connect     api.cognitive.microsoft.com
// @license      MIT
// ==/UserScript==

(function() {
    var autoSubmit = true;
    var guessCount = 0;

    function guessFunc(questionNo, guessNo, guess) {
        console.log("Question #" + questionNo + " = Answer #" + guessNo + ": " + guess);
        var $t = $('table[style="border:1px #336600 solid;"]:eq(' + questionNo + ')');
        $t.find("b").css("color","darkgreen");
        $t.find("input[type='radio']:eq(" + guessNo + ")").trigger("click");
    }

    function getGuess(question, aryAnswers, questionNo) {
        // statr all answers out as 0 weighted
        var answerWeights = Array.apply(null, Array(aryAnswers.length)).map(function () { return 0});
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://api.cognitive.microsoft.com/bing/v7.0/search?q=" + question,
            headers: {
                "Ocp-Apim-Subscription-Key": "8488571ecef3408898c1c7046a903317"
            },
            onload: function(response) {
                var objResponse = JSON.parse(response.response);
                console.log("#" + questionNo + ": " + question);
                var aryPages = objResponse.webPages.value;
                for (var i = 0; i < aryPages.length; i++) {
                    for (var j = 0; j < answerWeights.length; j++) {
                        var myRe = new RegExp(aryAnswers[j], 'gi');
                        if (aryPages[i].snippet.match(myRe)) {
                            answerWeights[j] += (aryPages[i].snippet.match(myRe).length);
                        }
                    }
                }
                var intMax = answerWeights[0];
                var maxPos = 0;
                for (var k = 1; k < answerWeights.length; k++) {
                    if (answerWeights[k] > intMax) {
                        maxPos = k;
                        intMax = answerWeights[k];
                    }
                }
                console.log(answerWeights);
                if (intMax > 0) {
                    guessFunc(questionNo,maxPos,aryAnswers[maxPos]);
                } else {
                    if (question.indexOf("\"") > -1) {
                        console.log(questionNo + " trying quoted");
                        guessCount++;
                        setTimeout( () => {
                            getGuess(question.split('"')[1],aryAnswers,questionNo);
                        }, 4000);
                    } else if (question.indexOf("'") > -1) {
                        console.log(questionNo + " trying single-quoted");
                        guessCount++;
                        setTimeout( () => {
                            getGuess(question.split("'")[1],aryAnswers,questionNo);
                        }, 4000);
                    }
                }
                guessCount--;
                if (autoSubmit && guessCount === 0) {
                    $("input[type='submit']").trigger("click");
                }
            }
        });
    }

    console.log("== FunTrivia Answer Ranomizer v" + GM_info.script.version + " ==");
    $('table[style="border:1px #336600 solid;"]').each(function( index ) {
        var $list = $( this );
        var $ans = $list.find('input[type="radio"]');

        if ($ans.length > 1) {
            var $sel = $list.find("input[type='radio']:eq(" + Math.floor(Math.random() * $ans.length) + ")").trigger("click");
            var q = $ans.parent().parent().parent().parent().parent().parent().find("b").text();
            guessCount++;
            setTimeout(
                () => {
                    getGuess(q,[$ans[0].value,$ans[1].value,$ans[2].value,$ans[3].value],index)
                },
                index * 700
            );

        }
    });
    window.scrollTo(0,0);
    console.log("/= FunTrivia Answer Ranomizer v" + GM_info.script.version + " =/");
})();