File size: 3,339 Bytes
bee9bbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Bounded browser settling for lazy-loaded offline clone pages."""

from __future__ import annotations

import time
from typing import Any


_OBSERVE_SCRIPT = """
() => {
  const root = document.documentElement;
  const images = [...document.images];
  const failedImages = images
    .filter((image) => image.complete && image.naturalWidth === 0)
    .map((image) => image.currentSrc || image.src || image.alt || "unknown");
  const scrollHeight = root.scrollHeight;
  const atBottom = window.scrollY + window.innerHeight >= scrollHeight - 2;
  if (!atBottom) window.scrollBy(0, Math.max(1, window.innerHeight - 24));
  return {
    scroll_height: scrollHeight,
    section_count: document.querySelectorAll("main section, main article, main h2").length,
    image_count: images.length,
    loaded_image_count: images.filter((image) => image.complete && image.naturalWidth > 0).length,
    failed_images: failedImages,
    at_bottom: atBottom,
  };
}
"""


def _signature(observation: dict[str, Any]) -> tuple[Any, ...]:
    return (
        observation.get("scroll_height"),
        observation.get("section_count"),
        observation.get("image_count"),
        observation.get("loaded_image_count"),
        tuple(observation.get("failed_images") or ()),
    )


def settle_page(
    page: Any,
    *,
    max_rounds: int = 24,
    timeout_ms: int = 20_000,
    pause_ms: int = 100,
) -> dict[str, Any]:
    """Scroll and wait for a bounded, fully loaded page state.

    Completion requires two consecutive bottom observations with identical
    geometry/counts and every image loaded successfully. The return value is
    deliberately serializable so scenarios can retain incomplete diagnostics.
    """

    deadline = time.monotonic() + timeout_ms / 1000
    previous_signature: tuple[Any, ...] | None = None
    stable_bottom_observations = 0
    latest: dict[str, Any] = {
        "scroll_height": 0,
        "section_count": 0,
        "image_count": 0,
        "loaded_image_count": 0,
        "failed_images": [],
        "at_bottom": False,
    }

    for _round in range(max_rounds):
        if time.monotonic() >= deadline:
            break
        latest = dict(page.evaluate(_OBSERVE_SCRIPT))
        signature = _signature(latest)
        if latest.get("at_bottom") and signature == previous_signature:
            stable_bottom_observations += 1
        elif latest.get("at_bottom"):
            stable_bottom_observations = 1
        else:
            stable_bottom_observations = 0
        previous_signature = signature

        images_healthy = (
            latest.get("image_count", 0) == latest.get("loaded_image_count", 0)
            and not latest.get("failed_images")
        )
        if stable_bottom_observations >= 2 and images_healthy:
            return {**latest, "complete": True, "incomplete_reason": None}
        if pause_ms:
            page.wait_for_timeout(pause_ms)

    if latest.get("failed_images"):
        reason = "image load failures remain"
    elif latest.get("image_count") != latest.get("loaded_image_count"):
        reason = "image health did not settle"
    elif not latest.get("at_bottom"):
        reason = "document did not reach the bottom"
    else:
        reason = "page geometry did not stabilize"
    return {**latest, "complete": False, "incomplete_reason": reason}