Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 25, 2020
1 parent b1cb51e commit f164718
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ can dynamically launch a browser for each remote driver with customizable browse
It's [tuned](lib/docker/Dockerfile) for screenshots and fonts among popular natural languages.
You can easily load balance requests to the cluster of this image, each container can create multiple browser instances at the same time.

You can also use it to launch a browser manually:

```bash
docker run -p 9222:9222 rodorg/rod chromium-browser --headless --no-sandbox --remote-debugging-port=9222 --remote-debugging-address=0.0.0.0
```

### Q: Why there is always an "about:blank" page

It's an issue of the browser itself. If we enable the `--no-first-run` flag and we don't create a blank page, it will create a hello page which will consume more power.
Expand Down
13 changes: 7 additions & 6 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ func Example_timeout_handling() {

// We use "Must" prefixed functions to write example code. But in production you may want to use
// the no-prefix version of them.
// About why we use "Must" as the prefix, it's similar with https://golang.org/pkg/regexp/#MustCompile
// About why we use "Must" as the prefix, it's similar to https://golang.org/pkg/regexp/#MustCompile
func Example_error_handling() {
page := rod.New().MustConnect().MustPage("https://example.com")

// The two code blocks below are basically the same:

// The block below is better for production code. It follows the standards of golang error handling.
// Usually, this style will make error handling more consistent and precisely.
// The block below is better for production code. It's the standard error handling.
// Usually, this style is more consistent and precisely.
{
el, err := page.Element("a")
if err != nil {
Expand All @@ -158,9 +158,10 @@ func Example_error_handling() {
fmt.Println(html)
}

// The block below is better for example code or quick scripting. We use panic to short-circuit logics.
// So that we can code in fluent style: https://en.wikipedia.org/wiki/Fluent_interface
// It will reduce the code to type, but it may also catch extra errors (less consistent and precisely).
// The block below is better for debugging or quick scripting. We use panic to short-circuit logics.
// So that we can take advantage of fluent interface (https://en.wikipedia.org/wiki/Fluent_interface)
// and fail-fast (https://en.wikipedia.org/wiki/Fail-fast).
// This style will reduce code, but it may also catch extra errors (less consistent and precisely).
{
err := rod.Try(func() {
fmt.Println(page.MustElement("a").MustHTML())
Expand Down
1 change: 1 addition & 0 deletions lib/cdp/client.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package cdp for application layer communication with browser.
package cdp

import (
Expand Down
17 changes: 16 additions & 1 deletion lib/examples/remote-launch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
)

func main() {
// Launch browser remotely
// To launch remote browsers, you need a remote launcher service,
// Rod provides a docker image for beginers, make sure have started:
// docker run -p 9222:9222 rodorg/rod
//
// For more information, check the doc of launcher.RemoteLauncher
l := launcher.MustNewRemote("ws://localhost:9222")

// Manipulate flags like the example in examples_test.go
Expand All @@ -27,3 +30,15 @@ func main() {

utils.Pause()
}

func hardcoreStyle() {
// You can also manually launch a browser in the image:
// docker run -p 9222:9222 rodorg/rod chromium-browser --headless --no-sandbox --remote-debugging-port=9222 --remote-debugging-address=0.0.0.0
u := launcher.MustResolveURL("9222")

browser := rod.New().ControlURL(u).MustConnect()

fmt.Println(
browser.MustPage("https://github.com").MustEval("() => document.title"),
)
}
1 change: 1 addition & 0 deletions lib/launcher/launcher.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package launcher for launching browser utils.
package launcher

import (
Expand Down
8 changes: 4 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (p *Page) Race() *RaceContext {
return &RaceContext{page: p, noSleepPage: p.Sleeper(nil)}
}

// Element the doc is similar with MustElement but has a callback when a match is found
// Element the doc is similar to MustElement but has a callback when a match is found
func (rc *RaceContext) Element(selector string, callback func(*Element) error) *RaceContext {
rc.branches = append(rc.branches, &raceBranch{
func() (*Element, error) { return rc.noSleepPage.Element(selector) },
Expand All @@ -334,7 +334,7 @@ func (rc *RaceContext) Element(selector string, callback func(*Element) error) *
return rc
}

// ElementX the doc is similar with ElementX but has a callback when a match is found
// ElementX the doc is similar to ElementX but has a callback when a match is found
func (rc *RaceContext) ElementX(selector string, callback func(*Element) error) *RaceContext {
rc.branches = append(rc.branches, &raceBranch{
func() (*Element, error) { return rc.noSleepPage.ElementX(selector) },
Expand All @@ -343,7 +343,7 @@ func (rc *RaceContext) ElementX(selector string, callback func(*Element) error)
return rc
}

// ElementR the doc is similar with ElementR but has a callback when a match is found
// ElementR the doc is similar to ElementR but has a callback when a match is found
func (rc *RaceContext) ElementR(selector, regex string, callback func(*Element) error) *RaceContext {
rc.branches = append(rc.branches, &raceBranch{
func() (*Element, error) { return rc.noSleepPage.ElementR(selector, regex) },
Expand All @@ -352,7 +352,7 @@ func (rc *RaceContext) ElementR(selector, regex string, callback func(*Element)
return rc
}

// ElementByJS the doc is similar with MustElementByJS but has a callback when a match is found
// ElementByJS the doc is similar to MustElementByJS but has a callback when a match is found
func (rc *RaceContext) ElementByJS(opts *EvalOptions, callback func(*Element) error) *RaceContext {
rc.branches = append(rc.branches, &raceBranch{
func() (*Element, error) { return rc.noSleepPage.ElementByJS(opts) },
Expand Down

0 comments on commit f164718

Please sign in to comment.