chromex

package module
v1.0.75 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 4 Imported by: 0

README

chromex

A minimal Go wrapper around chromedp for Chrome automation.

Installation

go get github.com/digital-foxy/chromex

Usage

package main

import (
    "context"
    "fmt"

    "github.com/chromedp/chromedp"
    "github.com/digital-foxy/chromex"
)

func main() {
    title, err := chromex.RunChrome(chromex.ChromeConfig{
        Timeout: 30 * time.Second,
    }, func(ctx context.Context) (string, error) {
        var title string
        err := chromedp.Run(ctx,
            chromedp.Navigate("https://example.com"),
            chromedp.Title(&title),
        )
        return title, err
    })

    if err != nil {
        panic(err)
    }
    fmt.Println(title)
}

Configuration

Option Description Default
ChromePath Path to Chrome executable Auto-detected
Timeout Operation timeout 60s
Flags Custom chromedp flags DefaultFlags()

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout is the default timeout for Chrome operations.
	DefaultTimeout time.Duration = 60 * time.Second
)

Variables

This section is empty.

Functions

func DefaultFlags

func DefaultFlags() []chromedp.ExecAllocatorOption

DefaultFlags returns the default chromedp flags used for automation. These flags help avoid detection and ensure stable operation.

func RunChrome

func RunChrome[T any](config Options, extractor Extractor[T]) (T, error)

RunChrome launches Chrome with the given configuration and executes the extractor function. It handles the Chrome lifecycle (launch, context creation, cleanup) and returns the extracted data.

Parameters:

  • config: Chrome configuration (path, timeout, flags)
  • extractor: Function that performs the actual extraction using the chromedp context

Returns the extracted data of type T and any error encountered.

Types

type Extractor

type Extractor[T any] func(ctx context.Context) (T, error)

Extractor is a function that extracts data from a Chrome context. It receives the chromedp context and should return the extracted data or an error.

type Options

type Options struct {
	// Path to the Chrome executable. If empty, chromedp will try to find Chrome automatically.
	Path string
	// Timeout for the entire Chrome operation. Defaults to 120 seconds if not set.
	Timeout time.Duration
	// Custom chromedp flags. If empty, DefaultFlags() will be used.
	Flags []chromedp.ExecAllocatorOption
}

Options holds configuration for Chrome execution.

Jump to

Keyboard shortcuts

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