-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputit.js
29 lines (29 loc) · 1014 Bytes
/
inputit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// - INPUT text entry simulator
function inputit(element, newtext, { clobber = false, interval = 100 } = {}) {
return new Promise((resolve, reject) => {
function doinput(element, newval, clobber) {
if (element.type == "range") {
setTimeout(() => {
element.value = newval;
element.dispatchEvent(new Event("input"));
resolve("range entered");
}, interval);
} else {
newval = newval.toString().replace(/_/g, " "); // use _ as space
if (clobber) element.value = "";
if (!newval) {
element.dispatchEvent(new Event("input"));
element.blur();
resolve("text entered");
return;
}
let [, fc, text] = newval.match(/^(.)(.*)$/s);
setTimeout(() => {
element.value += fc; // input characters one at a time
doinput(element, text, false); // continue with remainder of text
}, interval);
}
}
doinput(element, newtext, clobber);
});
}