Documentation
¶
Index ¶
- func EvaluateFunction[T any](ctx context.Context, b *Browser, fnName string, args ...any) (T, error)
- func EvaluateGeneric[T any](ctx context.Context, b *Browser, script string) (T, error)
- type Browser
- func (b *Browser) Click(ctx context.Context, opts ClickOptions) error
- func (b *Browser) Close() error
- func (b *Browser) Evaluate(ctx context.Context, script string, target any) error
- func (b *Browser) GetURL(ctx context.Context) (string, error)
- func (b *Browser) IsRunning() bool
- func (b *Browser) Navigate(ctx context.Context, targetURL string) error
- func (b *Browser) NavigateWait(ctx context.Context, targetURL string, waitSelector string) error
- func (b *Browser) Run(ctx context.Context, actions ...chromedp.Action[chromedp.Void]) error
- func (b *Browser) Screenshot(ctx context.Context, opts ScreenshotOptions) ([]byte, string, error)
- func (b *Browser) SetConsoleHandler(handler ConsoleHandler)
- func (b *Browser) Start(ctx context.Context) error
- type ClickOptions
- type Config
- type ConsoleHandler
- type ConsoleMessage
- type ScreenshotOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser provides an automated headless/display browser controller based on Chrome DevTools Protocol (CDP).
func (*Browser) Click ¶
func (b *Browser) Click(ctx context.Context, opts ClickOptions) error
Click triggers a mouse click on an element located by selector or at specified (X, Y) coordinates.
func (*Browser) Close ¶
Close terminates the Chrome process, cancels context resources, and cleans up any temporary user-data directory.
func (*Browser) Evaluate ¶
Evaluate runs a JavaScript expression and decodes the result into the target destination pointer.
func (*Browser) IsRunning ¶
IsRunning reports whether the browser process is currently active and accepting commands.
func (*Browser) Navigate ¶
Navigate loads the specified URL into the active browser page and waits for the body DOM ready state.
func (*Browser) NavigateWait ¶
NavigateWait loads the specified URL into the active browser page and waits until waitSelector is ready.
func (*Browser) Screenshot ¶
Screenshot captures an image of the current page based on the given ScreenshotOptions. Returns the image binary data, the MIME content type ("image/png" or "image/jpeg"), and any error.
func (*Browser) SetConsoleHandler ¶
func (b *Browser) SetConsoleHandler(handler ConsoleHandler)
SetConsoleHandler registers a callback function to receive browser console events in real-time.
type ClickOptions ¶
type ClickOptions struct {
// Selector is an optional CSS query selector to locate and click an element.
Selector string `json:"selector"`
// WaitVisible instructs the engine to wait until the element is visible before clicking.
WaitVisible bool `json:"wait_visible"`
// X specifies the horizontal coordinate in pixels when clicking by coordinates (used when Selector is empty).
X float64 `json:"x"`
// Y specifies the vertical coordinate in pixels when clicking by coordinates (used when Selector is empty).
Y float64 `json:"y"`
// Button specifies the mouse button: "left" (default), "middle", or "right".
Button string `json:"button"`
// ClickCount specifies the number of clicks (default is 1; 2 for double-click).
ClickCount int `json:"click_count"`
}
ClickOptions specifies target selection, coordinates, and behavior for mouse click operations.
type Config ¶
type Config struct {
// Address specifies the remote Chrome DevTools Protocol debugging endpoint (<host>:<port> or ws:// / http:// URL).
// When non-empty, Browser connects to the remote endpoint instead of launching a local Chrome process.
Address string `toml:"address"`
// StartURL specifies the initial landing page URL navigated to on browser startup and restart.
StartURL string `toml:"start_url"`
// DefaultURL is an alias for StartURL.
DefaultURL string `toml:"default_url"`
// Headless determines whether Chrome/Chromium runs without a graphical user interface.
Headless bool `toml:"headless"`
// Kiosk enables full-screen kiosk mode for dedicated display environments.
Kiosk bool `toml:"kiosk"`
// WindowWidth specifies the initial viewport and window width in pixels.
WindowWidth int `toml:"window_width"`
// WindowHeight specifies the initial viewport and window height in pixels.
WindowHeight int `toml:"window_height"`
// UserDataDir specifies the profile/user data directory path.
// If left empty, a temporary directory is created on startup and removed on close.
UserDataDir string `toml:"user_data_dir"`
// IgnoreCertErrors allows ignoring TLS certificate validation errors (useful for development/self-signed certs).
IgnoreCertErrors bool `toml:"ignore_cert_errors"`
// Timeout specifies the default timeout duration for browser operations.
Timeout time.Duration `toml:"timeout"`
// ExecPath allows providing a custom path to the Chrome/Chromium executable binary.
ExecPath string `toml:"exec_path"`
// CustomFlags allows passing additional custom command-line flags to the Chrome process.
CustomFlags []string `toml:"custom_flags"`
}
Config contains configuration options for initializing and running the browser instance.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config struct populated with recommended default settings.
func (Config) GetStartURL ¶
GetStartURL returns the configured startup URL, prioritizing StartURL over DefaultURL.
type ConsoleHandler ¶
type ConsoleHandler func(msg ConsoleMessage)
ConsoleHandler is a callback function invoked when a console message is received from the browser.
type ConsoleMessage ¶
type ConsoleMessage struct {
// Level indicates the severity level of the console entry (e.g. "log", "warn", "error", "info").
Level string `json:"level"`
// Message contains the concatenated text arguments of the console call.
Message string `json:"message"`
// Timestamp records the time when the console event occurred.
Timestamp time.Time `json:"timestamp"`
}
ConsoleMessage represents a browser console log event captured from JavaScript execution.
type ScreenshotOptions ¶
type ScreenshotOptions struct {
// Format specifies the image encoding ("png" or "jpeg"). Defaults to "png" if empty and Quality is 0.
Format string `json:"format"`
// Quality defines the compression quality for JPEG output (1-100).
Quality int `json:"quality"`
// FullPage determines whether to capture the full scrollable document height or only current viewport.
FullPage bool `json:"full_page"`
// Width defines an optional target width for image downscaling/resizing.
Width int `json:"width"`
// Height defines an optional target height for image downscaling/resizing.
Height int `json:"height"`
// MaxWidth defines an optional maximum bounding width for aspect-ratio preserving fit.
MaxWidth int `json:"max_width"`
// MaxHeight defines an optional maximum bounding height for aspect-ratio preserving fit.
MaxHeight int `json:"max_height"`
// SharpenSigma defines the Gaussian sharpening sigma factor applied after resizing (e.g. 0.5 - 1.5).
SharpenSigma float64 `json:"sharpen_sigma"`
}
ScreenshotOptions configures screenshot capture format, dimensions, and image processing.