Skip to content

https://arxiv.org/abs/2606.28344. The end of web parsing. The beginning of scalable pixel-native search. link: https://pixelrag.ai/

3.8 KB
Raw Blame
"""Shared page measurement for the capture backends: the in-page JS, and the
judgement about whether what it measured can be trusted.

Kept in one place because both backends measure page height the same way and
must agree: the standard (``cdp``) and turbo (``fast_cdp``) paths each embed
this snippet, and a divergence between them silently changes how much of a page
gets captured depending on which Chrome binary is installed. The same goes for
what they then report in ``tiles.json``.

No imports — a plain string constant and a pure function, so either backend can
use them without a dependency edge between them.
"""

# How tall is the document's *content*?
#
# `documentElement.scrollHeight` alone over-reports: padding on the root element
# or a trailing margin inflates it, buying a run of blank tiles at the bottom of
# every such page. So it is clamped to where the content actually ends.
#
# That bound has to be measured from the content, not from the body box. A body
# is only as tall as the document when the page lets it size to its content;
# sites that pin it to the viewport (`html, body { height: 100% }` — Wikipedia's
# Vector 2022 skin among them) leave the content overflowing a one-viewport box,
# and clamping to that box truncates a 20,000px article to a single tile
# (issue #124). Taking the lowest edge among the body and its element children
# reads the same on a self-sizing body and survives a pinned one.
#
# Coordinates are viewport-relative, so scroll offset is added back: a page
# navigated to a `#fragment` lands scrolled down, where a raw rect bottom would
# under-report by exactly the scrolled distance.
CONTENT_BOTTOM_JS = """
    function contentBottom(body) {
        const offset = window.scrollY || window.pageYOffset || 0;
        let bottom = body.getBoundingClientRect().bottom;
        for (let el = body.firstElementChild; el; el = el.nextElementSibling) {
            const r = el.getBoundingClientRect();
            // Skip elements with no box at all (display:none, empty <script>);
            // theirs is a zero rect at the origin and would not move `bottom`,
            // but skipping keeps the intent explicit.
            if (r.width > 0 || r.height > 0) {
                bottom = Math.max(bottom, r.bottom);
            }
        }
        return Math.ceil(bottom + offset);
    }
"""


def truncation_reason(
    page_height: int, tile_height: int, *, measured: bool
) -> str | None:
    """Why this capture must not be reported as complete — or ``None`` if it may.

    ``tile_height`` is also the emulated viewport height (both backends pass it
    to ``Emulation.setDeviceMetricsOverride``), which is what makes the two
    checks below meaningful:

    - The probe returned nothing usable, so the height fell back to the tile
      height. Whatever the page actually was, one viewport of it was captured.
    - The probe returned exactly the viewport height. Then it measured the
      viewport rather than the content, and everything below the fold is
      missing — the shape every truncation so far has taken (issues #124, #131,
      #133), independent of which of them caused it.

    The second check costs a false alarm on a page that happens to be exactly
    ``tile_height`` tall. That is the right trade against the alternative,
    which is reporting 5% of an article as the whole of it: a capture wrongly
    flagged is re-run, a truncation never flagged is read and summarised.
    """
    if not measured:
        return (
            f"the page-height probe returned nothing, so the height fell back "
            f"to the {tile_height}px tile height"
        )
    if page_height == tile_height:
        return (
            f"the measured page height ({page_height}px) is exactly the tile "
            f"height, so the probe tracked the emulated viewport, not the content"
        )
    return None