Documentation

Getting Started

Install go-browser, configure prerequisites, and make your first page fetch with Go.

Prerequisites

Requirement Version Notes
Go 1.21+ Module support required
Chrome / Chromium Latest Installed on the system PATH or standard location

go-browser uses go-rod under the hood, which automatically locates Chrome on your system. On macOS it checks /Applications/Google Chrome.app; on Linux it searches google-chrome, google-chrome-stable, chromium, and chromium-browser in PATH.

Installation

go get github.com/pardnchiu/go-browser

Basic Usage

1. Fetch a page as Markdown

package main

import (
    "context"
    "fmt"
    "time"

    goBrowser "github.com/pardnchiu/go-browser"
)

func main() {
    ctx := context.Background()
    result, err := goBrowser.Fetch(ctx, "https://example.com", 30*time.Second, nil)
    if err != nil {
        panic(err)
    }
    fmt.Println("Title:", result.Title)
    fmt.Println("Content:", result.Content)
}

2. Fetch with custom options

opt := &goBrowser.Option{
    IdleWait:  3 * time.Second,
    MaxLength: 50000,
    Headless:  true,
    Type:      goBrowser.TypeHTML,
    Viewport:  &goBrowser.Viewport{Width: 1920, Height: 1080},
}

result, err := goBrowser.Fetch(ctx, "https://example.com", 30*time.Second, opt)
opt := &goBrowser.Option{
    SameSession: true,
    Profile:     "Default",
}

result, err := goBrowser.Fetch(ctx, "https://github.com", 30*time.Second, opt)

This extracts cookies from your local Chrome profile and injects them into a temporary browser instance.

4. Interactive tab operations

tabID, err := goBrowser.CreateTab(ctx, "https://example.com", nil)
if err != nil {
    panic(err)
}
defer goBrowser.CloseTab(tabID)

// Click a button
goBrowser.TabClick(tabID, "#submit-btn")

// Type into a field
goBrowser.TabType(tabID, "#search-input", "hello world")

// Scroll down
goBrowser.TabScroll(tabID, 3)

// Take a snapshot
result, _ := goBrowser.TabSnapshot(tabID)
fmt.Println(result.Content)

5. Output as JSON tree

opt := &goBrowser.Option{
    Type: goBrowser.TypeJSON,
}

result, err := goBrowser.Fetch(ctx, "https://example.com", 30*time.Second, opt)
fmt.Println(result.Content) // JSON string with structured tree

Cleanup

Call goBrowser.Close() to release all browser resources when done:

defer goBrowser.Close()

Concurrency

Set the maximum number of concurrent page loads:

goBrowser.SetMaxConcurrency(16)

The default concurrency limit is 8.

中文