2020-11-05 08:44:45 +01:00
|
|
|
import { render, html } from "https://cdn.skypack.dev/lit-html";
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
const tags = ["p", "strong", "em", "sub", "sup", "span", "br"];
|
2020-11-05 07:48:37 +01:00
|
|
|
|
|
|
|
function removeTags(input) {
|
|
|
|
let copy = input.replace('"', '\\"');
|
|
|
|
|
|
|
|
for (const tag of tags)
|
|
|
|
copy = copy.replace(`<${tag}>`, "").replace(`</${tag}>`, "");
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here is the function to parse the json object.
|
|
|
|
// It returns object with key/value pair where key is the question and value is the answer
|
2020-11-05 08:44:45 +01:00
|
|
|
function parseQuestions(fileObject) {
|
2020-11-05 07:48:37 +01:00
|
|
|
const allAnswers = {};
|
|
|
|
|
|
|
|
for (const question of fileObject.data.quiz.info.questions) {
|
|
|
|
let answer;
|
|
|
|
|
|
|
|
if (question.type === "MCQ") {
|
|
|
|
if (question.structure.options[question.structure.answer].text == "") {
|
|
|
|
answer =
|
|
|
|
question.structure.options[question.structure.answer].media[0].url;
|
|
|
|
} else {
|
|
|
|
answer = removeTags(
|
|
|
|
question.structure.options[question.structure.answer].text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (question.type == "MSQ") {
|
|
|
|
answer = question.structure.answer
|
|
|
|
.map((answerC) => {
|
|
|
|
if (question.structure.options[answerC].text == "") {
|
|
|
|
return question.structure.options[answerC].media[0].url;
|
|
|
|
} else {
|
|
|
|
return removeTags(question.structure.options[answerC].text);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((a) => a !== undefined);
|
|
|
|
} else if (question.type == "BLANK") {
|
|
|
|
answer = question.structure.options.map((o) => removeTags(o.text));
|
|
|
|
}
|
|
|
|
|
|
|
|
const questionStr = removeTags(question.structure.query.text);
|
|
|
|
|
|
|
|
allAnswers[questionStr] = answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allAnswers;
|
|
|
|
}
|
2020-11-05 08:44:45 +01:00
|
|
|
async function main(id) {
|
|
|
|
const res = await fetch(
|
|
|
|
`https://api.allorigins.win/get?url=${encodeURIComponent(
|
|
|
|
`https://quizizz.com/quiz/${id}`
|
|
|
|
)}`,
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
const jsonObject = await res.json();
|
|
|
|
const parsedJson = JSON.parse(jsonObject.contents);
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
console.log(parsedJson.data);
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
const answers = parseQuestions(parsedJson);
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
const sorted = Object.entries(answers).sort();
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
render(renderQuestions(sorted), rootElement);
|
2020-11-05 07:48:37 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
const inputElement = document.getElementById("id");
|
|
|
|
const rootElement = document.getElementById("root");
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
inputElement.addEventListener("keypress", (e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
main(e.target.value).catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
2020-11-05 07:48:37 +01:00
|
|
|
|
2020-11-05 08:44:45 +01:00
|
|
|
console.log("here");
|
|
|
|
|
|
|
|
const renderQuestions = (questions) => {
|
|
|
|
return html`${questions.map(([question, answer]) => {
|
|
|
|
return html`
|
|
|
|
<div class="question-container">
|
|
|
|
<div class="question">${question}</div>
|
|
|
|
${Array.isArray(answer)
|
|
|
|
? html`
|
|
|
|
<ul>
|
|
|
|
${answer.map((correct) => {
|
|
|
|
return html`<li class="answer">${correct}</li> `;
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
`
|
|
|
|
: html` <div class="answer answer-single">${answer}</div> `}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
})}`;
|
|
|
|
};
|