2017-10-17 22:30:36 -04:00
|
|
|
function triggerEvents(input) {
|
|
|
|
input.dispatchEvent(new Event('keydown'));
|
|
|
|
input.dispatchEvent(new Event('keypress'));
|
|
|
|
input.dispatchEvent(new Event('input'));
|
|
|
|
input.dispatchEvent(new Event('keyup'));
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function simulateInput(target, text) {
|
|
|
|
const input = document.querySelector(target);
|
|
|
|
if (!input || !input.matches('textarea, input')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text.length > 0) {
|
2020-12-23 16:10:24 -05:00
|
|
|
Array.prototype.forEach.call(text, (char) => {
|
2017-10-17 22:30:36 -04:00
|
|
|
input.value += char;
|
|
|
|
triggerEvents(input);
|
|
|
|
});
|
|
|
|
} else {
|
2017-10-31 13:39:36 -04:00
|
|
|
triggerEvents(input);
|
2017-10-17 22:30:36 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|