screenshot

package
v0.0.0-...-2fe4d4b Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBlankPage = errors.New("page is blank (about:blank)")

ErrBlankPage is returned when attempting to screenshot a blank page.

View Source
var ErrEmptyScreenshot = errors.New("screenshot appears to be empty or all white")

ErrEmptyScreenshot is returned when screenshot appears to be empty/white.

Functions

func Annotate

func Annotate(imgData []byte, elementMap ElementMapInterface, cfg AnnotationConfig) ([]byte, error)

Annotate draws bounding boxes and labels on a screenshot image. Following browser-use pattern: boxes around interactive elements with index labels.

func AnnotateBrowserUseStyle

func AnnotateBrowserUseStyle(imgData []byte, elementMap ElementMapInterface) ([]byte, error)

AnnotateBrowserUseStyle annotates following browser-use pattern: Only show index labels for elements without text information.

func AnnotateForLLM

func AnnotateForLLM(imgData []byte, elementMap ElementMapInterface) ([]byte, error)

AnnotateForLLM annotates a screenshot optimized for LLM vision consumption. Uses default annotation config with appropriate sizing.

func Capture

func Capture(ctx context.Context, page *rod.Page, opts Options) ([]byte, error)

Capture takes a screenshot of the page and returns compressed bytes. It implements proper page readiness checks following browser-use best practices.

func CaptureAfterAction

func CaptureAfterAction(ctx context.Context, page *rod.Page, maxWidth int) ([]byte, error)

CaptureAfterAction captures a screenshot after an action has been performed. It waits for the page to stabilize after the action before capturing.

func CaptureAfterActionWithAnnotations

func CaptureAfterActionWithAnnotations(ctx context.Context, page *rod.Page, elementMap ElementMapInterface, maxWidth int) ([]byte, error)

CaptureAfterActionWithAnnotations captures an annotated screenshot after an action.

func CaptureElement

func CaptureElement(ctx context.Context, page *rod.Page, selector string, opts Options) ([]byte, error)

CaptureElement takes a screenshot of a specific element.

func CaptureFullPage

func CaptureFullPage(ctx context.Context, page *rod.Page, opts Options) ([]byte, error)

CaptureFullPage takes a screenshot of the entire page.

func CaptureViewport

func CaptureViewport(ctx context.Context, page *rod.Page, opts Options) ([]byte, error)

CaptureViewport takes a screenshot of the current viewport only.

func CaptureWithAnnotations

func CaptureWithAnnotations(ctx context.Context, page *rod.Page, elementMap ElementMapInterface, opts AnnotatedOptions) ([]byte, error)

CaptureWithAnnotations captures a screenshot and draws element annotations. This follows browser-use pattern: bounding boxes around interactive elements with index labels.

func ForLLM

func ForLLM(ctx context.Context, page *rod.Page, maxWidth int) ([]byte, error)

ForLLM captures a screenshot optimized for LLM consumption. Uses JPEG format with reasonable compression for token efficiency. Includes page readiness checks and skips blank pages.

func ForLLMSafe

func ForLLMSafe(ctx context.Context, page *rod.Page, maxWidth int) ([]byte, error)

ForLLMSafe captures a screenshot for LLM consumption with full validation. Returns nil data (not error) if page is blank or screenshot is empty. This is useful for agent loops where blank screenshots should be skipped.

func ForLLMSafeWithAnnotations

func ForLLMSafeWithAnnotations(ctx context.Context, page *rod.Page, elementMap ElementMapInterface, maxWidth int) ([]byte, error)

ForLLMSafeWithAnnotations captures an annotated screenshot, returning nil for blank pages.

func ForLLMWithAnnotations

func ForLLMWithAnnotations(ctx context.Context, page *rod.Page, elementMap ElementMapInterface, maxWidth int) ([]byte, error)

ForLLMWithAnnotations captures an annotated screenshot optimized for LLM vision. Includes bounding boxes around interactive elements with index labels.

func HasContent

func HasContent(img image.Image) bool

HasContent checks if the image has meaningful content (not mostly white).

func IsPageReady

func IsPageReady(page *rod.Page) bool

IsPageReady checks if the page is ready for screenshot capture.

func WaitUntilReady

func WaitUntilReady(ctx context.Context, page *rod.Page, timeout time.Duration) error

WaitUntilReady waits until the page is ready for screenshot, with timeout.

Types

type AnnotatedOptions

type AnnotatedOptions struct {
	Options
	// Annotate enables drawing bounding boxes and labels on the screenshot.
	Annotate bool
	// AnnotationConfig customizes annotation appearance.
	AnnotationConfig *AnnotationConfig
}

AnnotatedOptions extends Options with annotation settings.

func DefaultAnnotatedOptions

func DefaultAnnotatedOptions() AnnotatedOptions

DefaultAnnotatedOptions returns options with annotations enabled.

type AnnotationConfig

type AnnotationConfig struct {
	// BorderWidth is the width of bounding box borders in pixels.
	BorderWidth int

	// FontSize is the approximate height of index labels in pixels.
	FontSize int

	// ShowLabels determines whether to show index labels on elements.
	ShowLabels bool

	// ShowLabelsOnlyForUnlabeled shows labels only for elements without text.
	ShowLabelsOnlyForUnlabeled bool

	// Colors for different element types.
	LinkColor      color.RGBA
	ButtonColor    color.RGBA
	InputColor     color.RGBA
	DefaultColor   color.RGBA
	LabelBgColor   color.RGBA
	LabelTextColor color.RGBA
}

AnnotationConfig configures how annotations are drawn.

func DefaultAnnotationConfig

func DefaultAnnotationConfig() AnnotationConfig

DefaultAnnotationConfig returns sensible defaults for annotations.

type BoundingBoxInfo

type BoundingBoxInfo interface {
	GetX() float64
	GetY() float64
	GetWidth() float64
	GetHeight() float64
	GetIsEmpty() bool
}

BoundingBoxInfo provides bounding box data for annotation.

type ElementInfo

type ElementInfo interface {
	GetIndex() int
	GetTagName() string
	GetRole() string
	GetText() string
	GetBoundingBox() BoundingBoxInfo
	GetIsVisible() bool
}

ElementInfo provides element data needed for annotation.

type ElementMapInterface

type ElementMapInterface interface {
	Len() int
	GetElements() []ElementInfo
}

ElementMapInterface defines the interface for element maps used in annotations. This allows the screenshot package to work with element maps without circular imports.

type Options

type Options struct {
	// MaxWidth is the maximum width for the screenshot.
	// Images wider than this will be resized.
	MaxWidth int

	// Quality is the JPEG quality (1-100).
	// Only used when Format is JPEG.
	Quality int

	// Format is the output format (png or jpeg).
	Format string

	// FullPage captures the entire page, not just the viewport.
	FullPage bool

	// WaitForLoad waits for page load event before capturing.
	WaitForLoad bool

	// WaitForIdle waits for network idle before capturing.
	WaitForIdle bool

	// StabilityTimeout is how long to wait for page stability.
	// Default is 500ms if not set.
	StabilityTimeout time.Duration

	// SkipBlankPages returns ErrBlankPage instead of capturing blank pages.
	SkipBlankPages bool

	// ValidateContent checks if screenshot has actual content (not all white).
	ValidateContent bool
}

Options configures screenshot capture.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults for LLM consumption.

func LLMOptions

func LLMOptions() Options

LLMOptions returns options optimized for LLM vision consumption. Includes page readiness checks and content validation.

type PageInfo

type PageInfo struct {
	URL        string
	Title      string
	IsBlank    bool
	HasContent bool
}

GetPageInfo returns information about the current page state.

func GetPageInfo

func GetPageInfo(page *rod.Page) (*PageInfo, error)

GetPageInfo returns information about the current page state.

Jump to

Keyboard shortcuts

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