Greasy Fork

ZyBooks auto

Automatically do ZyBooks

< 脚本ZyBooks auto的反馈

评价:好评 - 脚本运行良好

§
发表于:2024-12-08

Great script! Here's some code to automatically select the correct option from the multiple choice options.
You might want to improve on it a little bit, it was something that I whipped up real quick while trying to rush through a lot of textbook assignments:

function selectAnswersUntilCorrect() {
// Get all question containers with the class 'question-set-question multiple-choice-question'
const questionDivs = document.querySelectorAll('.question-set-question.multiple-choice-question');

// If no question divs are found
if (questionDivs.length === 0) {
console.log("No questions found.");
return;
}

// Iterate over each question div
questionDivs.forEach(questionDiv => {
// Get all the options (radio or checkbox inputs) for this specific question
const options = questionDiv.querySelectorAll('input[type="radio"], input[type="checkbox"]');

// Check if there are options to select
if (options.length === 0) {
console.log("No options found in a question.");
return;
}

// Check if the correct answer is already selected (looking for "Correct" feedback)
const resultText = questionDiv.querySelector('.zb-explanation.has-explanation.correct');
if (resultText && resultText.textContent.includes("Correct")) {
console.log(`Correct answer already selected for question ${Array.from(questionDivs).indexOf(questionDiv) + 1}!`);
return; // Move to the next question, no need to select any more options
}

// Loop over each option in the question and select one if needed
let optionIndex = 0;
function selectOption() {
// Ensure we are within the available options
if (optionIndex < options.length) {
const option = options[optionIndex];

// If the option is not already selected, click it
if (!option.checked) {
console.log(`Selecting option ${optionIndex + 1} for question ${Array.from(questionDivs).indexOf(questionDiv) + 1}`);
option.click();

// Delay to allow for the feedback to be shown
setTimeout(() => {
// Check if the "Correct" text is displayed for this question
const updatedResultText = questionDiv.querySelector('.zb-explanation.has-explanation.correct');

// If "Correct" is found, log success and stop for this question
if (updatedResultText && updatedResultText.textContent.includes("Correct")) {
console.log(`Correct answer selected for question ${Array.from(questionDivs).indexOf(questionDiv) + 1}!`);
return; // Stop for this question, move to the next question
} else {
// If the answer is incorrect, move to the next option
optionIndex++;
selectOption(); // Recursively select the next option
}
}, 1000); // Adjust the delay based on your page's response time
} else {
// If this option is already selected, move to the next option
optionIndex++;
selectOption(); // Recursively select the next option
}
} else {
// If no correct option found, log that this question couldn't be answered
console.log(`No correct answer found for question ${Array.from(questionDivs).indexOf(questionDiv) + 1}`);
}
}

// Start selecting options for this question if needed
selectOption();
});
}

// Call the function
selectAnswersUntilCorrect();

孤傲小黑作者
§
发表于:2025-01-24

Sorry for the delayed response. Thank you for providing the code. I will consider adding it to the script.😊

发表回复

登录以发表回复。