// ==UserScript==
// @name KhanHack
// @namespace https://greasyfork.org/users/783447
// @version 5.7
// @description Khan Academy Answer Hack
// @author Logzilla6 - IlyTobias - Illusions
// @match https://*.khanacademy.org/*
// @icon https://i.ibb.co/K5g1KMq/Untitled-drawing-3.png
// @downloadURL none
// ==/UserScript==
//ALL FOLLOWING CODE IS UNDER THE KHANHACK TRADEMARK. UNAUTHORIZED DISTRIBUTION CAN/WILL RESULT IN LEGAL ACTION
//Note that KhanHack™ is an independent initiative and is not affiliated with or endorsed by Khan Academy. We respect the work of Khan Academy and its mission to provide free education, but KhanHack™ operates separately with its own unique goals.
let mainMenu = document.createElement('div');
mainMenu.id = 'mainMenu';
mainMenu.style.position = 'fixed';
mainMenu.style.bottom = '.5vw';
mainMenu.style.left = '19vw';
mainMenu.style.width = '300px';
mainMenu.style.height = '400px';
mainMenu.style.backgroundColor = '#123576';
mainMenu.style.border = '3px solid #07152e';
mainMenu.style.borderRadius = '20px';
mainMenu.style.padding = '10px';
mainMenu.style.color = "white";
mainMenu.style.fontFamily = "Noto sans";
mainMenu.style.fontWeight = "500";
mainMenu.style.transition = "all 0.3s ease";
mainMenu.style.zIndex = '1000';
mainMenu.style.display = 'flex';
mainMenu.style.flexDirection = 'column';
let copied = document.createElement('div');
copied.id = 'copyText';
copied.style.position = 'fixed';
copied.style.bottom = '17.5vw';
copied.style.left = '22.2vw';
copied.style.width = '150px';
copied.style.height = 'auto';
copied.style.backgroundColor = '#123576';
copied.style.border = '3px solid #07152e';
copied.style.borderRadius = '13px';
copied.style.color = "white";
copied.style.fontFamily = "Noto sans";
copied.style.fontWeight = "500";
copied.style.transition = "all 0.15s ease";
copied.style.padding = "5px";
copied.style.opacity = "0";
let answerBlocks = [];
let currentCombinedAnswer = '';
let isGhostModeEnabled = false;
const setCopiedContent = () => {
copied.innerHTML = `
Settings Menu
Ghost Mode:
Auto Answer: BETA
Point Farmer: (Coming Soon)
Beta Access In Discord
KhanHack™ | 5.7
`;
document.getElementById('backArrow').addEventListener('click', setMainMenuContent);
document.getElementById('ghostModeToggle').addEventListener('change', function() {
isGhostModeEnabled = this.checked;
if (isGhostModeEnabled) {
enableGhostMode();
} else {
disableGhostMode();
}
});
});
};
const enableGhostMode = () => {
mainMenu.style.opacity = '0';
mainMenu.addEventListener('mouseenter', handleMouseEnter);
mainMenu.addEventListener('mouseleave', handleMouseLeave);
};
const disableGhostMode = () => {
mainMenu.style.opacity = '1';
mainMenu.removeEventListener('mouseenter', handleMouseEnter);
mainMenu.removeEventListener('mouseleave', handleMouseLeave);
};
const handleMouseEnter = () => {
mainMenu.style.opacity = '1';
};
const handleMouseLeave = () => {
mainMenu.style.opacity = '0';
};
const addDiscord = () => {
document.getElementById('discordIcon').addEventListener('click', function() {
window.open('https://discord.gg/khanhack', '_blank');
});
};
const addClear = () => {
document.getElementById('clearButton').addEventListener('click', function() {
location.reload();
});
};
const script = document.createElement("script");
script.src = "https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js";
document.head.appendChild(script);
const katexStyle = document.createElement("link");
katexStyle.rel = "stylesheet";
katexStyle.href = "https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css";
document.head.appendChild(katexStyle);
const addAnswerBlock = (answer, save = true) => {
const answerList = document.getElementById('answerList');
const block = document.createElement('div');
block.className = 'block no-select';
block.style.cursor = "pointer";
block.addEventListener("click", () => {
navigator.clipboard.writeText(answer);
copyFlash();
});
const tc = document.createElement('p');
tc.className = 'answer';
const latexPattern = /\\frac|\\sqrt|\\times|\\cdot|\\left|\\right|\^|\$|\{|\}/;
if (latexPattern.test(answer)) {
tc.innerHTML = '';
katex.render(answer, tc)
} else {
tc.innerHTML = `${answer}`;
}
tc.style.fontFamily = '"Noto Sans", sans-serif';
tc.style.fontWeight = "500";
tc.style.fontStyle = "normal";
tc.style.fontSize = "16px"
block.appendChild(tc);
answerList.appendChild(block);
if (save) {
answerBlocks.push({ type: 'text', content: answer });
}
};
document.body.appendChild(mainMenu);
document.body.appendChild(copied);
setMainMenuContent();
setCopiedContent();
let originalJson = JSON.parse;
JSON.parse = function (jsonString) {
let parsedData = originalJson(jsonString);
try {
if (parsedData.data && parsedData.data.assessmentItem && parsedData.data.assessmentItem.item) {
let itemData = JSON.parse(parsedData.data.assessmentItem.item.itemData);
let hasGradedWidget = Object.values(itemData.question.widgets).some(widget => widget.graded === true);
if (hasGradedWidget) {
for (let widgetKey in itemData.question.widgets) {
let widget = itemData.question.widgets[widgetKey];
switch (widget.type) {
case "numeric-input":
handleNumeric(widget);
break;
case "radio":
handleRadio(widget);
break;
case "expression":
handleExpression(widget);
break;
case "dropdown":
handleDropdown(widget);
break;
case "interactive-graph":
handleIntGraph(widget);
break;
case "grapher":
handleGrapher(widget);
break;
case "input-number":
handleInputNum(widget);
break;
case "matcher":
handleMatcher(widget);
break;
case "categorizer":
handleCateg(widget);
break;
case "label-image":
handleLabel(widget);
break;
case "matrix":
handleMatrix(widget);
break;
default:
console.log("Unknown widget: " + widget.type);
break;
}
}
if (currentCombinedAnswer.trim() !== '') {
addAnswerBlock(currentCombinedAnswer);
currentCombinedAnswer = '';
}
}
}
} catch (error) {
console.log("Error parsing JSON:", error);
}
return parsedData;
};
function cleanLatexExpression(answer) {
return answer
//.replace(/\\times/g, '×')
// .replace(/\\frac{([^}]*)}{([^}]*)}/g, '($1/$2)')
// .replace(/\\cdot/g, '⋅')
// .replace(/\\left|\\right/g, '')
// .replace(/\^/g, '^')
// .replace(/\\(?:[a-zA-Z]+)/g, '')
// .replace(/(? item.correct === true).map(item => item.content);
let ansArr = [];
let isNone = widget.options.choices.filter(item => item.isNoneOfTheAbove === true && item.correct === true)
if (isNone.length > 0) {
currentCombinedAnswer += "None of the above";
return;
}
corAns.forEach(answer => {
const hasGraphie = answer.includes('web+graphie')
const hasNotGraphie = answer.includes('![]')
if(hasGraphie || hasNotGraphie == true) {
if(hasGraphie == true) {
const split = answer.split('web+graphie');
const midUrl = split[1].slice(0, -1);
const finalUrl = 'https' + midUrl + '.svg';
addImgAnswerBlock(finalUrl);
} else if(hasNotGraphie == true) {
const finalUrl = answer.slice(4, -1)
addImgAnswerBlock(finalUrl);
}
} else {
let cleaned = cleanLatexExpression(answer)
ansArr.push(cleaned)
}
})
if(ansArr.length) {
currentCombinedAnswer += ansArr.join("|")
}
}
function handleNumeric(widget) {
console.log(widget)
const numericAnswer = widget.options.answers[0].value;
currentCombinedAnswer += ` ${numericAnswer} `;
}
function handleExpression(widget) {
let expressionAnswer = widget.options.answerForms[0].value;
currentCombinedAnswer += ` ${expressionAnswer} `;
}
function handleDropdown(widget) {
let content = widget.options.choices.filter(item => item.correct === true).map(item => item.content);
currentCombinedAnswer += ` ${content[0]} `;
}
function handleIntGraph(widget) {
let coords = widget.options.correct.coords;
let validCoords = coords.filter(coord => coord !== undefined);
currentCombinedAnswer += ` ${validCoords.join(' | ')} `;
}
function handleInputNum(widget) {
let inputNumAnswer = widget.options.value;
currentCombinedAnswer += ` ${inputNumAnswer} `;
}
function handleMatcher(widget) {
let matchAnswer = widget.options.right;
currentCombinedAnswer += ` ${matchAnswer} `;
}
function handleGrapher(widget) {
let coords = widget.options.correct.coords;
currentCombinedAnswer += ` ${coords.join(' | ')} `;
}
function handleCateg(widget) {
let values = widget.options.values;
let categories = widget.options.categories;
let labeledValues = values.map(value => categories[value]);
currentCombinedAnswer += ` ${labeledValues} `
}
function handleLabel(widget) {
let markers = widget.options.markers;
let labels = markers.map(marker => marker.label);
markers.forEach((marker, index) => {
let answerLabel = labels[index];
//console.log(`${answerLabel}: ${marker.answers}`);
addAnswerBlock(` ${answerLabel} ${marker.answers} `)
});
//console.log(markers.map(marker => marker.label).toString())
//console.log(markers.map(test => test.answers[0]).toString())
}
function handleMatrix(widget) {
let arrs = widget.options.answers;
currentCombinedAnswer += ` ${arrs.join(' | ')} `
}