16 lines
672 B
JavaScript
16 lines
672 B
JavaScript
const browser = require('../browser');
|
|
|
|
module.exports = async ({ selector, attributes = [] }) => {
|
|
if (!selector) throw new Error('Selector é obrigatório');
|
|
await browser.start();
|
|
const attrs = Array.isArray(attributes) ? attributes : [];
|
|
const elements = await browser.evaluateJS(({ sel, attrs: a }) => {
|
|
return Array.from(document.querySelectorAll(sel)).map(el => {
|
|
const data = { text: (el.innerText || el.textContent || '').trim() };
|
|
a.forEach(attr => data[attr] = el.getAttribute(attr));
|
|
return data;
|
|
});
|
|
}, { sel: selector, attrs });
|
|
return { success: true, action: 'extract_elements', count: elements.length, elements };
|
|
};
|