new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · all subjects

runtime/html-rewriter

3 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

HTMLRewriter extracts links from HTML with CSS selectors

Bun's HTMLRewriter API can extract links from HTML by chaining CSS selectors to match elements, text, and attributes. The .transform method accepts a Response, ArrayBuffer, or string as input.

Extract links example with HTMLRewriter

Example showing how to extract all href attributes from anchor tags: ```ts async function extractLinks(url: string) { const links = new Set<string>(); const response = await fetch(url); const rewriter = new HTMLRewriter().on("a[href]", { element(el) { const href = el.getAttribute("href"); if (href) { links.add(href); } }, }); await rewriter.transform(response).blob(); console.log([...links]); } await extractLinks("https://bun.com"); ```

Convert relative URLs to absolute with HTMLRewriter

When extracting links, relative URLs can be converted to absolute URLs using the URL constructor with the base URL as second parameter, wrapped in a try-catch block to handle invalid URLs: ```ts async function extractLinksFromURL(url: string) { const response = await fetch(url); const links = new Set<string>(); const rewriter = new HTMLRewriter().on("a[href]", { element(el) { const href = el.getAttribute("href"); if (href) { try { const absoluteURL = new URL(href, url).href; links.add(absoluteURL); } catch { links.add(href); } } }, }); await rewriter.transform(response).blob(); return [...links]; } const websiteLinks = await extractLinksFromURL("https://example.com"); ```

Give your agent this brain