Skip to content

Instantly share code, notes, and snippets.

@thevtm
Last active March 5, 2023 19:00
Show Gist options
  • Save thevtm/ed7b7a4040fb74c8fc8be0a6de4b2875 to your computer and use it in GitHub Desktop.
Save thevtm/ed7b7a4040fb74c8fc8be0a6de4b2875 to your computer and use it in GitHub Desktop.
Magic Flashcards
/***
* __ ______ ________________ ________ ___ _____ __ ___________ ____ ____ _____
* / |/ / | / ____/ _/ ____/ / ____/ / / | / ___// / / / ____/ | / __ \/ __ \/ ___/
* / /|_/ / /| |/ / __ / // / / /_ / / / /| | \__ \/ /_/ / / / /| | / /_/ / / / /\__ \
* / / / / ___ / /_/ // // /___ / __/ / /___/ ___ |___/ / __ / /___/ ___ |/ _, _/ /_/ /___/ /
* /_/ /_/_/ |_\____/___/\____/ /_/ /_____/_/ |_/____/_/ /_/\____/_/ |_/_/ |_/_____//____/
*
*/
(function() {
var $ = document.querySelector.bind(document)
var get_user_guess = () => $('.flashcard-game-card-front #user-guess-text-area').value
var set_user_guess = (guess) => $('.flashcard-game-card-front #user-guess-text-area').value = guess
var get_current_card = () => parseInt(($('.deck-stats-message').textContent.match(/mastered (\d+) out/) || ["0", "0"])[1])
var get_total_cards = () => parseInt($('.deck-stats-message').textContent.match(/(\d+) cards/)[1])
var is_front_side = () => $('.flashcard-game-card-front').classList.contains("invisible") === false
var is_back_side = () => $('.flashcard-game-card-back').classList.contains("invisible") === false
var flip_card = () => flipCard()
var click_green = () => $("#played-card-submit.btn-green").click()
var is_done = () => /You have mastered all/.test($('.deck-stats-message').textContent)
var print_state = () => console.log(`card: ${get_current_card()}/${get_total_cards()}, front: ${is_front_side()}, back: ${is_back_side()}`)
var timeout_id = 0
var start_timeout = () => timeout_id = setInterval(do_flash_card, 100)
var stop_timeout = () => clearInterval(timeout_id)
function do_flash_card() {
var ANSWER = "I'm not a robot!"
print_state()
if (is_done()) {
console.log("Flashcards done!!")
stop_timeout()
} else if (is_front_side()) {
if (get_user_guess() !== ANSWER) {
console.log("Setting the right answer.")
set_user_guess(ANSWER)
} else {
console.log("Flipping the card.")
flip_card()
}
} else if (is_back_side()) {
console.log("Clicking the green button.")
click_green()
} else {
throw new Error("Weird side!!")
}
}
start_timeout()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment