Path: blob/master/scripts/quizlet_live.js
853 views
try {1async function getSetId(pin) {2const res = await fetch(`https://quizlet.com/webapi/3.8/multiplayer/game-instance?gameCode=${pin}`);3const json = await res.json();4if(json.error) throw new Error(json.error.message);5return json.gameInstance.itemId;6}7async function getSetData(setId) {8const res = await fetch(`https://quizlet.com/${setId}`);9const text = await res.text();10const data = text.match(/\\"termIdToTermsMap\\":{.+?{.+?\\"termSort\\":/gi)?.[0];11if(!data) throw new Error("Failed to parse set data.");12const parsed = JSON.parse(data.slice(21, -14).replaceAll(`\\"`,`"`));13return [Object.fromEntries(Object.values(parsed).map(({word, definition}) => [word, definition])), Object.fromEntries(Object.values(parsed).map(({word, definition}) => [definition, word]))];14}15function getActiveQuestion() {16try {17const question = document.querySelector(".StudentPrompt-text").textContent;18const answers = Array.from(document.querySelectorAll(".StudentAnswerOption-text"));19return [question, answers];20}catch {}21return [null, null];22}23(async() => {24const pin = prompt("Enter PIN, like: XXX-XXX", "").match(/[0-9a-zA-Z]/g).join("");25if(pin.length !== 6) throw new Error("Pin must be 6 characters in length.");26const setId = await getSetId(pin);27const [term2Def, def2Term] = await getSetData(setId);28setInterval(async function() {29const [question, choices] = getActiveQuestion();30if(!question || !choices) return;31if(question in term2Def) {32choices.forEach((choice) => {33if(choice.textContent === term2Def[question]) choice.style.fontWeight = "bolder";34});35}else if(question in def2Term) {36choices.forEach((choice) => {37if(choice.textContent === def2Term[question]) choice.style.fontWeight = "bolder";38});39}else {40choices.forEach((choice) => {41choice.style.fontWeight = "normal";42});43}44}, 0);45})();46}catch(err) {47alert(err);48}4950