Skip to content

Instantly share code, notes, and snippets.

@Apearson75
Last active April 30, 2024 19:21
Show Gist options
  • Save Apearson75/2247c8190d797ed9427bd36e5a0c7c95 to your computer and use it in GitHub Desktop.
Save Apearson75/2247c8190d797ed9427bd36e5a0c7c95 to your computer and use it in GitHub Desktop.
Mathswatch Answer Viewer
javascript:function getCookie(e){const t=`; ${document.cookie}`.split(`; ${e}=`);if(2===t.length)return t.pop().split(";").shift()}async function getRequest(e,t){const s=await fetch(e,{headers:{accept:"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"empty","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-csrf-token":getCookie("_csrf")},referrer:t,referrerPolicy:"strict-origin-when-cross-origin",body:null,method:"GET",mode:"cors",credentials:"include"});return await s.json()}async function getAnswers(){const e=document.querySelector("#studentmodal > div > div:nth-child(4) > div > div.questioncolumn > div > div.progressblock > div > div.col-sm-10.col-xs-8 > div > div:nth-child(1) > progressbar"),t=currentPage.split("/");let s="Answers Are:";if(100==parseInt(e.getAttribute("value"))&&7==t.length&&"assignment"==t[4]){const e=t[5],n=t[6],c=await getRequest(`https://vle.mathswatch.co.uk/duocms/api/answers?assignedwork_id=${e}`,currentPage);if("success"==c.status){c.data.forEach((e=>{e.question_id==n&&e.answer.forEach((e=>{0!=e.marks&&(e.text.constructor==Array?e.text.forEach((e=>{null!=e&&(s+=`\n${e}`)})):s+=`\n${e.text}`)}))}))}return[s,n]}return null}let currentPage=location.href;setInterval((async function(){if(currentPage!=location.href){currentPage=location.href;const[e,t]=await getAnswers(),s=document.querySelector("#studentmodal > div > div:nth-child(4) > div > div > div > div.row > div");if(null!=e){s.innerHTML="";const n=document.createElement("img");n.src=`https://vle.mathswatch.co.uk/images/questions/question${t}.png`,n.className="col-sm-offset-3 col-sm-6 col-xs-12",s.appendChild(n);const c=document.createElement("p");c.innerText+=e,c.className="col-sm-offset-3 col-sm-6 col-xs-12",c.style.fontWeight=700,c.style.fontSize="x-large",s.appendChild(c)}}}),500),console.log("Answer Viewer should be ready: ✅"),alert("Answer Viewer should be ready: ✅\nReloading the page will remove the script.");
eval(await (await fetch('https://gist.githubusercontent.com/Apearson75/2247c8190d797ed9427bd36e5a0c7c95/raw/fb1b3e94db7513babb8dee450e2bd478b0af9988/mini.js')).text())
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
async function getRequest(url, referrer) {
const req = await fetch(url, {
"headers": {
"accept": "*/*",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-csrf-token": getCookie('_csrf')
},
"referrer": referrer,
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
});
const json = await req.json();
return json;
}
async function getAnswers() {
const progressBar = document.querySelector("#studentmodal > div > div:nth-child(4) > div > div.questioncolumn > div > div.progressblock > div > div.col-sm-10.col-xs-8 > div > div:nth-child(1) > progressbar");
const parsedURL = currentPage.split('/');
let answers = 'Answers Are:';
if (parseInt(progressBar.getAttribute('value')) == 100 && parsedURL.length == 7 && parsedURL[4] == "assignment") {
const assignmentId = parsedURL[5];
const questionId = parsedURL[6];
const req = await getRequest(`https://vle.mathswatch.co.uk/duocms/api/answers?assignedwork_id=${assignmentId}`, currentPage);
if (req.status == "success") {
const questions = req.data;
questions.forEach(q => {
if (q.question_id == questionId) {
q.answer.forEach(a => {
if (a.marks!=0) {
if (a.text.constructor == Array) {
a.text.forEach(multipleAnswer => {
if (multipleAnswer != null) {
answers += `\n${multipleAnswer}`;
}
})
} else {
answers += `\n${a.text}`;
}
}
})
}
});
}
return [answers, questionId];
}
return null;
}
let currentPage = location.href;
setInterval(async function()
{
if (currentPage != location.href)
{
currentPage = location.href;
const [answers, questionId] = await getAnswers();
const theBox = document.querySelector("#studentmodal > div > div:nth-child(4) > div > div > div > div.row > div");
if (answers != null) {
theBox.innerHTML = '';
const questionImage = document.createElement('img');
questionImage.src = `https://vle.mathswatch.co.uk/images/questions/question${questionId}.png`;
questionImage.className="col-sm-offset-3 col-sm-6 col-xs-12";
theBox.appendChild(questionImage);
const answerThing = document.createElement('p');
answerThing.innerText += answers;
answerThing.className="col-sm-offset-3 col-sm-6 col-xs-12";
answerThing.style.fontWeight = 700;
answerThing.style.fontSize = "x-large"
theBox.appendChild(answerThing);
}
}
}, 500);
console.log("Answer Viewer should be ready: ✅");
alert("Answer Viewer should be ready: ✅\nReloading the page will remove the script.");
@nahjittrippinfr
Copy link

How exactly is the script supposed to work? After it's in the console nothing seems to happen
image
image

@3rev3
Copy link

3rev3 commented Apr 30, 2024

not work because the api doesnt cointain the answer, is there any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment