mark empty inputs always

This commit is contained in:
Zhora Shalyapin 2025-03-21 10:18:41 +00:00
parent 230b364206
commit eeca874d35

View File

@ -2,14 +2,11 @@
// @name Runcity Questionary // @name Runcity Questionary
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 2024-11-18 // @version 2024-11-18
// @description Prettify pages & store data on server // @description Add red border for empty inputs
// @author You // @author You
// @match https://www.runcity.org/ru/events/*/online/ // @match https://www.runcity.org/ru/events/*/online/
// @icon https://www.google.com/s2/favicons?sz=64&domain=runcity.org // @icon https://www.google.com/s2/favicons?sz=64&domain=runcity.org
// @grant none // @grant none
// @require https://code.jquery.com/jquery-3.7.1.min.js
// @require https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js
// @require https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js
// ==/UserScript== // ==/UserScript==
(function () { (function () {
@ -17,48 +14,6 @@
/* COPY FROM MAIN */ /* COPY FROM MAIN */
class Tag {
static make(name, params) {
let element = document.createElement(name)
for (const [name, value] of Object.entries(params)) {
if (name == "on") {
for (const [event, listener] of Object.entries(value)) {
element.addEventListener(event, listener)
}
}
else if (name == "classes") {
element.classList.add(...value.split(" "))
}
else if (name == "children") {
element.append(...value)
}
else if (name.startsWith("_")) {
element.setAttribute(name.substring(1), value)
}
else
element[name] = value
}
return element
}
static div(params) {
return this.make("div", params)
}
static span(params) {
return this.make("span", params)
}
static button(params) {
return this.make("button", params)
}
static input(params) {
return this.make("input", params)
}
}
function addStylesToHead(styles) { function addStylesToHead(styles) {
let styleSheet = document.createElement("style") let styleSheet = document.createElement("style")
styleSheet.textContent = styles styleSheet.textContent = styles
@ -67,52 +22,32 @@
/* END COPY FROM MAIN */ /* END COPY FROM MAIN */
function addEmptyCheckForSubmitButton() { function addClassIfEmptyInput(input) {
let saveButton = document.querySelector(`.check-answers-saved`) if (input.value.trim() == "") {
let inputs = document.querySelectorAll(`input.cp_team_answer`) input.classList.add("empty-input")
}
else {
input.classList.remove("empty-input")
}
}
inputs.forEach(el => el.addEventListener("focus", e => el.classList.remove("empty-input"))) function addEmptyInputCheck() {
let inputs = [...document.querySelectorAll(`input.cp_team_answer`)]
let pseudoSaveButton = Tag.div({ for (const input of inputs) {
id: "pseudo-save", addClassIfEmptyInput(input)
on: {
click: function (e) {
e.stopPropagation()
let canSend = true input.addEventListener("input", e => {
for (const input of inputs) { addClassIfEmptyInput(input)
if (input.value.trim() == "") { })
input.classList.add("empty-input") }
canSend = false
}
}
if (canSend) {
saveButton.click()
}
}
}
})
saveButton.append(pseudoSaveButton)
addStylesToHead(` addStylesToHead(`
.check-answers-saved {
position: relative;
#pseudo-save {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
}
.empty-input { .empty-input {
border-color: red !important; border-color: red !important;
} }
`) `)
} }
addEmptyCheckForSubmitButton() addEmptyInputCheck()
})(); })();