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 ¶
- Constants
- func MustResolveURL(u string) string
- func ResolveURL(u string) (string, error)
- type Browser
- type Launcher
- func (l *Launcher) Append(name string, values ...string) *Launcher
- func (l *Launcher) Bin(path string) *Launcher
- func (l *Launcher) Cleanup()
- func (l *Launcher) Client() *cdp.Client
- func (l *Launcher) Context(ctx context.Context) *Launcher
- func (l *Launcher) Delete(name string) *Launcher
- func (l *Launcher) Devtools(autoOpenForTabs bool) *Launcher
- func (l *Launcher) Env(env ...string) *Launcher
- func (l *Launcher) FormatArgs() []string
- func (l *Launcher) Get(name string) (string, bool)
- func (l *Launcher) GetFlags(name string) ([]string, bool)
- func (l *Launcher) Headless(enable bool) *Launcher
- func (l *Launcher) JSON() []byte
- func (l *Launcher) KeepUserDataDir() *Launcher
- func (l *Launcher) Kill()
- func (l *Launcher) Launch() (string, error)
- func (l *Launcher) Leakless(enable bool) *Launcher
- func (l *Launcher) Logger(w io.Writer) *Launcher
- func (l *Launcher) MustLaunch() string
- func (l *Launcher) PID() int
- func (l *Launcher) ProfileDir(dir string) *Launcher
- func (l *Launcher) Proxy(host string) *Launcher
- func (l *Launcher) RemoteDebuggingPort(port int) *Launcher
- func (l *Launcher) Set(name string, values ...string) *Launcher
- func (l *Launcher) UserDataDir(dir string) *Launcher
- func (l *Launcher) WorkingDir(path string) *Launcher
- type RemoteLauncher
- type URLParser
Examples ¶
Constants ¶
const DefaultRevision = 809590
DefaultRevision for chrome
const HeaderName = "Rod-Launcher"
HeaderName for remote launch
const HostGoogle = "https://storage.googleapis.com"
HostGoogle to download browser
const HostTaobao = "https://npm.taobao.org/mirrors"
HostTaobao to download browser
Variables ¶
This section is empty.
Functions ¶
func ResolveURL ¶
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 (*Browser) Get ¶
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.
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 ¶
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 ¶
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) Bin ¶
Bin set browser executable file path. If it's empty, launcher will automatically search or download the bin.
func (*Launcher) Cleanup ¶
func (l *Launcher) Cleanup()
Cleanup wait until the Browser exits and remove UserDataDir
func (*Launcher) Client ¶
Client for launching browser remotely, such as browser from a docker container.
func (*Launcher) Env ¶
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 ¶
FormatArgs returns the formated arg list for cli
func (*Launcher) Headless ¶
Headless switch. Whether to run browser in headless mode. A mode without visible UI.
func (*Launcher) KeepUserDataDir ¶
KeepUserDataDir after remote browser is closed. By default user-data-dir will be removed.
func (*Launcher) Launch ¶
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 ¶
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 ¶
Logger to handle stdout and stderr from browser. For example, pipe all browser output to stdout: launcher.New().Logger(os.Stdout)
func (*Launcher) MustLaunch ¶
MustLaunch is similar to Launch
func (*Launcher) ProfileDir ¶
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) RemoteDebuggingPort ¶
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 ¶
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 ¶
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 ¶
WorkingDir to launch the browser process.
type RemoteLauncher ¶
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 (*RemoteLauncher) ServeHTTP ¶
func (p *RemoteLauncher) ServeHTTP(w http.ResponseWriter, r *http.Request)