2018-07-13 05:46:30 +00:00
|
|
|
// Package chromedp is a high level Chrome DevTools Protocol client that
|
|
|
|
// simplifies driving browsers for scraping, unit testing, or profiling web
|
|
|
|
// pages using the CDP.
|
2017-12-22 03:14:21 +00:00
|
|
|
//
|
2018-07-13 05:46:30 +00:00
|
|
|
// chromedp requires no third-party dependencies, implementing the async Chrome
|
|
|
|
// DevTools Protocol entirely in Go.
|
2017-01-24 15:09:23 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/cdp"
|
2018-05-18 22:03:47 +00:00
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/chromedp/client"
|
|
|
|
"github.com/chromedp/chromedp/runner"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultNewTargetTimeout is the default time to wait for a new target to
|
|
|
|
// be started.
|
|
|
|
DefaultNewTargetTimeout = 3 * time.Second
|
|
|
|
|
|
|
|
// DefaultCheckDuration is the default time to sleep between a check.
|
|
|
|
DefaultCheckDuration = 50 * time.Millisecond
|
|
|
|
|
|
|
|
// DefaultPoolStartPort is the default start port number.
|
|
|
|
DefaultPoolStartPort = 9000
|
|
|
|
|
|
|
|
// DefaultPoolEndPort is the default end port number.
|
|
|
|
DefaultPoolEndPort = 10000
|
2017-01-24 15:09:23 +00:00
|
|
|
)
|
|
|
|
|
2018-07-13 05:46:30 +00:00
|
|
|
// CDP is the high-level Chrome DevTools Protocol browser manager, handling the
|
|
|
|
// browser process runner, WebSocket clients, associated targets, and network,
|
|
|
|
// page, and DOM events.
|
2017-01-24 15:09:23 +00:00
|
|
|
type CDP struct {
|
|
|
|
// r is the chrome runner.
|
|
|
|
r *runner.Runner
|
|
|
|
|
|
|
|
// opts are command line options to pass to a created runner.
|
|
|
|
opts []runner.CommandLineOption
|
|
|
|
|
|
|
|
// watch is the channel for new client targets.
|
|
|
|
watch <-chan client.Target
|
|
|
|
|
|
|
|
// cur is the current active target's handler.
|
2017-12-27 02:30:28 +00:00
|
|
|
cur cdp.Executor
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// handlers is the active handlers.
|
2017-02-12 04:59:33 +00:00
|
|
|
handlers []*TargetHandler
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// handlerMap is the map of target IDs to its active handler.
|
|
|
|
handlerMap map[string]int
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// logging funcs
|
2018-05-18 22:03:47 +00:00
|
|
|
logf, debugf, errf func(string, ...interface{})
|
2017-02-13 09:00:25 +00:00
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// New creates and starts a new CDP instance.
|
2017-01-24 15:09:23 +00:00
|
|
|
func New(ctxt context.Context, opts ...Option) (*CDP, error) {
|
|
|
|
c := &CDP{
|
2017-02-12 04:59:33 +00:00
|
|
|
handlers: make([]*TargetHandler, 0),
|
2017-01-24 15:09:23 +00:00
|
|
|
handlerMap: make(map[string]int),
|
2017-02-13 09:00:25 +00:00
|
|
|
logf: log.Printf,
|
|
|
|
debugf: func(string, ...interface{}) {},
|
2018-05-18 22:03:47 +00:00
|
|
|
errf: func(s string, v ...interface{}) { log.Printf("error: "+s, v...) },
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply options
|
|
|
|
for _, o := range opts {
|
2017-11-25 20:20:52 +00:00
|
|
|
if err := o(c); err != nil {
|
2017-01-24 15:09:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for supplied runner, if none then create one
|
|
|
|
if c.r == nil && c.watch == nil {
|
2017-11-25 20:20:52 +00:00
|
|
|
var err error
|
2017-01-24 15:09:23 +00:00
|
|
|
c.r, err = runner.Run(ctxt, c.opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch handlers
|
|
|
|
if c.watch == nil {
|
2018-07-13 03:57:20 +00:00
|
|
|
c.watch = c.r.Client().WatchPageTargets(ctxt)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for t := range c.watch {
|
2017-03-13 02:09:57 +00:00
|
|
|
if t == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
go c.AddTarget(ctxt, t)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO: fix this
|
2017-12-18 00:11:42 +00:00
|
|
|
timeout := time.After(defaultNewTargetTimeout)
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// wait until at least one target active
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
c.RLock()
|
|
|
|
exists := c.cur != nil
|
|
|
|
c.RUnlock()
|
|
|
|
if exists {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// TODO: fix this
|
2017-01-24 15:09:23 +00:00
|
|
|
time.Sleep(DefaultCheckDuration)
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
2017-02-12 04:59:33 +00:00
|
|
|
return nil, ctxt.Err()
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
case <-timeout:
|
2017-11-25 20:15:28 +00:00
|
|
|
return nil, errors.New("timeout waiting for initial target")
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTarget adds a target using the supplied context.
|
|
|
|
func (c *CDP) AddTarget(ctxt context.Context, t client.Target) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
// create target manager
|
2018-05-18 22:03:47 +00:00
|
|
|
h, err := NewTargetHandler(t, c.logf, c.debugf, c.errf)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2018-05-18 22:03:47 +00:00
|
|
|
c.errf("could not create handler for %s: %v", t, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// run
|
2017-11-25 20:20:52 +00:00
|
|
|
if err := h.Run(ctxt); err != nil {
|
2018-05-18 22:03:47 +00:00
|
|
|
c.errf("could not start handler for %s: %v", t, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to active handlers
|
|
|
|
c.handlers = append(c.handlers, h)
|
|
|
|
c.handlerMap[t.GetID()] = len(c.handlers) - 1
|
|
|
|
if c.cur == nil {
|
|
|
|
c.cur = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for the Chrome runner to terminate.
|
|
|
|
func (c *CDP) Wait() error {
|
|
|
|
c.RLock()
|
|
|
|
r := c.r
|
|
|
|
c.RUnlock()
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
return r.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown closes all Chrome page handlers.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (c *CDP) Shutdown(ctxt context.Context, opts ...client.Option) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
2017-03-13 02:09:57 +00:00
|
|
|
if c.r != nil {
|
|
|
|
return c.r.Shutdown(ctxt, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListTargets returns the target IDs of the managed targets.
|
|
|
|
func (c *CDP) ListTargets() []string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
2017-02-18 08:36:24 +00:00
|
|
|
i, targets := 0, make([]string, len(c.handlers))
|
2017-01-24 15:09:23 +00:00
|
|
|
for k := range c.handlerMap {
|
|
|
|
targets[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandlerByIndex retrieves the domains manager for the specified index.
|
2017-12-27 02:30:28 +00:00
|
|
|
func (c *CDP) GetHandlerByIndex(i int) cdp.Executor {
|
2017-01-24 15:09:23 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
if i < 0 || i >= len(c.handlers) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.handlers[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandlerByID retrieves the domains manager for the specified target ID.
|
2017-12-27 02:30:28 +00:00
|
|
|
func (c *CDP) GetHandlerByID(id string) cdp.Executor {
|
2017-01-24 15:09:23 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
if i, ok := c.handlerMap[id]; ok {
|
|
|
|
return c.handlers[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// SetHandler sets the active handler to the target with the specified index.
|
2017-01-24 15:09:23 +00:00
|
|
|
func (c *CDP) SetHandler(i int) error {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if i < 0 || i >= len(c.handlers) {
|
|
|
|
return fmt.Errorf("no handler associated with target index %d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cur = c.handlers[i]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHandlerByID sets the active target to the target with the specified id.
|
|
|
|
func (c *CDP) SetHandlerByID(id string) error {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if i, ok := c.handlerMap[id]; ok {
|
|
|
|
c.cur = c.handlers[i]
|
2018-12-07 23:57:14 +00:00
|
|
|
return nil
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("no handler associated with target id %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTarget creates a new target using supplied context and options, returning
|
|
|
|
// the id of the created target only after the target has been started for
|
|
|
|
// monitoring.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (c *CDP) newTarget(ctxt context.Context, opts ...client.Option) (string, error) {
|
2017-01-24 15:09:23 +00:00
|
|
|
c.RLock()
|
|
|
|
cl := c.r.Client(opts...)
|
|
|
|
c.RUnlock()
|
|
|
|
|
|
|
|
// new page target
|
|
|
|
t, err := cl.NewPageTarget(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.After(DefaultNewTargetTimeout)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
var ok bool
|
|
|
|
id := t.GetID()
|
|
|
|
c.RLock()
|
|
|
|
_, ok = c.handlerMap[id]
|
|
|
|
c.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(DefaultCheckDuration)
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
2017-02-12 04:59:33 +00:00
|
|
|
return "", ctxt.Err()
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
case <-timeout:
|
2017-11-25 20:15:28 +00:00
|
|
|
return "", errors.New("timeout waiting for new target to be available")
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTarget is an action that sets the active Chrome handler to the specified
|
|
|
|
// index i.
|
|
|
|
func (c *CDP) SetTarget(i int) Action {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(context.Context, cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
return c.SetHandler(i)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTargetByID is an action that sets the active Chrome handler to the handler
|
|
|
|
// associated with the specified id.
|
|
|
|
func (c *CDP) SetTargetByID(id string) Action {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(context.Context, cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
return c.SetHandlerByID(id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTarget is an action that creates a new Chrome target, and sets it as the
|
|
|
|
// active target.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (c *CDP) NewTarget(id *string, opts ...client.Option) Action {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
n, err := c.newTarget(ctxt, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != nil {
|
|
|
|
*id = n
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseByIndex closes the Chrome target with specified index i.
|
|
|
|
func (c *CDP) CloseByIndex(i int) Action {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseByID closes the Chrome target with the specified id.
|
|
|
|
func (c *CDP) CloseByID(id string) Action {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the action against the current target using the supplied
|
|
|
|
// context.
|
|
|
|
func (c *CDP) Run(ctxt context.Context, a Action) error {
|
|
|
|
c.RLock()
|
|
|
|
cur := c.cur
|
|
|
|
c.RUnlock()
|
|
|
|
|
|
|
|
return a.Do(ctxt, cur)
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:46:30 +00:00
|
|
|
// Option is a Chrome DevTools Protocol option.
|
2017-01-24 15:09:23 +00:00
|
|
|
type Option func(*CDP) error
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// WithRunner is a CDP option to specify the underlying Chrome runner to
|
|
|
|
// monitor for page handlers.
|
2017-01-24 15:09:23 +00:00
|
|
|
func WithRunner(r *runner.Runner) Option {
|
|
|
|
return func(c *CDP) error {
|
|
|
|
c.r = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// WithTargets is a CDP option to specify the incoming targets to monitor for
|
|
|
|
// page handlers.
|
2017-01-24 15:09:23 +00:00
|
|
|
func WithTargets(watch <-chan client.Target) Option {
|
|
|
|
return func(c *CDP) error {
|
|
|
|
c.watch = watch
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 03:17:19 +00:00
|
|
|
// WithClient is a CDP option to use the incoming targets from a client.
|
|
|
|
func WithClient(ctxt context.Context, cl *client.Client) Option {
|
|
|
|
return func(c *CDP) error {
|
|
|
|
return WithTargets(cl.WatchPageTargets(ctxt))(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithURL is a CDP option to use a client with the specified URL.
|
|
|
|
func WithURL(ctxt context.Context, urlstr string) Option {
|
|
|
|
return func(c *CDP) error {
|
|
|
|
return WithClient(ctxt, client.New(client.URL(urlstr)))(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// WithRunnerOptions is a CDP option to specify the options to pass to a newly
|
2017-01-24 15:09:23 +00:00
|
|
|
// created Chrome process runner.
|
|
|
|
func WithRunnerOptions(opts ...runner.CommandLineOption) Option {
|
|
|
|
return func(c *CDP) error {
|
|
|
|
c.opts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 09:00:25 +00:00
|
|
|
|
|
|
|
// WithLogf is a CDP option to specify a func to receive general logging.
|
2018-05-18 22:03:47 +00:00
|
|
|
func WithLogf(f func(string, ...interface{})) Option {
|
2017-02-13 09:00:25 +00:00
|
|
|
return func(c *CDP) error {
|
|
|
|
c.logf = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDebugf is a CDP option to specify a func to receive debug logging (ie,
|
|
|
|
// protocol information).
|
2018-05-18 22:03:47 +00:00
|
|
|
func WithDebugf(f func(string, ...interface{})) Option {
|
2017-02-13 09:00:25 +00:00
|
|
|
return func(c *CDP) error {
|
|
|
|
c.debugf = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithErrorf is a CDP option to specify a func to receive error logging.
|
2018-05-18 22:03:47 +00:00
|
|
|
func WithErrorf(f func(string, ...interface{})) Option {
|
2017-02-13 09:00:25 +00:00
|
|
|
return func(c *CDP) error {
|
2018-05-18 22:03:47 +00:00
|
|
|
c.errf = f
|
2017-02-13 09:00:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLog is a CDP option that sets the logging, debugging, and error funcs to
|
|
|
|
// f.
|
2018-05-18 22:03:47 +00:00
|
|
|
func WithLog(f func(string, ...interface{})) Option {
|
2017-02-13 09:00:25 +00:00
|
|
|
return func(c *CDP) error {
|
2018-05-18 22:03:47 +00:00
|
|
|
c.logf, c.debugf, c.errf = f, f, f
|
2017-02-13 09:00:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithConsolef is a CDP option to specify a func to receive chrome log events.
|
|
|
|
//
|
|
|
|
// Note: NOT YET IMPLEMENTED.
|
2018-05-18 22:03:47 +00:00
|
|
|
func WithConsolef(f func(string, ...interface{})) Option {
|
2017-02-13 09:00:25 +00:00
|
|
|
return func(c *CDP) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 00:11:42 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// defaultNewTargetTimeout is the default target timeout -- used by
|
|
|
|
// testing.
|
|
|
|
defaultNewTargetTimeout = DefaultNewTargetTimeout
|
|
|
|
)
|