2019-04-08 10:33:08 +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.
|
|
|
|
//
|
|
|
|
// chromedp requires no third-party dependencies, implementing the async Chrome
|
|
|
|
// DevTools Protocol entirely in Go.
|
2019-03-05 13:14:50 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-15 17:17:57 +00:00
|
|
|
"fmt"
|
2019-04-07 12:17:15 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-03-15 17:17:57 +00:00
|
|
|
|
|
|
|
"github.com/chromedp/cdproto/css"
|
|
|
|
"github.com/chromedp/cdproto/dom"
|
|
|
|
"github.com/chromedp/cdproto/inspector"
|
|
|
|
"github.com/chromedp/cdproto/log"
|
|
|
|
"github.com/chromedp/cdproto/page"
|
|
|
|
"github.com/chromedp/cdproto/runtime"
|
|
|
|
"github.com/chromedp/cdproto/target"
|
2019-03-05 13:14:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// Context is attached to any context.Context which is valid for use with Run.
|
2019-03-05 13:14:50 +00:00
|
|
|
type Context struct {
|
2019-04-07 12:17:15 +00:00
|
|
|
// Allocator is used to create new browsers. It is inherited from the
|
|
|
|
// parent context when using NewContext.
|
2019-03-05 13:14:50 +00:00
|
|
|
Allocator Allocator
|
|
|
|
|
2019-04-07 12:17:15 +00:00
|
|
|
// Browser is the browser being used in the context. It is inherited
|
|
|
|
// from the parent context when using NewContext.
|
2019-03-21 15:44:28 +00:00
|
|
|
Browser *Browser
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-04-07 12:17:15 +00:00
|
|
|
// Target is the target to run actions (commands) against. It is not
|
|
|
|
// inherited from the parent context, and typically each context will
|
|
|
|
// have its own unique Target pointing to a separate browser tab (page).
|
2019-04-01 13:31:11 +00:00
|
|
|
Target *Target
|
2019-04-07 12:17:15 +00:00
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
// browserOpts holds the browser options passed to NewContext via
|
|
|
|
// WithBrowserOption, so that they can later be used when allocating a
|
|
|
|
// browser in Run.
|
|
|
|
browserOpts []BrowserOption
|
|
|
|
|
2019-04-07 17:25:03 +00:00
|
|
|
// cancel simply cancels the context that was used to start Browser.
|
|
|
|
// This is useful to stop all activity and avoid deadlocks if we detect
|
|
|
|
// that the browser was closed or happened to crash. Note that this
|
|
|
|
// cancel function doesn't do any waiting.
|
|
|
|
cancel func()
|
|
|
|
|
2019-04-07 12:17:15 +00:00
|
|
|
// first records whether this context was the one that allocated
|
|
|
|
// Browser. This is important, because its cancellation will stop the
|
|
|
|
// entire browser handler, meaning that no further actions can be
|
|
|
|
// executed.
|
|
|
|
first bool
|
|
|
|
|
|
|
|
// wg allows waiting for a target to be closed on cancellation.
|
|
|
|
wg sync.WaitGroup
|
2019-04-08 16:52:14 +00:00
|
|
|
|
|
|
|
// cancelErr is the first error encountered when cancelling this
|
|
|
|
// context, for example if a browser's temporary user data directory
|
|
|
|
// couldn't be deleted.
|
|
|
|
cancelErr error
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 04:24:19 +00:00
|
|
|
// NewContext creates a chromedp context from the parent context. The parent
|
|
|
|
// context's Allocator is inherited, defaulting to an ExecAllocator with
|
|
|
|
// DefaultExecAllocatorOptions.
|
|
|
|
//
|
|
|
|
// If the parent context contains an allocated Browser, the child context
|
|
|
|
// inherits it, and its first Run creates a new tab on that browser. Otherwise,
|
|
|
|
// its first Run will allocate a new browser.
|
|
|
|
//
|
|
|
|
// Cancelling the returned context will close a tab or an entire browser,
|
|
|
|
// depending on the logic described above. To cancel a context while checking
|
|
|
|
// for errors, see Cancel.
|
2019-03-05 13:14:50 +00:00
|
|
|
func NewContext(parent context.Context, opts ...ContextOption) (context.Context, context.CancelFunc) {
|
|
|
|
ctx, cancel := context.WithCancel(parent)
|
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
c := &Context{cancel: cancel, first: true}
|
2019-03-05 13:14:50 +00:00
|
|
|
if pc := FromContext(parent); pc != nil {
|
|
|
|
c.Allocator = pc.Allocator
|
2019-03-21 20:24:09 +00:00
|
|
|
c.Browser = pc.Browser
|
2019-04-18 06:07:33 +00:00
|
|
|
// don't inherit Target, so that NewContext can be used to
|
2019-03-21 20:24:09 +00:00
|
|
|
// create a new tab on the same browser.
|
2019-04-08 10:56:16 +00:00
|
|
|
|
|
|
|
c.first = c.Browser == nil
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
if c.Allocator == nil {
|
2019-04-17 04:17:01 +00:00
|
|
|
c.Allocator = setupExecAllocator(DefaultExecAllocatorOptions...)
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, contextKey{}, c)
|
2019-04-18 06:07:33 +00:00
|
|
|
c.wg.Add(1)
|
2019-04-07 12:17:15 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
if c.first {
|
|
|
|
// This is the original browser tab, so the entire
|
|
|
|
// browser will already be cleaned up elsewhere.
|
|
|
|
c.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-18 06:07:33 +00:00
|
|
|
if c.Target == nil {
|
|
|
|
// This is a new tab, but we didn't create it and attach
|
|
|
|
// to it yet. Nothing to do.
|
|
|
|
c.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:17:15 +00:00
|
|
|
// Not the original browser tab; simply detach and close it.
|
|
|
|
// We need a new context, as ctx is cancelled; use a 1s timeout.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if id := c.Target.SessionID; id != "" {
|
|
|
|
action := target.DetachFromTarget().WithSessionID(id)
|
2019-04-08 16:52:14 +00:00
|
|
|
if err := action.Do(ctx, c.Browser); c.cancelErr == nil {
|
|
|
|
c.cancelErr = err
|
2019-04-07 12:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if id := c.Target.TargetID; id != "" {
|
|
|
|
action := target.CloseTarget(id)
|
2019-04-08 16:52:14 +00:00
|
|
|
if ok, err := action.Do(ctx, c.Browser); c.cancelErr == nil {
|
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("could not close target %q", id)
|
|
|
|
}
|
|
|
|
c.cancelErr = err
|
2019-04-07 12:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
|
|
|
cancelWait := func() {
|
|
|
|
cancel()
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
return ctx, cancelWait
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type contextKey struct{}
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// FromContext extracts the Context data stored inside a context.Context.
|
2019-03-05 13:14:50 +00:00
|
|
|
func FromContext(ctx context.Context) *Context {
|
|
|
|
c, _ := ctx.Value(contextKey{}).(*Context)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:03:00 +00:00
|
|
|
// Cancel cancels a chromedp context, waits for its resources to be cleaned up,
|
|
|
|
// and returns any error encountered during that process.
|
|
|
|
//
|
|
|
|
// Usually a "defer cancel()" will be enough for most use cases. This API is
|
|
|
|
// useful if you want to catch underlying cancel errors, such as when a
|
|
|
|
// temporary directory cannot be deleted.
|
|
|
|
func Cancel(ctx context.Context) error {
|
2019-04-08 16:52:14 +00:00
|
|
|
c := FromContext(ctx)
|
|
|
|
if c == nil {
|
|
|
|
return ErrInvalidContext
|
|
|
|
}
|
2019-04-09 11:03:00 +00:00
|
|
|
c.cancel()
|
|
|
|
c.wg.Wait()
|
2019-04-08 16:52:14 +00:00
|
|
|
return c.cancelErr
|
|
|
|
}
|
|
|
|
|
2019-04-16 05:26:36 +00:00
|
|
|
// Run runs an action against context. The provided context must be a valid
|
|
|
|
// chromedp context, typically created via NewContext.
|
2019-04-01 15:57:22 +00:00
|
|
|
func Run(ctx context.Context, actions ...Action) error {
|
2019-03-05 13:14:50 +00:00
|
|
|
c := FromContext(ctx)
|
2019-04-14 10:56:09 +00:00
|
|
|
// If c is nil, it's not a chromedp context.
|
|
|
|
// If c.Allocator is nil, NewContext wasn't used properly.
|
|
|
|
// If c.cancel is nil, Run is being called directly with an allocator
|
|
|
|
// context.
|
|
|
|
if c == nil || c.Allocator == nil || c.cancel == nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
return ErrInvalidContext
|
|
|
|
}
|
2019-04-07 16:49:53 +00:00
|
|
|
if c.Browser == nil {
|
2019-04-08 10:56:16 +00:00
|
|
|
browser, err := c.Allocator.Allocate(ctx, c.browserOpts...)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-21 15:44:28 +00:00
|
|
|
c.Browser = browser
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
2019-04-01 13:31:11 +00:00
|
|
|
if c.Target == nil {
|
2019-04-07 12:17:15 +00:00
|
|
|
if err := c.newSession(ctx); err != nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 15:57:22 +00:00
|
|
|
return Tasks(actions).Do(ctx, c.Target)
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:17:15 +00:00
|
|
|
func (c *Context) newSession(ctx context.Context) error {
|
2019-04-06 20:32:02 +00:00
|
|
|
var targetID target.ID
|
2019-04-07 12:17:15 +00:00
|
|
|
if c.first {
|
2019-04-06 20:32:02 +00:00
|
|
|
// If we just allocated this browser, and it has a single page
|
|
|
|
// that's blank and not attached, use it.
|
|
|
|
infos, err := target.GetTargets().Do(ctx, c.Browser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pages := 0
|
|
|
|
for _, info := range infos {
|
|
|
|
if info.Type == "page" && info.URL == "about:blank" && !info.Attached {
|
|
|
|
targetID = info.TargetID
|
|
|
|
pages++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pages > 1 {
|
|
|
|
// Multiple blank pages; just in case, don't use any.
|
|
|
|
targetID = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetID == "" {
|
|
|
|
var err error
|
|
|
|
targetID, err = target.CreateTarget("about:blank").Do(ctx, c.Browser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
|
2019-04-01 16:12:17 +00:00
|
|
|
sessionID, err := target.AttachToTarget(targetID).Do(ctx, c.Browser)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-07 12:17:15 +00:00
|
|
|
|
2019-04-07 11:36:48 +00:00
|
|
|
c.Target = c.Browser.newExecutorForTarget(ctx, targetID, sessionID)
|
2019-03-15 17:17:57 +00:00
|
|
|
|
|
|
|
// enable domains
|
|
|
|
for _, enable := range []Action{
|
|
|
|
log.Enable(),
|
|
|
|
runtime.Enable(),
|
2019-04-09 11:03:00 +00:00
|
|
|
// network.Enable(),
|
2019-03-15 17:17:57 +00:00
|
|
|
inspector.Enable(),
|
|
|
|
page.Enable(),
|
|
|
|
dom.Enable(),
|
|
|
|
css.Enable(),
|
|
|
|
} {
|
2019-04-01 13:31:11 +00:00
|
|
|
if err := enable.Do(ctx, c.Target); err != nil {
|
2019-03-15 17:17:57 +00:00
|
|
|
return fmt.Errorf("unable to execute %T: %v", enable, err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
return nil
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
// ContextOption is a context option.
|
2019-03-05 13:14:50 +00:00
|
|
|
type ContextOption func(*Context)
|
2019-04-06 20:32:02 +00:00
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
// WithLogf is a shortcut for WithBrowserOption(WithBrowserLogf(f)).
|
|
|
|
func WithLogf(f func(string, ...interface{})) ContextOption {
|
|
|
|
return WithBrowserOption(WithBrowserLogf(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithErrorf is a shortcut for WithBrowserOption(WithBrowserErrorf(f)).
|
|
|
|
func WithErrorf(f func(string, ...interface{})) ContextOption {
|
|
|
|
return WithBrowserOption(WithBrowserErrorf(f))
|
|
|
|
}
|
|
|
|
|
2019-04-09 08:11:14 +00:00
|
|
|
// WithDebugf is a shortcut for WithBrowserOption(WithBrowserDebugf(f)).
|
|
|
|
func WithDebugf(f func(string, ...interface{})) ContextOption {
|
|
|
|
return WithBrowserOption(WithBrowserDebugf(f))
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
// WithBrowserOption allows passing a number of browser options to the allocator
|
|
|
|
// when allocating a new browser. As such, this context option can only be used
|
|
|
|
// when NewContext is allocating a new browser.
|
|
|
|
func WithBrowserOption(opts ...BrowserOption) ContextOption {
|
|
|
|
return func(c *Context) {
|
|
|
|
if !c.first {
|
|
|
|
panic("WithBrowserOption can only be used when allocating a new browser")
|
|
|
|
}
|
|
|
|
c.browserOpts = append(c.browserOpts, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:32:02 +00:00
|
|
|
// Targets lists all the targets in the browser attached to the given context.
|
|
|
|
func Targets(ctx context.Context) ([]*target.Info, error) {
|
|
|
|
// Don't rely on Run, as that needs to be able to call Targets, and we
|
|
|
|
// don't want cyclic func calls.
|
|
|
|
c := FromContext(ctx)
|
|
|
|
if c == nil || c.Allocator == nil {
|
|
|
|
return nil, ErrInvalidContext
|
|
|
|
}
|
|
|
|
if c.Browser == nil {
|
2019-04-08 10:56:16 +00:00
|
|
|
browser, err := c.Allocator.Allocate(ctx, c.browserOpts...)
|
2019-04-06 20:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Browser = browser
|
|
|
|
}
|
|
|
|
return target.GetTargets().Do(ctx, c.Browser)
|
|
|
|
}
|