launcher

package
v0.79.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 24 Imported by: 280

README

Overview

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

Documentation

Overview

Package launcher for launching browser utils.

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 = 809590

DefaultRevision for chrome

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 MustResolveURL added in v0.65.0

func MustResolveURL(u string) string

MustResolveURL is similar to FetchURL

func ResolveURL added in v0.65.0

func ResolveURL(u string) (string, error)

ResolveURL by requesting the u, it will try best to normalize the u. The format of u can be "9222", ":9222", "host:9222", "ws://host:9222", "wss://host:9222", "https://host:9222" "http://host:9222". The return string will look like: "ws://host:9222/devtools/browser/4371405f-84df-4ad6-9e0f-eab81f7521cc"

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

	// Lock a tcp port to prevent race downloading. Default is 2968 .
	Lock int
}

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) LookPath added in v0.77.1

func (lc *Browser) LookPath() (string, bool)

LookPath of the browser executable.

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 MustNewRemote added in v0.65.0

func MustNewRemote(remoteURL string) *Launcher

MustNewRemote is similar to NewRemote

func New

func New() *Launcher

New returns the default arguments to start browser. Headless will be enabled by default. Leakless will be enabled by default. UserDataDir will use OS tmp dir by default.

func NewRemote

func NewRemote(remoteURL string) (*Launcher, error)

NewRemote creates a Launcher instance from remote defaults. For more info check the doc of RemoteLauncher.

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 remove UserDataDir

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 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) 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 is similar to Launch

func (*Launcher) PID

func (l *Launcher) PID() int

PID returns the browser process pid

func (*Launcher) ProfileDir added in v0.78.3

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

ProfileDir is the browser profile the browser will use. When set to empty, the profile 'Default' is used. Related article: https://superuser.com/a/377195

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()

Related doc: https://chromedevtools.github.io/devtools-protocol/

func (*Launcher) Set

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

Set a command line argument to launch the browser. People also call it command line flag or switch. List of available flags: https://peter.sh/experiments/chromium-command-line-switches/

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, browser will use current OS home dir. Related doc: https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md

func (*Launcher) WorkingDir added in v0.56.0

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

WorkingDir to launch the browser process.

type RemoteLauncher added in v0.65.0

type RemoteLauncher struct {
	Logger utils.Logger
}

RemoteLauncher is used to launch browsers via http server on another machine. For example, the work flow looks like:

|     Machine A      |                           Machine B                                  |
 NewRemote("a.com") --> http.ListenAndServe("a.com", NewRemoteLauncher()) --> launch browser

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 NewRemoteLauncher added in v0.65.0

func NewRemoteLauncher() *RemoteLauncher

NewRemoteLauncher instance

func (*RemoteLauncher) ServeHTTP added in v0.65.0

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

type URLParser added in v0.56.0

type URLParser struct {
	sync.Mutex

	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