Documentation
¶
Overview ¶
Package browser provides Chrome detection, launch, and target management.
Index ¶
- Constants
- Variables
- func FindChrome() (string, error)
- type Browser
- func (b *Browser) Close() error
- func (b *Browser) PID() int
- func (b *Browser) PageTarget(ctx context.Context) (*Target, error)
- func (b *Browser) Port() int
- func (b *Browser) Targets(ctx context.Context) ([]Target, error)
- func (b *Browser) Version(ctx context.Context) (*VersionInfo, error)
- func (b *Browser) WebSocketURL(ctx context.Context) (string, error)
- type LaunchOptions
- type Target
- type VersionInfo
Constants ¶
const DefaultPort = 9222
DefaultPort is the default CDP debugging port.
const UserDataDirDefault = "default"
UserDataDirDefault is the special value that means "use the user's Chrome profile".
Variables ¶
var ErrBrowserClosed = errors.New("browser is closed")
ErrBrowserClosed is returned when operating on a closed browser.
var ErrChromeNotFound = errors.New("chrome not found")
ErrChromeNotFound is returned when no Chrome binary can be located.
var ErrNoPageTarget = errors.New("no page target found")
ErrNoPageTarget is returned when no page target is available.
var ErrPortInUse = errors.New("port is already in use")
ErrPortInUse is returned when the requested port is already in use.
var ErrStartTimeout = errors.New("browser start timeout")
ErrStartTimeout is returned when the browser fails to start in time.
var ErrSystemProfileInUse = errors.New("system Chrome profile is already in use by another browser instance")
ErrSystemProfileInUse is returned when --system-profile is selected but the launched browser exits before CDP comes up, which typically means another Chrome instance already holds the default profile and the new launch forwarded to it (without remote debugging) and quit.
Functions ¶
func FindChrome ¶
FindChrome searches for a Chrome or Chromium binary on the system. It first checks the WEBCTL_CHROME environment variable, then searches common installation paths for the current platform. Returns the path to the executable or ErrChromeNotFound.
Types ¶
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser represents a running Chrome instance with CDP enabled.
func Start ¶
func Start(opts LaunchOptions) (*Browser, error)
Start launches a new Chrome browser with CDP enabled. It waits for the CDP endpoint to become available before returning.
func StartWithBinary ¶
func StartWithBinary(binPath string, opts LaunchOptions) (*Browser, error)
StartWithBinary launches Chrome using the specified binary path.
func (*Browser) PageTarget ¶
PageTarget returns the first page-type target.
type LaunchOptions ¶
type LaunchOptions struct {
// Headless runs the browser without a visible window.
Headless bool
// Port for CDP remote debugging. If 0, uses default 9222.
Port int
// UserDataDir specifies the browser profile directory.
// Special values:
// - Empty string: create a temporary directory (default)
// - "default": use the user's default Chrome profile
// - Any path: use that directory
UserDataDir string
}
LaunchOptions configures browser launch behavior.
type Target ¶
type Target struct {
ID string `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description,omitempty"`
WebSocketURL string `json:"webSocketDebuggerUrl"`
}
Target represents a CDP target (page, worker, etc).
func FetchTargets ¶
FetchTargets retrieves the list of available targets from the CDP endpoint. Uses http.DefaultClient which has no timeout; callers must provide a context with timeout. This is acceptable for local CDP calls where network issues are rare.
func FindPageTarget ¶
FindPageTarget returns the first page-type target from the list.
type VersionInfo ¶
type VersionInfo struct {
Browser string `json:"Browser"`
ProtocolVer string `json:"Protocol-Version"`
UserAgent string `json:"User-Agent"`
V8Version string `json:"V8-Version"`
WebKitVersion string `json:"WebKit-Version"`
WebSocketURL string `json:"webSocketDebuggerUrl"`
}
VersionInfo contains browser version information from /json/version.
func FetchVersion ¶
FetchVersion retrieves browser version info from the CDP endpoint. Uses http.DefaultClient which has no timeout; callers must provide a context with timeout. This is acceptable for local CDP calls where network issues are rare.