browser-search icon indicating copy to clipboard operation
browser-search copied to clipboard

smart-extract: avoid reloading the page for snapshot/text fallbacks; Readability fails on Trusted Types CSP

Open Johell1NS opened this issue 1 month ago • 1 comments

Problem

smart-extract (Camofox path, no --expr) can load the same page up to three times when Readability returns too little text:

  1. camofox.mjs readability opens a tab, runs Readability and (if empty) takes a snapshot in that tab;
  2. camofox.mjs snapshot reloads the page just to take a snapshot again;
  3. tab open + tab evaluate document.body.innerText + tab close loads it a third time (three Node processes).

Each reload costs 3–6 s on a Raspberry Pi, and the snapshot and innerText are already available in the first tab for ~1–2 s.

Separately, Readability throws on sites with a Trusted Types CSP (e.g. YouTube): Element.innerHTML setter: Sink type mismatch violation blocked by CSP. The whole readability call then fails, the tab is closed and everything is reloaded.

Proposed change

  • readability() (client): create a permissive default Trusted Types policy in the extraction tab before running Readability (wrapped in try, so CSPs that forbid it behave as before).
  • camofox.mjs readability <url…> [--min-chars N]: each step is guarded by its own try/catch; if the text is missing or shorter than N, take the snapshot and innerText in the same tab and return them. Without --min-chars the output is unchanged.
  • smart-extract: evaluate readability → snapshot → rendered text from that single load, keeping the same source values. Reload (the old steps 2–3) only if the first attempt failed outright or the poor content looks like a block (HTTP ≥ 400 or an error page), so a challenge can still be retried before escalating to Cloak.

Measured (Raspberry Pi 5, before → after)

URL before after
x.com/NASA 7.3 s 4.1 s
youtube.com 13.2 s 8.0 s
amazon.com 8.8 s 4.8 s

Pages where Readability already succeeds are unchanged (same source and character counts).

Johell1NS avatar Oct 21 '26 10:10 Johell1NS

Released in v3.2.0.

Johell1NS avatar Oct 21 '26 12:10 Johell1NS