Adding headless docker example (NOT WORKING)

This commit is contained in:
Kenneth Shaw 2017-01-28 09:14:38 +07:00
parent 4ff7580243
commit a5f5b4fbc2
8 changed files with 126 additions and 5 deletions

View File

@ -26,9 +26,6 @@ const (
) )
var ( var (
// ErrInvalidProtocolVersion is the invalid protocol version error.
ErrInvalidProtocolVersion = errors.New("invalid protocol version")
// ErrUnsupportedProtocolType is the unsupported protocol type error. // ErrUnsupportedProtocolType is the unsupported protocol type error.
ErrUnsupportedProtocolType = errors.New("unsupported protocol type") 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 { if m := browserRE.FindAllStringSubmatch(v["Browser"], -1); len(m) != 0 {
c.ver = v["Protocol-Version"]
c.typ = strings.ToLower(m[0][0]) c.typ = strings.ToLower(m[0][0])
} }
c.ver = v["Protocol-Version"]
} }
return c.ver, c.typ, nil return c.ver, c.typ, nil
@ -180,7 +177,7 @@ func (c *Client) newTarget(ctxt context.Context, buf []byte) (Target, error) {
} }
switch typ { switch typ {
case "chrome", "chromium", "microsoft edge", "safari": case "chrome", "chromium", "microsoft edge", "safari", "":
x := new(Chrome) x := new(Chrome)
if buf != nil { if buf != nil {
err = easyjson.Unmarshal(buf, x) err = easyjson.Unmarshal(buf, x)

2
examples/headless/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
headless
headless.exe

View File

@ -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
```

59
examples/headless/main.go Normal file
View File

@ -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)
}),
}
}

17
testdata/alert.html vendored Normal file
View File

@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<title>javascript alert test</title>
</head>
<body>
<div id="div1">
<input id="input1" value="input value1">
</div>
<script>
window.onload = function(e) {
setTimeout(function() { alert("alert1"); }, 1000);
setTimeout(function() { window.confirm("confirm1"); }, 2000);
};
</script>
</body>
</html>

10
testdata/child1.html vendored Normal file
View File

@ -0,0 +1,10 @@
<html>
<head>
<title>child 1</title>
</head>
<body>
<div id="child1">
<p>child one</p>
</div>
</body>
</html>

10
testdata/child2.html vendored Normal file
View File

@ -0,0 +1,10 @@
<html>
<head>
<title>child 2</title>
</head>
<body>
<div id="child2">
<p>child two</p>
</div>
</body>
</html>

9
testdata/frameset.html vendored Normal file
View File

@ -0,0 +1,9 @@
<html>
<head>
<title>frameset test</title>
</head>
<frameset cols="50%,*">
<frame src="child1.html">
<frame src="child2.html">
</frameset>
</html>