Last active
August 24, 2019 05:19
-
-
Save Richienb/cdea080af076da0c0959bc3d15d59608 to your computer and use it in GitHub Desktop.
Live Mathletics Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-disable require-jsdoc */ | |
| (() => { | |
| function injectScript(src) { | |
| return new Promise((resolve, reject) => { | |
| const script = document.createElement("script") | |
| script.async = true | |
| script.src = src | |
| script.addEventListener("load", resolve) | |
| script.addEventListener("error", () => reject(new Error("Error loading script."))) | |
| script.addEventListener("abort", () => reject(new Error("Script loading aborted."))) | |
| document.head.appendChild(script) | |
| }) | |
| } | |
| function start() { | |
| const question = $("body > div.stage > ui-view > div > div.gameQuestionSection > div.livemathLightBlueBG.gameQuestionBlock > div.question-input-div.ng-scope > aligned-question > div > form > div") | |
| const answer = $("body > div.stage > ui-view > div > div.gameQuestionSection > div.livemathLightBlueBG.gameQuestionBlock > div.question-input-div.ng-scope > aligned-question > div > form > div > input") | |
| setInterval(() => { | |
| let q = question.text() | |
| const blankMiddle = q.trim().match(/(.+) \+ = (.+)/) | |
| if (blankMiddle[1]) answer.val(math.eval(`${blankMiddle[2]} - ${blankMiddle[1]}`)) | |
| else { | |
| q = q.replace("=", "") | |
| q = q.replace("÷", "/") | |
| q = q.replace("×", "*") | |
| q = q.replace(/Half of (\d+)/, "$1 / 2") | |
| answer.val(math.eval(q)) | |
| } | |
| const event = document.createEvent("HTMLEvents") | |
| event.initEvent("change", true, false) | |
| answer.get(0).dispatchEvent(event) | |
| const e = jQuery.Event("keypress") | |
| e.which = 13 | |
| e.keyCode = 13 | |
| question.trigger(e) | |
| }, 100) | |
| } | |
| let numLoaded = 0 | |
| function scriptLoaded() { | |
| numLoaded++ | |
| if (numLoaded === 2) start() | |
| } | |
| try { | |
| math | |
| scriptLoaded() | |
| } catch (e) { | |
| injectScript("https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.10.3/math.min.js").then(scriptLoaded) | |
| } | |
| if (!$.Event) injectScript("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js").then(scriptLoaded) | |
| else scriptLoaded() | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment