Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active November 19, 2019 00:51
Show Gist options
  • Save wlib/0d4c68436f7930a8804a109afe484317 to your computer and use it in GitHub Desktop.
Save wlib/0d4c68436f7930a8804a109afe484317 to your computer and use it in GitHub Desktop.
Toggle quizlet test answers on and off every time you press the escape key
javascript:script=document.createElement("script");script.src="https://cdn.rawgit.com/wlib/0d4c68436f7930a8804a109afe484317/raw/quizletTestAnswers.js";document.body.appendChild(script);//
// Answers aren't being displayed yet
let isDisplayed = false;
// Map of terms; word: definition
const answers = Quizlet.TestModeData.terms.reduce((termsMap, term) => {
termsMap[term.word] = term.definition;
return termsMap;
}, {});
// Make a pretty-printed string to display
const answersString = JSON.stringify(answers, null, 2).replace(/\"/g, "").replace(/[{}]/g, "");
// Grab an element to display to
const answersContainer = document.querySelector(".ModeControls-main");
// Get that element's state so we can return to it later
const hiddenState = answersContainer.innerHTML;
// Make sure text fits and we can scroll if it doesn't
answersContainer.style.fontSize = "0.8em";
answersContainer.style.overflow = "auto"
const toggle = () => {
if (isDisplayed) {
answersContainer.innerText = answersString;
} else {
answersContainer.innerHTML = hiddenState;
}
return isDisplayed = !isDisplayed;
}
// Switch between hiding and showing answers by pressing the ESCAPE key
document.body.addEventListener("keydown", e => {
if (e.key === "Escape") {
toggle();
}
});
Copy link

ghost commented Nov 19, 2019

Super useful script :)

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