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" "github.com/ysmood/leakless" ) func main() { // get the browser executable path path := launcher.NewBrowser().MustGet() // use the FormatArgs to construct args, this line is optional, you can construct the args manually args := launcher.New().FormatArgs() var cmd *exec.Cmd if true { // decide whether to use leakless or not cmd = leakless.New().Command(path, args...) } else { cmd = exec.Command(path, args...) } parser := launcher.NewURLParser() cmd.Stderr = parser utils.E(cmd.Start()) u := launcher.MustResolveURL(<-parser.URL) rod.New().ControlURL(u).MustConnect() }
Output:
Example (Print_browser_CLI_output) ¶
package main import ( "os" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" ) func main() { // Pipe the browser stderr and stdout to os.Stdout . u := launcher.New().Logger(os.Stdout).MustLaunch() rod.New().ControlURL(u).MustConnect() }
Output:
Example (Use_system_browser) ¶
package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" ) func main() { if path, exists := launcher.LookPath(); exists { u := launcher.New().Bin(path).MustLaunch() rod.New().ControlURL(u).MustConnect() } }
Output:
Index ¶
- Constants
- Variables
- func HostGoogle(revision int) string
- func HostNPM(revision int) string
- func HostPlaywright(revision int) string
- func LookPath() (found string, has bool)
- func MustResolveURL(u string) string
- func Open(url string)
- func ResolveURL(u string) (string, error)
- type Browser
- type Host
- type Launcher
- func (l *Launcher) Append(name flags.Flag, values ...string) *Launcher
- func (l *Launcher) Bin(path string) *Launcher
- func (l *Launcher) Cleanup()
- func (l *Launcher) Client() (*cdp.Client, error)
- func (l *Launcher) ClientHeader() (string, http.Header)
- func (l *Launcher) Context(ctx context.Context) *Launcher
- func (l *Launcher) Delete(name flags.Flag) *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 flags.Flag) string
- func (l *Launcher) GetFlags(name flags.Flag) ([]string, bool)
- func (l *Launcher) Has(name flags.Flag) bool
- func (l *Launcher) Headless(enable bool) *Launcher
- func (l *Launcher) IgnoreCerts(pks []crypto.PublicKey) error
- 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) MustClient() *cdp.Client
- func (l *Launcher) MustLaunch() string
- func (l *Launcher) NoSandbox(enable bool) *Launcher
- 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) Revision(rev int) *Launcher
- func (l *Launcher) Set(name flags.Flag, values ...string) *Launcher
- func (l *Launcher) StartURL(u string) *Launcher
- func (l *Launcher) UserDataDir(dir string) *Launcher
- func (l *Launcher) WorkingDir(path string) *Launcher
- func (l *Launcher) XVFB(args ...string) *Launcher
- type Manager
- type URLParser
Examples ¶
Constants ¶
const (
// HeaderName for remote launch
HeaderName = "Rod-Launcher"
)
const RevisionDefault = 1131003
RevisionDefault for chromium
const RevisionPlaywright = 1067
RevisionPlaywright for arm linux
Variables ¶
var DefaultBrowserDir = filepath.Join(map[string]string{ "windows": filepath.Join(os.Getenv("APPDATA")), "darwin": filepath.Join(os.Getenv("HOME"), ".cache"), "linux": filepath.Join(os.Getenv("HOME"), ".cache"), }[runtime.GOOS], "rod", "browser")
DefaultBrowserDir for downloaded browser. For unix is "$HOME/.cache/rod/browser", for Windows it's "%APPDATA%\rod\browser"
var DefaultUserDataDirPrefix = filepath.Join(os.TempDir(), "rod", "user-data")
DefaultUserDataDirPrefix ...
Functions ¶
func HostPlaywright ¶ added in v0.106.3
HostPlaywright to download browser
func LookPath ¶ added in v0.91.0
LookPath searches for the browser executable from often used paths on current operating system.
func MustResolveURL ¶ added in v0.65.0
MustResolveURL is similar to ResolveURL
func Open ¶ added in v0.91.0
func Open(url string)
Open tries to open the url via system's default browser.
func ResolveURL ¶ added in v0.65.0
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 are the candidates to download the browser. Hosts []Host // Revision of the browser to use Revision int // RootDir to download different browser versions. RootDir string // Log to print output Logger utils.Logger // LockPort a tcp port to prevent race downloading. Default is 2968 . LockPort int // HTTPClient to download the browser HTTPClient *http.Client }
Browser is a helper to download browser smartly
func (*Browser) Download ¶
Download browser from the fastest host. It will race downloading a TCP packet from each host and use the fastest host.
func (*Browser) Get ¶
Get is a smart helper to get the browser executable path. If Destination is not valid it will auto download the browser to Destination.
type Launcher ¶
type Launcher struct { Flags map[flags.Flag][]string `json:"flags"` // contains filtered or unexported fields }
Launcher is a helper to launch browser binary smartly
func MustNewManaged ¶ added in v0.98.0
MustNewManaged is similar to NewManaged
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, this folder will usually be cleaned up by the OS after reboot.
func NewAppMode ¶ added in v0.106.1
NewAppMode is a preset to run the browser like a native application.
func NewManaged ¶ added in v0.98.0
NewManaged creates a default Launcher instance from launcher.Manager. The serviceURL must point to a launcher.Manager. It will send a http request to the serviceURL to get the default settings of the Launcher instance. For example if the launcher.Manager running on a Linux machine will return different default settings from the one on Mac. If Launcher.Leakless is enabled, the remote browser will be killed after the websocket is closed.
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 of the browser binary path to launch, if the path is not empty the auto download will be disabled
func (*Launcher) Cleanup ¶ added in v0.49.7
func (l *Launcher) Cleanup()
Cleanup wait until the Browser exits and remove UserDataDir
func (*Launcher) ClientHeader ¶ added in v0.106.0
ClientHeader for launching browser remotely via the launcher.Manager.
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(append(os.Environ(), "TZ=Asia/Tokyo")...)
func (*Launcher) FormatArgs ¶
FormatArgs returns the formatted arg list for cli
func (*Launcher) Headless ¶
Headless switch. Whether to run browser in headless mode. A mode without visible UI.
func (*Launcher) IgnoreCerts ¶ added in v0.112.1
IgnoreCerts configure the Chrome's ignore-certificate-errors-spki-list argument with the public keys.
func (*Launcher) KeepUserDataDir ¶
KeepUserDataDir after remote browser is closed. By default launcher.FlagUserDataDir 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 ¶ 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) MustClient ¶ added in v0.106.0
MustClient similar to Launcher.Client
func (*Launcher) MustLaunch ¶ added in v0.50.0
MustLaunch is similar to Launch
func (*Launcher) NoSandbox ¶ added in v0.94.3
NoSandbox switch. Whether to run browser in no-sandbox mode. Linux users may face "running as root without --no-sandbox is not supported" in some Linux/Chrome combinations. This function helps switch mode easily. Be aware disabling sandbox is not trivial. Use at your own risk. Related doc: https://bugs.chromium.org/p/chromium/issues/detail?id=638180
func (*Launcher) ProfileDir ¶ added in v0.78.3
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 and the Launcher.Leakless is disabled, the launcher will try to reconnect to it first, if the reconnection fails it will launch a new browser.
func (*Launcher) Set ¶
Set a command line argument when launching the browser. Be careful the first argument is a flag name, it shouldn't contain values. The values the will be joined with comma. You can use the Launcher.FormatArgs to debug the final CLI arguments.
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 ¶ added in v0.56.0
WorkingDir to launch the browser process.
type Manager ¶ added in v0.98.0
type Manager struct { // Logger for key events Logger utils.Logger // Defaults should return the default Launcher settings Defaults func(http.ResponseWriter, *http.Request) *Launcher // BeforeLaunch hook is called right before the launching with the Launcher instance that will be used // to launch the browser. // Such as use it to filter malicious values of Launcher.UserDataDir, Launcher.Bin, or Launcher.WorkingDir. BeforeLaunch func(*Launcher, http.ResponseWriter, *http.Request) }
Manager is used to launch browsers via http server on another machine. The reason why we have Manager is after we launcher a browser, we can't dynamically change its CLI arguments, such as "--headless". The Manager allows us to decide what CLI arguments to pass to the browser when launch it remotely. The work flow looks like:
| Machine X | Machine Y | | NewManaged("a.com") -|-> http.ListenAndServe("a.com", launcher.NewManager()) --> launch browser | 1. X send a http request to Y, Y respond default Launcher settings based the OS of Y. 2. X start a websocket connect to Y with the Launcher settings 3. Y launches a browser with the Launcher settings X 4. Y transparently proxy the websocket connect between X and the launched browser
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
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
fixtures
|
|
chrome-empty
Package main ...
|
Package main ... |
chrome-exit-err
Package main ...
|
Package main ... |
chrome-lib-missing
Package main ...
|
Package main ... |
Package flags ...
|
Package flags ... |
Package main ...
|
Package main ... |
A server to help launch browser remotely
|
A server to help launch browser remotely |