34 lines
2.0 KiB
JavaScript
34 lines
2.0 KiB
JavaScript
const browser = require('../browser');
|
|
|
|
module.exports = async ({ label, text }) => {
|
|
if (!label || !text) throw new Error('Label e text são obrigatórios');
|
|
await browser.start();
|
|
const success = await browser.evaluateJS(({ labelText, inputText }) => {
|
|
const setInput = (input, value) => {
|
|
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
|
|
const nativeTextAreaSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value')?.set;
|
|
if (input.tagName === 'INPUT' && nativeSetter) nativeSetter.call(input, value);
|
|
else if (input.tagName === 'TEXTAREA' && nativeTextAreaSetter) nativeTextAreaSetter.call(input, value);
|
|
else input.value = value;
|
|
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
};
|
|
const foundLabel = Array.from(document.querySelectorAll('label')).find(l => (l.innerText || l.textContent || '').toLowerCase().includes(labelText.toLowerCase()));
|
|
if (foundLabel) {
|
|
const forAttr = foundLabel.getAttribute('for');
|
|
if (forAttr) { const input = document.getElementById(forAttr); if (input) { setInput(input, inputText); return true; } }
|
|
const input = foundLabel.querySelector('input, textarea'); if (input) { setInput(input, inputText); return true; }
|
|
}
|
|
const foundInput = Array.from(document.querySelectorAll('input, textarea')).find(i =>
|
|
(i.placeholder || '').toLowerCase().includes(labelText.toLowerCase()) ||
|
|
(i.name || '').toLowerCase().includes(labelText.toLowerCase()) ||
|
|
(i.id || '').toLowerCase().includes(labelText.toLowerCase()) ||
|
|
(i.getAttribute('aria-label') || '').toLowerCase().includes(labelText.toLowerCase())
|
|
);
|
|
if (foundInput) { setInput(foundInput, inputText); return true; }
|
|
return false;
|
|
}, { labelText: label, inputText: text });
|
|
if (!success) throw new Error('Campo não encontrado');
|
|
return { success: true, action: 'smart_type', label, text };
|
|
};
|