Skip to content

Instantly share code, notes, and snippets.

@CodeDraken
Created December 22, 2020 03:22
Show Gist options
  • Save CodeDraken/79365528cc9b91125bd14433663eb084 to your computer and use it in GitHub Desktop.
Save CodeDraken/79365528cc9b91125bd14433663eb084 to your computer and use it in GitHub Desktop.
Automate the Web - Basics (Udemy, Lichess Scripts)
/// UDEMY ///
// Resets progress
// ERROR PRONE WAY
// checkboxes.forEach((checkbox, i) => {
// setTimeout(() => checkbox.click(), i * 50);
// });
// all *checked* checkbox elements within the "curriculum section"
var checkboxes = document
.querySelector('[data-purpose=curriculum-section-container]')
.querySelectorAll('input[type=checkbox]:checked');
// a functional loop
function loopCheckboxes(i = 0) {
// no more checkboxes - our exit case
if (i > checkboxes.length - 1) return;
const checkbox = checkboxes[i];
checkbox.click();
setTimeout(() => waitForCheck(checkbox, i), 50);
}
// wait for the checkbox to be unchecked
// keep track of the attempts to retry clicking after a number of failures
function waitForCheck(checkbox, i, attempt = 1) {
// retry clicking after 5 seconds (for network issues)
if (checkbox.checked && attempt >= 20) {
return loopCheckboxes(i);
} else if (checkbox.checked) {
// if the checkbox is still checked keep waiting, pausing the loop
return setTimeout(() => waitForCheck(checkbox, i, attempt + 1), 250);
} else {
// continue loop on next checkbox (if we didnt exit in the above if-statement)
loopCheckboxes(i + 1);
}
}
function checkAll() {
var checkboxes = document
.querySelector('[data-purpose=curriculum-section-container]')
.querySelectorAll('input[type=checkbox]');
checkboxes.forEach((checkbox, i) => {
if (!checkbox.checked) {
setTimeout(() => checkbox.click(), i * 50);
}
});
}
// start the loop
loopCheckboxes();
// checkAll()
/// LICHESS ////
// Converts study chapters to interactive lessons
// 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