From a5f5b4fbc23093221e9e3df4065924670bd913ae Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Sat, 28 Jan 2017 09:14:38 +0700 Subject: [PATCH] Adding headless docker example (NOT WORKING) --- client/client.go | 7 ++--- examples/headless/.gitignore | 2 ++ examples/headless/README.md | 17 +++++++++++ examples/headless/main.go | 59 ++++++++++++++++++++++++++++++++++++ testdata/alert.html | 17 +++++++++++ testdata/child1.html | 10 ++++++ testdata/child2.html | 10 ++++++ testdata/frameset.html | 9 ++++++ 8 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 examples/headless/.gitignore create mode 100644 examples/headless/README.md create mode 100644 examples/headless/main.go create mode 100644 testdata/alert.html create mode 100644 testdata/child1.html create mode 100644 testdata/child2.html create mode 100644 testdata/frameset.html diff --git a/client/client.go b/client/client.go index d38955e..d091c39 100644 --- a/client/client.go +++ b/client/client.go @@ -26,9 +26,6 @@ const ( ) var ( - // ErrInvalidProtocolVersion is the invalid protocol version error. - ErrInvalidProtocolVersion = errors.New("invalid protocol version") - // ErrUnsupportedProtocolType is the unsupported protocol type error. ErrUnsupportedProtocolType = errors.New("unsupported protocol type") @@ -158,9 +155,9 @@ func (c *Client) loadProtocolInfo(ctxt context.Context) (string, string, error) } if m := browserRE.FindAllStringSubmatch(v["Browser"], -1); len(m) != 0 { - c.ver = v["Protocol-Version"] c.typ = strings.ToLower(m[0][0]) } + c.ver = v["Protocol-Version"] } return c.ver, c.typ, nil @@ -180,7 +177,7 @@ func (c *Client) newTarget(ctxt context.Context, buf []byte) (Target, error) { } switch typ { - case "chrome", "chromium", "microsoft edge", "safari": + case "chrome", "chromium", "microsoft edge", "safari", "": x := new(Chrome) if buf != nil { err = easyjson.Unmarshal(buf, x) diff --git a/examples/headless/.gitignore b/examples/headless/.gitignore new file mode 100644 index 0000000..cf924f1 --- /dev/null +++ b/examples/headless/.gitignore @@ -0,0 +1,2 @@ +headless +headless.exe diff --git a/examples/headless/README.md b/examples/headless/README.md new file mode 100644 index 0000000..cae80b1 --- /dev/null +++ b/examples/headless/README.md @@ -0,0 +1,17 @@ +# About headless + +This is a version of the simple example but with the chromedp settings changed +to use the docker [yukinying/chrome-headless](yukinying/chrome-headless) image. + +## Running + +```sh +# retrieve docker image +docker pull yukinying/chrome-headless + +# start docker headless +docker run -i -t --shm-size=256m --rm --name=chrome-headless -p=127.0.0.1:9222:9222 yukinying/chrome-headless about:blank + +# run chromedp headless example +go build && ./headless +``` diff --git a/examples/headless/main.go b/examples/headless/main.go new file mode 100644 index 0000000..f667349 --- /dev/null +++ b/examples/headless/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "fmt" + "io/ioutil" + "log" + "time" + + cdp "github.com/knq/chromedp" + cdptypes "github.com/knq/chromedp/cdp" + "github.com/knq/chromedp/client" +) + +func main() { + var err error + + // create context + ctxt, cancel := context.WithCancel(context.Background()) + defer cancel() + + // create chrome + c, err := cdp.New(ctxt, cdp.WithTargets(client.New().WatchPageTargets(ctxt))) + if err != nil { + log.Fatal(err) + } + + // run task list + var site, res string + err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res)) + if err != nil { + log.Fatal(err) + } + + log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site) +} + +func googleSearch(q, text string, site, res *string) cdp.Tasks { + var buf []byte + sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text) + return cdp.Tasks{ + cdp.Navigate(`https://www.google.com`), + cdp.Sleep(2 * time.Second), + cdp.WaitVisible(`#hplogo`, cdp.ByID), + cdp.SendKeys(`#lst-ib`, q, cdp.ByID), + cdp.Click(`input[name="btnK"]`, cdp.ByQuery), + cdp.WaitNotVisible(`input[name="btnI"]`, cdp.ByQuery), + cdp.Text(sel, res), + cdp.Click(sel), + cdp.Sleep(2 * time.Second), + cdp.WaitVisible(`#footer`, cdp.ByQuery), + cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery), + cdp.Location(site), + cdp.Screenshot(`#testimonials`, &buf, cdp.ByID), + cdp.ActionFunc(func(context.Context, cdptypes.FrameHandler) error { + return ioutil.WriteFile("testimonials.png", buf, 0644) + }), + } +} diff --git a/testdata/alert.html b/testdata/alert.html new file mode 100644 index 0000000..13b4ba7 --- /dev/null +++ b/testdata/alert.html @@ -0,0 +1,17 @@ + + + +javascript alert test + + +
+ +
+ + + diff --git a/testdata/child1.html b/testdata/child1.html new file mode 100644 index 0000000..e1102f7 --- /dev/null +++ b/testdata/child1.html @@ -0,0 +1,10 @@ + + +child 1 + + +
+

child one

+
+ + diff --git a/testdata/child2.html b/testdata/child2.html new file mode 100644 index 0000000..e89823c --- /dev/null +++ b/testdata/child2.html @@ -0,0 +1,10 @@ + + +child 2 + + +
+

child two

+
+ + diff --git a/testdata/frameset.html b/testdata/frameset.html new file mode 100644 index 0000000..7f26ec8 --- /dev/null +++ b/testdata/frameset.html @@ -0,0 +1,9 @@ + + + frameset test + + + + + +