src/collectors/html.ts (view raw)
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 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import * as cheerio from 'cheerio';
import type { Observations } from '../core/types.js';
export function coletarHtml(obs: Observations): void {
if (!obs.html) return;
let $: cheerio.CheerioAPI;
try {
$ = cheerio.load(obs.html);
} catch (erro) {
obs.avisos.push(`Falha ao interpretar o HTML: ${(erro as Error).message}`);
return;
}
const meta: Record<string, string> = {};
$('meta').each((_, el) => {
const nome = ($(el).attr('name') ?? $(el).attr('property') ?? $(el).attr('http-equiv') ?? '')
.toLowerCase()
.trim();
const conteudo = $(el).attr('content');
if (nome && conteudo != null) meta[nome] = conteudo;
});
obs.meta = meta;
const scriptSrc: string[] = [];
$('script[src]').each((_, el) => {
const src = $(el).attr('src');
if (src) scriptSrc.push(src);
});
obs.scriptSrc = scriptSrc;
const links: string[] = [];
$('link[href]').each((_, el) => {
const href = $(el).attr('href');
if (href) links.push(href);
});
$('img[src]').each((_, el) => {
const src = $(el).attr('src');
if (src) links.push(src);
});
obs.linksAssets = links;
}
|