2019-03-05 13:14:50 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-15 17:17:57 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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 {
|
|
|
|
Allocator Allocator
|
|
|
|
|
2019-03-21 15:44:28 +00:00
|
|
|
Browser *Browser
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-04-01 13:31:11 +00:00
|
|
|
Target *Target
|
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)
|
|
|
|
|
|
|
|
c := &Context{}
|
|
|
|
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-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
if c.Allocator == nil {
|
|
|
|
WithExecAllocator(
|
|
|
|
NoFirstRun,
|
|
|
|
NoDefaultBrowserCheck,
|
|
|
|
Headless,
|
|
|
|
)(&c.Allocator)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, contextKey{}, c)
|
|
|
|
return ctx, cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// contain a valid Allocator; typically, that will be created via NewContext or
|
|
|
|
// NewAllocator.
|
2019-03-05 13:14:50 +00:00
|
|
|
func Run(ctx context.Context, action Action) error {
|
|
|
|
c := FromContext(ctx)
|
|
|
|
if c == nil || c.Allocator == nil {
|
|
|
|
return ErrInvalidContext
|
|
|
|
}
|
2019-03-21 15:44:28 +00:00
|
|
|
if c.Browser == nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
browser, err := c.Allocator.Allocate(ctx)
|
|
|
|
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-03-15 17:17:57 +00:00
|
|
|
if err := c.newSession(ctx); err != nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 13:31:11 +00:00
|
|
|
return action.Do(ctx, c.Target)
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
func (c *Context) newSession(ctx context.Context) error {
|
|
|
|
create := target.CreateTarget("about:blank")
|
2019-03-21 15:44:28 +00:00
|
|
|
targetID, err := create.Do(ctx, c.Browser)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
|
|
|
|
attach := target.AttachToTarget(targetID)
|
2019-03-21 15:44:28 +00:00
|
|
|
sessionID, err := attach.Do(ctx, c.Browser)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
|
2019-04-01 13:31:11 +00:00
|
|
|
c.Target = c.Browser.newExecutorForTarget(ctx, 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
|
|
|
}
|
|
|
|
|
|
|
|
type ContextOption func(*Context)
|