headless_browser

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 6 Imported by: 6

README

headless_browser

A simple headless browser library powered by go-rod, with built-in stealth mode support.

Installation

go get github.com/xpzouying/headless_browser

Usage

package main

import (
    "time"
    
    "github.com/xpzouying/headless_browser"
)

func main() {
    // Create browser with default settings (headless mode)
    browser := headless_browser.New()
    defer browser.Close()
    
    // Create a new page
    page := browser.NewPage()
    defer page.Close()
    
    // Navigate to a website
    page.Timeout(30 * time.Second).
        MustNavigate("https://example.com").
        MustWaitStable()
}

Configuration Options

// Run in non-headless mode (visible browser)
browser := headless_browser.New(
    headless_browser.WithHeadless(false),
)

// Set custom user agent
browser := headless_browser.New(
    headless_browser.WithUserAgent("Custom User Agent"),
)

// Set cookies (JSON format)
browser := headless_browser.New(
    headless_browser.WithCookies(`[{"name":"session","value":"abc123","domain":"example.com"}]`),
)

// Combine multiple options
browser := headless_browser.New(
    headless_browser.WithHeadless(false),
    headless_browser.WithUserAgent("Custom User Agent"),
    headless_browser.WithCookies(cookiesJSON),
)

Example

package main

import (
    "time"
    
    "github.com/xpzouying/headless_browser"
)

func main() {
    // Create browser instance
    browser := headless_browser.New(headless_browser.WithHeadless(false))
    defer browser.Close()

    // Create new page with stealth mode
    page := browser.NewPage()
    defer page.Close()

    // Navigate and wait for page to be stable
    page.Timeout(30 * time.Second).
        MustNavigate("https://www.haha.ai").
        MustWaitStable()

    // Additional page operations can be performed here
    time.Sleep(1 * time.Second)
}

Documentation

Overview

Package headless_browser provides a simple headless browser library powered by go-rod with built-in stealth mode support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Browser

type Browser struct {
	// contains filtered or unexported fields
}

Browser represents a headless browser instance with an underlying rod.Browser and launcher.

func New

func New(options ...Option) *Browser

New creates a new Browser instance with the provided options. It initializes a Chrome browser with stealth mode enabled.

func (*Browser) Close

func (b *Browser) Close()

Close closes the browser and cleans up resources.

func (*Browser) NewPage

func (b *Browser) NewPage() *rod.Page

NewPage creates a new page with stealth mode enabled. The returned page can be used to navigate and interact with web content.

type Config

type Config struct {
	Headless      bool   // Whether to run browser in headless mode
	UserAgent     string // Custom user agent string
	Cookies       string // JSON string of cookies to set
	ChromeBinPath string // Custom Chrome/Chromium executable path
	Proxy         string // Proxy server URL (e.g. "http://host:port", "socks5://host:port")

	Trace bool // Whether to enable tracing (not implemented yet)
}

Config holds the configuration options for the browser.

type Option

type Option func(*Config)

Option is a functional option for configuring the browser.

func WithChromeBinPath added in v0.1.0

func WithChromeBinPath(path string) Option

WithChromeBinPath sets a custom Chrome/Chromium executable path. If not set or empty, launcher will auto-detect or download a browser. Common paths:

  • macOS: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  • Linux: "/usr/bin/google-chrome" or "/usr/bin/chromium"
  • Windows: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"

func WithCookies

func WithCookies(cookies string) Option

WithCookies sets cookies for the browser from a JSON string. The cookies should be in the format expected by proto.NetworkCookie.

func WithHeadless

func WithHeadless(headless bool) Option

WithHeadless sets whether the browser should run in headless mode.

func WithProxy added in v0.3.0

func WithProxy(proxy string) Option

WithProxy sets a proxy server for all browser requests. Supports HTTP, HTTPS, and SOCKS5 proxies. Example: "http://proxy.example.com:8080", "socks5://127.0.0.1:1080"

Note: Chrome's --proxy-server flag does not support embedded credentials (e.g. "http://user:pass@host:port"). For authenticated proxies, handle authentication separately at the page level.

func WithTrace added in v0.2.0

func WithTrace() Option

func WithUserAgent

func WithUserAgent(userAgent string) Option

WithUserAgent sets a custom user agent string for the browser.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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