KhanHack

Khan Academy Answer Hack

< Feedback on KhanHack

Review: Good - script works

§
Posted: 19 Juli 2024

use this it may not work with some questions but you have to reload it when you lose track of which question it was because it shows the current answer and the next answer and it keeps doing that so you may have to reload it to find the right question also it may bug like on 3 answer type ins because it repeats it 3 times. so heres the code. and by the way i dont care how you use it.:


// ==UserScript==
// @name KhanHack
// @version 3.9
// @description Here is a Khan Academy Solver!
// @author Logzilla6
// @match https://www.khanacademy.org/*
// @grant none
// @namespace https://greasyfork.org/users/783447
// ==/UserScript==

(function () {
const overlayHTML = `


Toggle

KhanHack

Reset Answer List

-Answers-

 
 
M also toggles Menu

`;

function get(x) { return document.getElementById(x); }

function resetAnswers() {
document.getElementById('ansBreak').innerHTML = ''; // Clear the answer list
curAns = 1; // Reset the answer counter
console.log('Answer list has been reset.');
}

function handleFetchError(error) {
console.error('An error occurred:', error);
alert('An error occurred. Please check the console for details.');
}

let overlay = document.createElement("div");
overlay.innerHTML = overlayHTML;
document.body.appendChild(overlay);

let acc = get("accordian");
acc.onclick = function () {
let panel = get("box2");
if (panel.style.display === "grid") {
panel.style.display = "none";
} else {
panel.style.display = "grid";
}
}

document.addEventListener('keydown', (event) => {
if (event.key === 'm') {
let panel = get("box2");
if (panel.style.display === "grid") {
panel.style.display = "none";
} else {
panel.style.display = "grid";
}
}
});

'use strict';
window.loaded = false;

class Answer {
constructor(answer, type) {
this.body = answer;
this.type = type;
}

get isMultiChoice() {
return this.type === "multiple_choice";
}

get isFreeResponse() {
return this.type === "free_response";
}

get isExpression() {
return this.type === "expression";
}

get isDropdown() {
return this.type === "dropdown";
}

log() {
try {
const answer = this.body;
answer.forEach(ans => {
if (typeof ans === "string") {
if (ans.includes("web+graphie")) {
this.body[this.body.indexOf(ans)] = "";
this.printImage(ans);
} else {
answer[answer.indexOf(ans)] = ans.replaceAll("$", "");
}
}
});
} catch (error) {
handleFetchError(error);
}
}
}

const originalFetch = window.fetch;
window.fetch = function () {
return originalFetch.apply(this, arguments).then(async (res) => {
try {
if (res.url.includes("/getAssessmentItem")) {
const clone = res.clone();
const json = await clone.json();
let item, question;

item = json.data.assessmentItem.item.itemData;
question = JSON.parse(item).question;

Object.keys(question.widgets).forEach(widgetName => {
switch (widgetName.split(" ")[0]) {
case "numeric-input":
freeResponseAnswerFrom(question).log();
break;
case "radio":
multipleChoiceAnswerFrom(question).log();
break;
case "expression":
expressionAnswerFrom(question).log();
break;
case "dropdown":
dropdownAnswerFrom(question).log();
break;
default:
console.warn('Unhandled widget type:', widgetName);
}
});
}

if (!window.loaded) {
console.clear();
window.loaded = true;
}

return res;
} catch (error) {
handleFetchError(error);
return res; // Ensure the original response is returned even if an error occurs
}
});
}

let curAns = 1;

function freeResponseAnswerFrom(question) {
const answer = Object.values(question.widgets).flatMap((widget) => {
if (widget.options?.answers) {
return widget.options.answers
.filter(answer => answer.status === "correct")
.map(answer => {
let createPar = document.createElement('section');
createPar.innerHTML = answer.value;
document.getElementById('ansBreak').append(createPar);
curAns++;
return answer.value;
});
}
return [];
});

return new Answer(answer, "free_response");
}

function multipleChoiceAnswerFrom(question) {
const answer = Object.values(question.widgets).flatMap((widget) => {
if (widget.options?.choices) {
return widget.options.choices
.filter(choice => choice.correct)
.map(choice => {
let createPar = document.createElement('section');
createPar.innerHTML = choice.content;
document.getElementById('ansBreak').append(createPar);
curAns++;
return choice.content;
});
}
return [];
});

return new Answer(answer, "multiple_choice");
}

function expressionAnswerFrom(question) {
const answer = Object.values(question.widgets).flatMap((widget) => {
if (widget.options?.answerForms) {
return widget.options.answerForms
.filter(answer => Object.values(answer).includes("correct"))
.map(answer => {
let createPar = document.createElement('section');
createPar.innerHTML = answer.value;
document.getElementById('ansBreak').append(createPar);
curAns++;
return answer.value;
});
}
return [];
});

return new Answer(answer, "expression");
}

function dropdownAnswerFrom(question) {
const answer = Object.values(question.widgets).flatMap((widget) => {
if (widget.options?.choices) {
return widget.options.choices
.filter(choice => choice.correct)
.map(choice => {
let createPar = document.createElement('section');
createPar.innerHTML = choice.content;
document.getElementById('ansBreak').append(createPar);
curAns++;
return choice.content;
});
}
return [];
});

return new Answer(answer, "dropdown");
}
})();

Post reply

Sign in to post a reply.