// Newgrounds captures (release 38 lane C, "Found during the case"): a user // page (`.newgrounds.com/`), an art page // (`www.newgrounds.com/art/view//`) and a movie page // (`www.newgrounds.com/portal/view/`). The generic extractor reads almost // nothing of a 2014 user page: its content sits in many small "pods" (a // heading box and a body box, stats as
pairs, a sidebar column) and the // densest-block pick keeps one of them. Here every pod is read, in page order, // as "Heading" then its lines. // // kind itemId url author // ng-user none (each capture a version) — the subdomain // ng-art ng-art-- https://www.newgrounds.com/art/view/… the URL's user // ng-movie ng-movie- https://www.newgrounds.com/portal/view/ the first author link // // Two layouts are read: the 2012–2017 pods (`.pod` holding `.pod-head` and // `.pod-body`) and the older side-by-side boxes (`.podtop` then // `.podcontent`). A page with nothing readable is ONE whole-page draft, // `noContent`, with no item id (a later shell must not overwrite a good // capture's record). Shapes are synthetic as of 2026-11-02 (no live page was // read): the raw capture is kept, so a better reading is a `reextract-pages`. import type { PageExtractInput, PageExtractor, PageRecordDraft } from "../types"; import type { PostMedia } from "../../posts"; import { absoluteUrl, findAll, findFirst, hasClass, isEl, isoFromIso, metaContent, pageTitle, parseEnglishDate, parseHtml, pushUnique, renderBlock, squash, textOf, unwrapWaybackHtml, type HtmlElement, } from "./util"; export const NEWGROUNDS_EXTRACTOR_NAME = "newgrounds"; export const NEWGROUNDS_EXTRACTOR_VERSION = 1; // Subdomains that are Newgrounds' own services, not a user's page. const NOT_USERS = new Set(["www", "art", "uploads", "css", "img", "js", "aicon", "uimg", "picon", "chat", "store", "wiki", "m", "api", "audio", "games", "portal"]); export type NewgroundsPage = | { kind: "ng-user"; user: string } | { kind: "ng-art"; user: string; slug: string } | { kind: "ng-movie"; id: string }; const isNgHost = (host: string) => host === "newgrounds.com" || host.endsWith(".newgrounds.com"); // What a Newgrounds URL is a page of, or null (a page this extractor leaves to the generic one). export function newgroundsPageOf(url: URL): NewgroundsPage | null { const host = url.hostname.toLowerCase(); if (!isNgHost(host)) return null; const path = url.pathname.replace(/\/+$/, "") || "/"; if (host === "www.newgrounds.com" || host === "newgrounds.com") { const art = /^\/art\/view\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)$/.exec(path); if (art) return { kind: "ng-art", user: art[1].toLowerCase(), slug: art[2].toLowerCase() }; const movie = /^\/portal\/view\/(\d+)$/.exec(path); if (movie) return { kind: "ng-movie", id: movie[1] }; return null; } const sub = host.slice(0, -".newgrounds.com".length); if (!sub || sub.includes(".") || NOT_USERS.has(sub)) return null; // The user page itself (its root, or the old /profile path). if (path === "/" || path === "/profile") return { kind: "ng-user", user: sub }; return null; } // --- the pods ------------------------------------------------------------------------------------ type Pod = { heading: string; body: HtmlElement }; const isHeading = (e: HtmlElement) => /^h[1-6]$/.test(e.tag); // Every pod of the page, in order: a `.pod` with its head and body, or a // `.podtop` heading followed (as a sibling) by its `.podcontent`. function podsOf(root: HtmlElement): Pod[] { const out: Pod[] = []; const seen = new Set(); const walk = (el: HtmlElement) => { const kids = el.children.filter(isEl); for (let i = 0; i < kids.length; i++) { const c = kids[i]; if (hasClass(c, "pod")) { const head = findFirst(c, (e) => hasClass(e, "pod-head") || hasClass(e, "podtop")); const body = findFirst(c, (e) => hasClass(e, "pod-body") || hasClass(e, "podcontent")); if (body && !seen.has(body)) { seen.add(body); out.push({ heading: head ? textOf(findFirst(head, isHeading) ?? head) : "", body }); continue; } } if (hasClass(c, "podtop")) { const next = kids[i + 1]; if (next && hasClass(next, "podcontent") && !seen.has(next)) { seen.add(next); out.push({ heading: textOf(findFirst(c, isHeading) ?? c), body: next }); i++; continue; } } if (hasClass(c, "pod-body") || hasClass(c, "podcontent")) { if (!seen.has(c)) { seen.add(c); out.push({ heading: "", body: c }); } continue; } walk(c); } }; walk(root); return out; } // A
's pairs as "Term: value" lines (a term with several values joins them). function dlLines(dl: HtmlElement): string[] { const lines: string[] = []; let term = ""; let values: string[] = []; const flush = () => { if (term || values.length) lines.push(term ? `${term.replace(/:$/, "")}: ${values.join(" ")}`.trim() : values.join(" ")); term = ""; values = []; }; for (const c of dl.children) { if (!isEl(c)) continue; if (c.tag === "dt") { flush(); term = textOf(c); } else if (c.tag === "dd") { const lis = findAll(c, (e) => e.tag === "li"); const v = lis.length ? lis.map((li) => textOf(li)).filter(Boolean).join(", ") : textOf(c).replace(/^\|\s*/, ""); if (v) values.push(v); } } flush(); return lines; } type Read = { text: string; links: string[]; media: PostMedia[] }; // A pod body's lines: its
s as pairs, the rest as the shared renderer writes it. function readBody(body: HtmlElement, base: string): Read { const dls = findAll(body, (e) => e.tag === "dl"); const rest = renderBlock(body, base, (e) => e.tag === "dl"); const lines = rest.text ? [rest.text] : []; for (const dl of dls) lines.push(...dlLines(dl)); return { text: lines.join("\n").trim(), links: rest.links, media: rest.media }; } function addRead(into: Read, r: Read) { for (const l of r.links) pushUnique(into.links, l); for (const m of r.media) if (!into.media.some((x) => x.url === m.url && x.kind === m.kind)) into.media.push(m); } // The value of a sidestats term ("Uploaded" → "Mar 3, 2014 3:22 PM EST"). function sideStat(root: HtmlElement, term: RegExp): string | undefined { for (const dl of findAll(root, (e) => e.tag === "dl")) { const kids = dl.children.filter(isEl); for (let i = 0; i < kids.length; i++) { if (kids[i].tag !== "dt" || !term.test(textOf(kids[i]))) continue; const vals: string[] = []; for (let j = i + 1; j < kids.length && kids[j].tag === "dd"; j++) vals.push(textOf(kids[j]).replace(/^\|\s*/, "")); const v = squash(vals.join(" ")); if (v) return v; } } return undefined; } // The users an item credits: links to .newgrounds.com in its author boxes. function creditedUsers(root: HtmlElement, base: string): string[] { const boxes = findAll(root, (e) => hasClass(e, "item-user") || hasClass(e, "authorlinks") || hasClass(e, "item-details-main")); const out: string[] = []; for (const box of boxes) { for (const a of findAll(box, (e) => e.tag === "a")) { const href = absoluteUrl(a.attrs.href, base); if (!href) continue; try { const p = newgroundsPageOf(new URL(href)); if (p?.kind === "ng-user") pushUnique(out, p.user); } catch { // not a URL } } } return out; } const stripSiteTitle = (t: string | undefined) => t ?.replace(/\s*(?:[|—–-]\s*)?Newgrounds\.com\s*(?:[|—–-]\s*)?/i, " ") .replace(/\s+on Newgrounds$/i, "") .trim() || undefined; // --- the drafts ------------------------------------------------------------------------------------ function noContent(page: NewgroundsPage | null, author: string | undefined): PageRecordDraft[] { return [{ kind: page?.kind ?? "ng-user", ...(author ? { author } : {}), text: "This capture holds no readable Newgrounds content.", links: [], noContent: true, }]; } function extractUser(root: HtmlElement, base: string, user: string): PageRecordDraft[] { const pods = podsOf(root); const name = textOf(findFirst(root, (e) => hasClass(e, "user-header-name"))) || undefined; const all: Read = { text: "", links: [], media: [] }; const blocks: string[] = []; for (const p of pods) { const r = readBody(p.body, base); addRead(all, r); if (!r.text) continue; blocks.push(p.heading ? `${p.heading}\n${r.text}` : r.text); } // The header's own links (News, Movies, Art, Fans) are furniture: only the pods are read. if (blocks.length === 0) return noContent({ kind: "ng-user", user }, user); const draft: PageRecordDraft = { kind: "ng-user", author: user, title: stripSiteTitle(pageTitle(root)) ?? metaContent(root, "og:title") ?? user, text: [name && name.toLowerCase() !== user ? name : "", ...blocks].filter(Boolean).join("\n\n"), links: all.links, }; if (name) draft.authorName = name; if (all.media.length) draft.media = all.media; return [draft]; } function extractItem(root: HtmlElement, base: string, page: Extract): PageRecordDraft[] { const title = textOf(findFirst(root, (e) => e.attrs.itemprop === "name" && isHeading(e))) || metaContent(root, "og:title") || stripSiteTitle(pageTitle(root)) || undefined; const comments = findFirst(root, (e) => e.attrs.id === "author_comments"); const credited = creditedUsers(root, base); const author = page.kind === "ng-art" ? page.user : credited[0]; const read: Read = { text: "", links: [], media: [] }; const lines: string[] = []; if (title) lines.push(title); if (comments) { const r = readBody(comments, base); addRead(read, r); if (r.text) lines.push(r.text); } else { const d = metaContent(root, "og:description") ?? metaContent(root, "description"); if (d) lines.push(d); } if (credited.length > 1) lines.push(`Credits: ${credited.join(", ")}`); // The work itself: an art page's image, a movie's embedded file. for (const img of findAll(root, (e) => e.tag === "img" && !!e.attrs.src)) { const inImage = findFirst(root, (e) => (hasClass(e, "image") || e.attrs.itemprop === "image") && findFirst(e, (x) => x === img) !== null); if (!inImage) continue; const src = absoluteUrl(img.attrs.src, base); if (src && !read.media.some((m) => m.url === src)) read.media.push({ kind: "image", url: src, ...(img.attrs.alt ? { name: img.attrs.alt } : {}) }); } for (const e of findAll(root, (x) => (x.tag === "embed" || x.tag === "object") && !!(x.attrs.src ?? x.attrs.data))) { const src = absoluteUrl(e.attrs.src ?? e.attrs.data, base); if (src) pushUnique(read.links, src); } const tagsDl = findAll(root, (e) => e.tag === "ul" && hasClass(e, "tags")); const tags: string[] = []; for (const ul of tagsDl) for (const li of findAll(ul, (e) => e.tag === "li")) pushUnique(tags, textOf(li).toLowerCase()); const uploaded = sideStat(root, /^(uploaded|date added)/i); const publishedAt = isoFromIso(metaContent(root, "datePublished")) ?? (uploaded ? parseEnglishDate(uploaded) : undefined); if (!title && !comments) return noContent(page, author); const draft: PageRecordDraft = { kind: page.kind, itemId: page.kind === "ng-art" ? `ng-art-${page.user}-${page.slug}` : `ng-movie-${page.id}`, url: page.kind === "ng-art" ? `https://www.newgrounds.com/art/view/${page.user}/${page.slug}` : `https://www.newgrounds.com/portal/view/${page.id}`, text: lines.join("\n"), links: read.links, }; if (title) draft.title = title; if (author) draft.author = author; if (publishedAt) draft.publishedAt = publishedAt; if (read.media.length) draft.media = read.media; if (tags.length) draft.tags = tags; return [draft]; } export function extractNewgrounds(input: PageExtractInput): PageRecordDraft[] { const html = unwrapWaybackHtml(input.html); const base = input.capture.originalUrl; let page: NewgroundsPage | null = null; try { page = newgroundsPageOf(new URL(base)); } catch { page = null; } const root = parseHtml(html); if (!page) return noContent(null, undefined); return page.kind === "ng-user" ? extractUser(root, base, page.user) : extractItem(root, base, page); } export const newgroundsExtractor: PageExtractor = { name: NEWGROUNDS_EXTRACTOR_NAME, version: NEWGROUNDS_EXTRACTOR_VERSION, matches(originalUrl) { return newgroundsPageOf(originalUrl) !== null; }, extract: extractNewgrounds, };