pagecapture

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package pagecapture captures normalized, reusable evidence about web pages — rendered HTML, a screenshot, visible text, and structural signals (title, language, links, headings) — using a w3pilot browser session. It is domain-neutral: accessibility evaluation, functional/journey testing, i18n checks, and security review all consume the same PageEvidence rather than each driving the browser themselves.

Index

Constants

View Source
const (
	MaxHTMLBytes = 200_000
	MaxTextBytes = 40_000
)

Bounds on captured text to keep evidence bundles a sane size.

Variables

This section is empty.

Functions

This section is empty.

Types

type CrawlOptions

type CrawlOptions struct {
	Options
	// MaxPages caps how many pages to capture (including the start page).
	MaxPages int
	// SameHostOnly restricts the crawl to the start URL's host.
	SameHostOnly bool
}

CrawlOptions configure a site capture.

func DefaultCrawlOptions

func DefaultCrawlOptions() CrawlOptions

DefaultCrawlOptions returns defaults: up to 5 same-host pages.

type Heading

type Heading struct {
	Level int    `json:"level"`
	Text  string `json:"text"`
}

Heading is a heading element captured from the page.

type Link struct {
	Text string `json:"text"`
	Href string `json:"href"`
}

Link is an anchor captured from the page.

type Options

type Options struct {
	// LoadWait bounds how long to wait for the page to settle. The wait is
	// best-effort: capture proceeds against the rendered DOM regardless.
	LoadWait time.Duration
	// MinSettle is the minimum time to wait before accepting a stable DOM.
	// SPAs render in phases (shell, then async data) with quiet gaps between;
	// this floor prevents settling in an early gap before data arrives.
	// Defaults to 2.5s when zero.
	MinSettle time.Duration
	// Settle selects the readiness strategy (default SettleDOMStable).
	Settle SettleMode
	// Screenshot toggles screenshot capture (the most expensive part).
	Screenshot bool
}

Options configure a capture.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults: DOM-stability settle, screenshot on.

type PageEvidence

type PageEvidence struct {
	URL      string `json:"url"`
	Title    string `json:"title"`
	Language string `json:"language,omitempty"` // <html lang>

	// HTML is the rendered outer HTML, bounded to MaxHTMLBytes.
	HTML string `json:"html,omitempty"`
	// Text is the visible body text, bounded to MaxTextBytes.
	Text string `json:"text,omitempty"`
	// ScreenshotPNG is a full-page screenshot; base64-encoded when serialized.
	ScreenshotPNG []byte `json:"screenshotPng,omitempty"`

	Links    []Link    `json:"links,omitempty"`
	Headings []Heading `json:"headings,omitempty"`

	// Truncated reports whether HTML/Text were clipped to their bounds.
	Truncated bool `json:"truncated,omitempty"`
	// LoadWarning is set when the page-load wait did not fully settle.
	LoadWarning string    `json:"loadWarning,omitempty"`
	CapturedAt  time.Time `json:"capturedAt"`
}

PageEvidence is normalized evidence for a single page.

func Capture

func Capture(ctx context.Context, pilot *w3pilot.Pilot, pageURL string, opts Options) (*PageEvidence, error)

Capture navigates to pageURL, waits for it to settle, and captures evidence.

func CaptureCurrent

func CaptureCurrent(ctx context.Context, pilot *w3pilot.Pilot, pageURL string, screenshot bool) *PageEvidence

CaptureCurrent captures evidence from the page the Pilot is *already* on, without navigating or settling. Use it when the caller has already loaded and settled the page (e.g. an audit engine that just ran against it), to avoid a redundant navigation. pageURL labels the evidence.

func CaptureURL

func CaptureURL(ctx context.Context, pageURL string, opts Options) (*PageEvidence, error)

CaptureURL launches a headless browser, captures one page, and closes it. Convenience for callers that don't already hold a Pilot.

func (PageEvidence) HasScreenshot

func (p PageEvidence) HasScreenshot() bool

HasScreenshot reports whether a screenshot was captured (used by consumers to decide whether visual criteria can be judged).

type SettleMode

type SettleMode int

SettleMode selects how Capture waits for a page to be ready.

const (
	// SettleDOMStable (default) polls the DOM until it stops changing and has
	// real content. This is the robust choice for client-rendered SPAs, whose
	// "load" event fires before content exists and whose network may never idle.
	SettleDOMStable SettleMode = iota
	// SettleLoad waits only for the "load" event. Fine for static pages.
	SettleLoad
	// SettleNone captures immediately after navigation.
	SettleNone
)

type SiteEvidence

type SiteEvidence struct {
	StartURL string         `json:"startUrl"`
	Pages    []PageEvidence `json:"pages"`
}

SiteEvidence is evidence captured across multiple pages of one site.

func CaptureSite

func CaptureSite(ctx context.Context, pilot *w3pilot.Pilot, startURL string, opts CrawlOptions) (*SiteEvidence, error)

CaptureSite crawls from startURL, breadth-first, capturing evidence for each page up to MaxPages. It reuses one Pilot for all pages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL