32 lines
1.6 KiB
JavaScript
32 lines
1.6 KiB
JavaScript
const browser = require('../browser');
|
|
|
|
module.exports = async ({ data }) => {
|
|
if (!data) throw new Error('Data é obrigatória');
|
|
await browser.start();
|
|
const result = await browser.evaluateJS((formData) => {
|
|
const inputs = Array.from(document.querySelectorAll('input, textarea, select'));
|
|
const filled = [];
|
|
inputs.forEach(input => {
|
|
const key = Object.keys(formData).find(k =>
|
|
(input.name && input.name.toLowerCase().includes(k.toLowerCase())) ||
|
|
(input.placeholder || '').toLowerCase().includes(k.toLowerCase()) ||
|
|
(input.id || '').toLowerCase().includes(k.toLowerCase()) ||
|
|
(input.getAttribute('aria-label') || '').toLowerCase().includes(k.toLowerCase())
|
|
);
|
|
if (key) {
|
|
const value = formData[key];
|
|
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 }));
|
|
filled.push({ field: input.name || input.id || input.placeholder || 'unknown', value });
|
|
}
|
|
});
|
|
return filled;
|
|
}, data);
|
|
return { success: true, action: 'fill_form_auto', filled: result, count: result.length };
|
|
};
|