Created
December 3, 2024 18:50
-
-
Save masflam/233a56cfdc140dbee7a6cfddb9836685 to your computer and use it in GitHub Desktop.
Tampermonkey userscript for Advent of Code that prevents you from submitting non-numbers by accident and losing 1 minute to a misclick
This file contains hidden or 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
| // ==UserScript== | |
| // @name AoC Guardrails | |
| // @namespace https://masflam.com | |
| // @version 2024-12-03 | |
| // @description Prevent sending non-numbers by accident. | |
| // @author MasFlam | |
| // @match *://adventofcode.com/* | |
| // @icon https://adventofcode.com/favicon.png | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| const style = document.createElement('style'); | |
| style.type = 'text/css'; | |
| //style.styleSheet.cssText = ':invalid { border-color: red; }'; | |
| style.appendChild(document.createTextNode(':invalid { border-color: red !important; }')); | |
| document.getElementsByTagName('head')[0].appendChild(style); | |
| console.log(document.getElementsByName('answer')); | |
| const ans_inputs = document.getElementsByName('answer'); | |
| for (const answer_input of ans_inputs) { | |
| console.log(answer_input); | |
| answer_input.required = true; | |
| const ins = document.createElement('label'); | |
| //ins.style = 'color: red;'; | |
| const L = document.createElement('span'); | |
| const R = document.createElement('span'); | |
| L.innerText = ' '; | |
| R.innerText = 'force?'; | |
| const checkbox = document.createElement('input'); | |
| checkbox.type = 'checkbox'; | |
| checkbox.addEventListener('change', (evt) => { | |
| console.log(evt.target.checked); | |
| if (!evt.target.checked) { | |
| for (const ansin of ans_inputs) { | |
| console.log(ansin); | |
| ansin.pattern = '[0-9]+'; | |
| } | |
| } else { | |
| for (const ansin of ans_inputs) { | |
| console.log(ansin); | |
| //ansin.removeAttribute('pattern'); | |
| ansin.pattern = '.+'; | |
| } | |
| } | |
| }); | |
| checkbox.dispatchEvent(new Event('change')); | |
| ins.appendChild(L); | |
| ins.appendChild(checkbox); | |
| ins.appendChild(R); | |
| answer_input.insertAdjacentElement('afterend', ins); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment