diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-08 21:24:32 +0530 |
|---|---|---|
| committer | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-08 21:24:32 +0530 |
| commit | 68bf7fd2988e42343833be09e4115cd7175029d0 (patch) | |
| tree | 52b8eaf99fa7e4d383ff169dd3bdccfe13911217 /src/domains/readpage/capture.ts | |
| parent | c5c42df5976ed63faac2adcf3b04a4ad2f519b32 (diff) | |
feat: add browser_read_page reader-mode tool
URL (or owned targetId) → clean main-article text with nav/ads/footer
boilerplate stripped, via a dependency-free readability heuristic. DOM walk
lives in an in-page capture expression; scoring/selection is a pure, fixture-
tested function (link-density + boilerplate-ancestor filtering, body-text
fallback for structure-less pages). Reuses the isolated-attach lifecycle;
registered unserialized (safe concurrent read). 10 extractor unit scenarios.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/readpage/capture.ts')
| -rw-r--r-- | src/domains/readpage/capture.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/domains/readpage/capture.ts b/src/domains/readpage/capture.ts new file mode 100644 index 0000000..1ee4500 --- /dev/null +++ b/src/domains/readpage/capture.ts @@ -0,0 +1,46 @@ +/** + * The JavaScript source evaluated in a loaded page to capture candidate content + * blocks + metadata for the pure readability extractor (readability.ts). Crosses + * the CDP boundary via Runtime.evaluate (returnByValue) and returns a JSON + * string shaped like PageCapture. + * + * The DOM walk lives here (unavoidable); all scoring/selection is pure and lives + * in readability.ts, so the decision logic stays unit-testable. + */ + +/** Max bytes of body-text fallback captured for thin/structure-less pages. */ +const BODY_TEXT_LIMIT = 20_000; + +/** Build the capture expression. No interpolated values ⇒ no injection surface. */ +export const buildPageCaptureExpr = (): string => ` + (() => { + const BOILERPLATE = new Set(['NAV', 'HEADER', 'FOOTER', 'ASIDE']); + const inBoilerplate = (el) => { + for (let n = el; n; n = n.parentElement) { + if (BOILERPLATE.has(n.tagName)) return true; + const role = n.getAttribute && n.getAttribute('role'); + if (role === 'navigation' || role === 'banner' || role === 'contentinfo') return true; + } + return false; + }; + const kindOf = (tag) => { + if (/^H[1-6]$/.test(tag)) return 'heading'; + if (tag === 'LI') return 'listitem'; + if (tag === 'BLOCKQUOTE') return 'blockquote'; + if (tag === 'P') return 'paragraph'; + return 'other'; + }; + const selector = 'p, h1, h2, h3, h4, h5, h6, li, blockquote'; + const root = document.querySelector('article, main, [role=main]') || document.body; + const blocks = []; + for (const el of root.querySelectorAll(selector)) { + const text = (el.innerText || '').replace(/\\s+/g, ' ').trim(); + if (!text) continue; + let linkTextLength = 0; + for (const a of el.querySelectorAll('a')) linkTextLength += (a.innerText || '').length; + blocks.push({ kind: kindOf(el.tagName), text, linkTextLength, inBoilerplate: inBoilerplate(el) }); + } + const bodyText = (document.body.innerText || '').replace(/\\n{3,}/g, '\\n\\n').slice(0, ${BODY_TEXT_LIMIT}); + return JSON.stringify({ url: location.href, title: document.title, blocks, bodyText }); + })() +`; |
