Skip to content

Instantly share code, notes, and snippets.

@ShreyJ1729
Created February 23, 2024 00:29
Show Gist options
  • Save ShreyJ1729/f7e168f3359296567351d6df2a0c266a to your computer and use it in GitHub Desktop.
Save ShreyJ1729/f7e168f3359296567351d6df2a0c266a to your computer and use it in GitHub Desktop.
Arts 1301 Automation Script
const questionAnswerCache = {};
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
/* ------------------ PAGE LEVEL ACTIONS ------------------ */
function clickReadAboutConcept() {
document.getElementsByClassName("lr__action-label")[0].click();
}
function clickBackToQuestions() {
document.getElementsByClassName("button-bar-wrapper")[0].firstChild.click();
}
async function gotoConceptAndBack() {
clickReadAboutConcept();
await sleep(500);
clickBackToQuestions();
}
function clickNextQuestion() {
document.getElementsByClassName("next-button")[0].click();
}
/* ------------------ GET PAGE DATA ------------------ */
function getCorrectAnswer() {
return document.getElementsByClassName("correct-answer-container")[0]
.innerText;
}
function getPageState() {
const isUnanswered =
document.getElementsByClassName("correct-answer-container").length === 0;
if (isUnanswered) {
return "unanswered";
} else {
if (document.getElementsByClassName("lr-tray-header").length > 0) {
return "answered-now-review";
} else {
return "answered";
}
}
}
function getQuestionText(questionType) {
if (questionType == "mcq-select-all") {
return document.getElementsByClassName("multiple-select-component")[0]
.innerText;
} else if (questionType == "mcq") {
return document.getElementsByClassName("multiple-choice-component")[0]
.innerText;
} else if (questionType == "fill-in-blank") {
return document.getElementsByClassName("fitb-component")[0].innerText;
} else if (questionType == "true-false") {
return document.getElementsByClassName("true-false-component")[0].innerText;
}
}
function getQuestionType() {
const questionTypeHeader =
document.getElementsByClassName("probe-header")[0].innerText;
if (questionTypeHeader == "Fill in the Blank Question") {
return "fill-in-blank";
} else if (questionTypeHeader == "Multiple Select Question") {
return "mcq-select-all";
} else if (questionTypeHeader == "Multiple Choice Question") {
return "mcq";
} else if (questionTypeHeader == "True or False Question") {
return "true-false";
}
}
function isNextDisabled() {
const nextButton = document.getElementsByClassName("next-button")[0];
return nextButton.disabled;
}
/* ------------------ INTERACT WITH QUESTIONS ------------------ */
// gets all the mcq answers
function getMCQAnswersText() {
classname = "responses-container";
let answers = document.getElementsByClassName(classname)[0].innerText;
answers = answers.split("\n\n");
answers.shift();
return answers;
}
function clickMCQAnswerOfText(answerText) {
// get index of answer
const answers =
getQuestionType() == "true-false" ? ["True", "False"] : getMCQAnswersText();
const answerIndex = answers.indexOf(answerText);
let questionType = getQuestionType();
let classname =
questionType == "mcq-select-all"
? "mcms-fieldset"
: questionType == "true-false"
? "true-false-fieldset"
: "multiple-choice-fieldset";
document
.getElementsByClassName(classname)[0]
.children[answerIndex + 1].firstChild.firstChild.firstChild.click();
}
function typeInAnswer(answerText) {
inputbox = document.getElementsByClassName("input-container span-to-div")[0];
inputbox.firstChild.value = answerText;
inputbox.focus();
}
function hitAnswer() {
const button = document.getElementsByClassName(
"confidence-buttons-container"
)[1].firstChild;
button.disabled = false;
button.click();
console.log("Answered question");
}
/* ------------------ MAIN FUNCTION ------------------ */
async function singleRun() {
let questionType = getQuestionType();
let questionText = getQuestionText(questionType);
console.log("Question type is " + questionType);
console.log("Question text is " + questionText);
questionText = questionText.split("\n\n")[0];
let questionIsCached = questionAnswerCache.hasOwnProperty(questionText);
// if question was previously cached
if (questionIsCached) {
let answer = questionAnswerCache[questionText];
console.log(
"Question was cached - answering with cached answer of " + answer + ".."
);
if (questionType == "fill-in-blank") {
// its a string of the right answer
typeInAnswer(answer);
} else if (questionType == "mcq") {
// its a string of the right answer
clickMCQAnswerOfText(answer);
} else if (questionType == "mcq-select-all") {
// its an array of the right answers
for (let i = 0; i < answer.length; i++) {
clickMCQAnswerOfText(answer[i]);
}
} else if (questionType == "true-false") {
// its a string of the right answer
clickMCQAnswerOfText(answer);
}
console.log("Answered question with cached answer");
} else {
// if answer is not yet cached, give a random answer to cache it
console.log("Question not cached - giving random answer");
if (questionType == "fill-in-blank") {
typeInAnswer("fill-in-blank");
} else if (questionType == "mcq" || questionType == "mcq-select-all") {
let answersText = getMCQAnswersText()[0];
clickMCQAnswerOfText(answersText);
} else if (questionType == "true-false") {
clickMCQAnswerOfText("True");
}
}
// hit the answer button
await sleep(500);
hitAnswer();
await sleep(500);
// now that the question is answered, cache the correct answer and if reading concept needed, do that
let correctAnswer = getCorrectAnswer()
.replace("Correct Answer\n\n", "")
.replace("Correct Answer\n", "")
.trim();
console.log("Caching correct answer - " + correctAnswer);
if (
questionType == "fill-in-blank" ||
questionType == "mcq" ||
questionType == "true-false"
) {
questionAnswerCache[questionText] = correctAnswer;
} else if (questionType == "mcq-select-all") {
questionAnswerCache[questionText] = correctAnswer
.split("\n")
.filter((n) => n);
}
// if next button is disabled, we must go to concepts and back
if (isNextDisabled()) {
console.log("Next button is disabled - going to concepts and back");
await gotoConceptAndBack();
await sleep(500);
console.log("Going to next question");
clickNextQuestion();
} else {
console.log("Next button is not disabled - going to next question");
clickNextQuestion();
}
}
async function main() {
while (true) {
await singleRun();
await sleep(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment