Last active
December 22, 2020 03:14
-
-
Save CodeDraken/af7ab390824d58e21c68277fefec9f7b to your computer and use it in GitHub Desktop.
Converts Lichess study chapters to interactive lessons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all gear buttons for the chapters | |
var chapterOptionButtons = document.querySelectorAll('.study__chapters act') | |
// "id": "value" | |
var defaultOptions = { | |
// available options: normal, practice, conceal, gamebook | |
"chapter-mode": "gamebook", | |
// "chapter-orientation": "white" | |
} | |
// loop over the gear buttons, clicking each | |
function loopChapterOptions(i = 0) { | |
// always start with an exit case to avoid infinite loops | |
if (i > chapterOptionButtons.length - 1) return; | |
const optionsBtn = chapterOptionButtons[i]; | |
optionsBtn.click(); | |
waitForModal(i); | |
} | |
// calls itself with a delay until the modal appears | |
// retry clicking after X number of attempts | |
function waitForModal(i, attempt = 1) { | |
console.log('waiting for modal: ', i, attempt) | |
const studyModal = document.querySelector('.study__modal'); | |
// retry in 250 milliseconds if the modal hasn't appeared | |
if (!studyModal) { | |
// exit and retry clicking (for connection issues) | |
if (attempt > 20) return loopChapterOptions(i) | |
return setTimeout(() => waitForModal(i, attempt + 1), 250); | |
} | |
// if the modal is visible then set options and continue the loop | |
setOptionsAndContinue(studyModal, i); | |
} | |
function setOptionsAndContinue(studyModal, i, options = defaultOptions) { | |
const saveButton = studyModal.querySelector('.form-actions > button:nth-child(1)'); | |
// set options | |
for (const [elementId, desiredValue] of Object.entries(options)) { | |
const input = document.getElementById(elementId); | |
input.value = desiredValue; | |
} | |
// save and close the modal | |
// the modal always disappears even if there is no internet connection, so we dont have to wait for it | |
saveButton.click() | |
// continue the loop | |
setTimeout(() => loopChapterOptions(i + 1)) | |
} | |
loopChapterOptions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment