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-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a browser context using the parent context.
|
|
|
|
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
|
|
|
|
// don't inherit SessionID, so that NewContext can be used to
|
|
|
|
// 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-08 10:10:59 +00:00
|
|
|
c.Allocator = setupExecAllocator(
|
2019-03-05 13:14:50 +00:00
|
|
|
NoFirstRun,
|
|
|
|
NoDefaultBrowserCheck,
|
|
|
|
Headless,
|
2019-04-08 10:10:59 +00:00
|
|
|
)
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, contextKey{}, c)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if err := action.Do(ctx, c.Browser); err != nil {
|
|
|
|
c.Browser.errf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if id := c.Target.TargetID; id != "" {
|
|
|
|
action := target.CloseTarget(id)
|
|
|
|
if _, err := action.Do(ctx, c.Browser); err != nil {
|
|
|
|
c.Browser.errf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-21 15:21:52 +00:00
|
|
|
// Run runs an action against the provided context. The provided context must
|
2019-04-08 10:10:59 +00:00
|
|
|
// contain a valid Allocator; typically, that will be created via NewContext, or
|
|
|
|
// via one of the allocator constructors like NewExecAllocator.
|
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)
|
|
|
|
if c == nil || c.Allocator == nil {
|
|
|
|
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
|
|
|
c.wg.Add(1)
|
|
|
|
|
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(),
|
|
|
|
//network.Enable(),
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|