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 ¶
- Constants
- func GetWebSocketDebuggerURL(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) 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 Proxy
- type URLParser
Examples ¶
Constants ¶
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
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 GetWebSocketDebuggerURL ¶
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
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 ¶
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) Bin ¶
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 ¶
Client for launching browser remotely, such as browser from a docker container.
func (*Launcher) Env ¶ added in v0.56.0
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) Leakless ¶ added in v0.57.1
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
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
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) 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()
func (*Launcher) UserDataDir ¶
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
WorkingDir to launch the browser process.
type Proxy ¶
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.