3 ms·
My tool-assisted speedrun (0.001s): let setGo = document.querySelector(".setGo"); let observer = new MutationObserver(mutations => { if (setGo.classL
by chrismorgan 4y ago
My tool-assisted speedrun (0.001s):
let setGo = document.querySelector(".setGo");
let observer = new MutationObserver(mutations => {
if (setGo.classList.contains("going")) {
for (const { target } of mutations) {
if (target.checked == target.disabled) {
target.click();
}
}
}
});
observer.observe(setGo, {attributes: true});
document.querySelectorAll(".theSport input").forEach(checkbox => {
observer.observe(checkbox, {attributes: true});
});
Explanation: instead of messing about with timers, it asks to be informed when any attribute changes (could use attributeFilter to only hear about "class" for setGo and "disabled" for checkboxes, but I didn’t bother). This is faster than can be acheived with setTimeout, setInterval or requestAnimationFrame, as timers are deliberately speed-capped. (Whereas mutation observers create microtasks, so we can go through this whole thing in one frame.)
When the observer gets notified (in a microtask), it checks if we’re going (in order to avoid false starts), and then goes through the checkboxes it’s been told just changed, and checks if they’re enabled and unchecked, and clicks them. In practice, the first mutation will come in with mutation records for setGo and the first checkbox, which we’ll click, which will trigger an event (a task) from the page code which enables the second checkbox, which will trigger an immediate mutation notification (a microtask), &c. until it’s all done, without ever rendering a frame in the meantime.