launcher

package
v0.63.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 22 Imported by: 278

README

Overview

A lib to help launch or download browser. You can also use it as a standalone lib without Rod.

Documentation

Overview

Example (Custom_launch)
package main

import (
	"os/exec"

	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/utils"
)

func main() {
	// get the browser executable path
	bin, err := launcher.NewBrowser().Get()
	utils.E(err)

	// use the helper to construct args, this line is optional, you can construct the args manually
	args := launcher.New().Headless(false).Env("TZ=Asia/Tokyo").FormatArgs()

	parser := launcher.NewURLParser()

	cmd := exec.Command(bin, args...)
	cmd.Stderr = parser
	err = cmd.Start()
	utils.E(err)

	rod.New().ControlURL(<-parser.URL).MustConnect()
}
Output:

Index

Examples

Constants

View Source
const DefaultRevision = 800071

DefaultRevision for chrome curl -s -S https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Mac%2FLAST_CHANGE\?alt\=media

View Source
const HeaderName = "Rod-Launcher"

HeaderName for remote launch

View Source
const HostGoogle = "https://storage.googleapis.com"

HostGoogle to download browser

View Source
const HostTaobao = "https://npm.taobao.org/mirrors"

HostTaobao to download browser

Variables

This section is empty.

Functions

func GetWebSocketDebuggerURL

func GetWebSocketDebuggerURL(u string) (string, error)

GetWebSocketDebuggerURL from browser remote url

Types

type Browser

type Browser struct {
	Context context.Context

	// Hosts to download browser, examples:
	// https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/748030/chrome-linux.zip
	// https://storage.googleapis.com/chromium-browser-snapshots/Mac/748030/chrome-mac.zip
	// https://storage.googleapis.com/chromium-browser-snapshots/Win/748030/chrome-win.zip
	Hosts []string

	// Revision of the browser to use
	Revision int

	// Dir default is the filepath.Join(os.TempDir(), "rod")
	Dir string

	// Log to print output
	Logger io.Writer

	ExecSearchMap map[string][]string
}

Browser is a helper to download browser smartly

func NewBrowser

func NewBrowser() *Browser

NewBrowser with default values

func (*Browser) Download

func (lc *Browser) Download() error

Download chromium

func (*Browser) ExecPath

func (lc *Browser) ExecPath() string

ExecPath of the chromium executable

func (*Browser) Get

func (lc *Browser) Get() (string, error)

Get is a smart helper to get the browser executable binary. It will first try to find the browser from local disk, if not exists it will try to download the chromium to Dir.

func (*Browser) Open added in v0.49.0

func (lc *Browser) Open(url string)

Open url via a browser

type Launcher

type Launcher struct {
	Flags map[string][]string `json:"flags"`
	// contains filtered or unexported fields
}

Launcher is a helper to launch browser binary smartly

func New

func New() *Launcher

New returns the default arguments to start browser. "--" is optional, with or without it won't affect the result. Headless will be enabled by default. Leakless will be enabled by default. List of switches: https://peter.sh/experiments/chromium-command-line-switches/

func NewRemote

func NewRemote(remoteURL string) *Launcher

NewRemote create a Launcher instance from remote defaults. You must use it with launch.NewProxy or use the docker image mentioned from here: https://github.com/go-rod/rod/blob/master/lib/examples/remote-launch

func NewUserMode

func NewUserMode() *Launcher

NewUserMode is a preset to enable reusing current user data. Useful for automation of personal browser. If you see any error, it may because you can't launch debug port for existing browser, the solution is to completely close the running browser. Unfortunately, there's no API for rod to tell it automatically yet.

func (*Launcher) Append added in v0.48.0

func (l *Launcher) Append(name string, values ...string) *Launcher

Append values to the flag

func (*Launcher) Bin

func (l *Launcher) Bin(path string) *Launcher

Bin set browser executable file path. If it's empty, launcher will automatically search or download the bin.

func (*Launcher) Cleanup added in v0.49.7

func (l *Launcher) Cleanup()

Cleanup wait until the Browser exits and release related resources

func (*Launcher) Client

func (l *Launcher) Client() *cdp.Client

Client for launching browser remotely, such as browser from a docker container.

func (*Launcher) Context

func (l *Launcher) Context(ctx context.Context) *Launcher

Context set the context

func (*Launcher) Delete

func (l *Launcher) Delete(name string) *Launcher

Delete a flag

func (*Launcher) Devtools

func (l *Launcher) Devtools(autoOpenForTabs bool) *Launcher

Devtools switch to auto open devtools for each tab

func (*Launcher) Env added in v0.56.0

func (l *Launcher) Env(env ...string) *Launcher

Env to launch the browser process. The default value is os.Environ(). Usually you use it to set the timezone env. Such as Env("TZ=America/New_York"). Timezone list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

func (*Launcher) FormatArgs

func (l *Launcher) FormatArgs() []string

FormatArgs returns the formated arg list for cli

func (*Launcher) Get

func (l *Launcher) Get(name string) (string, bool)

Get flag's first value

func (*Launcher) GetFlags

func (l *Launcher) GetFlags(name string) ([]string, bool)

GetFlags from settings

func (*Launcher) Headless

func (l *Launcher) Headless(enable bool) *Launcher

Headless switch. Whether to run browser in headless mode. A mode without visible UI.

func (*Launcher) JSON

func (l *Launcher) JSON() []byte

JSON serialization

func (*Launcher) KeepUserDataDir

func (l *Launcher) KeepUserDataDir() *Launcher

KeepUserDataDir after remote browser is closed. By default user-data-dir will be removed.

func (*Launcher) Kill added in v0.59.0

func (l *Launcher) Kill()

Kill the browser process

func (*Launcher) Launch

func (l *Launcher) Launch() (string, error)

Launch doc is similar to the method MustLaunch

func (*Launcher) Leakless added in v0.57.1

func (l *Launcher) Leakless(enable bool) *Launcher

Leakless switch. If enabled, the browser will be force killed after the Go process exits. The doc of leakless: https://github.com/ysmood/leakless.

func (*Launcher) Logger added in v0.56.0

func (l *Launcher) Logger(w io.Writer) *Launcher

Logger to handle stdout and stderr from browser. For example, pipe all browser output to stdout: launcher.New().Logger(os.Stdout)

func (*Launcher) MustLaunch added in v0.50.0

func (l *Launcher) MustLaunch() string

MustLaunch a standalone temp browser instance and returns the debug url. bin and profileDir are optional, set them to empty to use the default values. If you want to reuse sessions, such as cookies, set the userDataDir to the same location.

func (*Launcher) PID

func (l *Launcher) PID() int

PID returns the browser process pid

func (*Launcher) Proxy added in v0.57.2

func (l *Launcher) Proxy(host string) *Launcher

Proxy switch. When disabled leakless will be disabled.

func (*Launcher) RemoteDebuggingPort

func (l *Launcher) RemoteDebuggingPort(port int) *Launcher

RemoteDebuggingPort to launch the browser. Zero for a random port. Zero is the default value. If it's not zero, the launcher will try to connect to it before starting a new browser process. For example, to reuse the same browser process for between 2 runs of a Go program, you can do something like launcher.New().RemoteDebuggingPort(9222).MustLaunch()

func (*Launcher) Set

func (l *Launcher) Set(name string, values ...string) *Launcher

Set a flag

func (*Launcher) UserDataDir

func (l *Launcher) UserDataDir(dir string) *Launcher

UserDataDir is where the browser will look for all of its state, such as cookie and cache. When set to empty, system user's default dir will be used.

func (*Launcher) WorkingDir added in v0.56.0

func (l *Launcher) WorkingDir(path string) *Launcher

WorkingDir to launch the browser process.

type Proxy

type Proxy struct {
	Logger io.Writer
}

Proxy to help launch browser remotely. Any http request will return a default Launcher based on remote OS environment. Any websocket request will start a new browser and the request will be proxied to the browser. The websocket header "Rod-Launcher" holds the options to launch browser. If the websocket is closed, the browser will be killed.

func NewProxy added in v0.49.0

func NewProxy() *Proxy

NewProxy instance

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

type URLParser added in v0.56.0

type URLParser struct {
	URL    chan string
	Buffer string // buffer for the browser stdout
	// contains filtered or unexported fields
}

URLParser to get control url from stderr

func NewURLParser added in v0.56.0

func NewURLParser() *URLParser

NewURLParser instance

func (*URLParser) Write added in v0.56.0

func (r *URLParser) Write(p []byte) (n int, err error)

Write interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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