2017-01-24 15:09:23 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-27 02:30:28 +00:00
|
|
|
"encoding/json"
|
2019-03-15 17:17:57 +00:00
|
|
|
"sync/atomic"
|
2017-01-24 15:09:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mailru/easyjson"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto"
|
|
|
|
"github.com/chromedp/cdproto/cdp"
|
|
|
|
"github.com/chromedp/cdproto/dom"
|
|
|
|
"github.com/chromedp/cdproto/inspector"
|
|
|
|
"github.com/chromedp/cdproto/page"
|
2019-03-15 17:17:57 +00:00
|
|
|
"github.com/chromedp/cdproto/target"
|
2017-01-24 15:09:23 +00:00
|
|
|
)
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// Target manages a Chrome DevTools Protocol target.
|
|
|
|
type Target struct {
|
|
|
|
browser *Browser
|
|
|
|
sessionID target.SessionID
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
waitQueue chan func(cur *cdp.Frame) bool
|
|
|
|
eventQueue chan *cdproto.Message
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// below are the old TargetHandler fields.
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// frames is the set of encountered frames.
|
|
|
|
frames map[cdp.FrameID]*cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-20 11:23:15 +00:00
|
|
|
// cur is the current top level frame.
|
|
|
|
cur *cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// logging funcs
|
2019-03-15 17:17:57 +00:00
|
|
|
logf, errf func(string, ...interface{})
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
func (t *Target) run(ctx context.Context) {
|
2017-01-24 15:09:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-03-15 17:17:57 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case msg := <-t.eventQueue:
|
|
|
|
if err := t.processEvent(ctx, msg); err != nil {
|
|
|
|
t.errf("could not process event: %v", err)
|
|
|
|
continue
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
default:
|
|
|
|
// prevent busy spinning. TODO: do better
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
n := len(t.waitQueue)
|
|
|
|
if n == 0 {
|
|
|
|
continue
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2019-03-20 11:23:15 +00:00
|
|
|
if t.cur == nil {
|
2019-03-15 17:17:57 +00:00
|
|
|
continue
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
fn := <-t.waitQueue
|
2019-03-20 11:23:15 +00:00
|
|
|
if !fn(t.cur) {
|
2019-03-15 17:17:57 +00:00
|
|
|
// try again later.
|
|
|
|
t.waitQueue <- fn
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
func (t *Target) Execute(ctx context.Context, method string, params json.Marshaler, res json.Unmarshaler) error {
|
|
|
|
paramsMsg := emptyObj
|
|
|
|
if params != nil {
|
|
|
|
var err error
|
|
|
|
if paramsMsg, err = json.Marshal(params); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
innerID := atomic.AddInt64(&t.browser.next, 1)
|
|
|
|
msg := &cdproto.Message{
|
|
|
|
ID: innerID,
|
|
|
|
Method: cdproto.MethodType(method),
|
|
|
|
Params: paramsMsg,
|
|
|
|
}
|
|
|
|
msgJSON, err := json.Marshal(msg)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2019-03-15 17:17:57 +00:00
|
|
|
return err
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2019-03-15 17:17:57 +00:00
|
|
|
sendParams := target.SendMessageToTarget(string(msgJSON)).
|
|
|
|
WithSessionID(t.sessionID)
|
|
|
|
sendParamsJSON, _ := json.Marshal(sendParams)
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// We want to grab the response from the inner message.
|
|
|
|
ch := make(chan *cdproto.Message, 1)
|
|
|
|
t.browser.cmdQueue <- cmdJob{
|
|
|
|
msg: &cdproto.Message{ID: innerID},
|
|
|
|
resp: ch,
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// The response from the outer message is uninteresting; pass a nil
|
|
|
|
// resp channel.
|
|
|
|
outerID := atomic.AddInt64(&t.browser.next, 1)
|
|
|
|
t.browser.cmdQueue <- cmdJob{
|
|
|
|
msg: &cdproto.Message{
|
|
|
|
ID: outerID,
|
|
|
|
Method: target.CommandSendMessageToTarget,
|
|
|
|
Params: sendParamsJSON,
|
|
|
|
},
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
select {
|
|
|
|
case msg := <-ch:
|
|
|
|
switch {
|
|
|
|
case msg == nil:
|
|
|
|
return ErrChannelClosed
|
|
|
|
case msg.Error != nil:
|
|
|
|
return msg.Error
|
|
|
|
case res != nil:
|
|
|
|
return json.Unmarshal(msg.Result, res)
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
// below are the old TargetHandler methods.
|
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
// processEvent processes an incoming event.
|
2019-03-20 12:08:50 +00:00
|
|
|
func (t *Target) processEvent(ctx context.Context, msg *cdproto.Message) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
if msg == nil {
|
2017-12-27 02:30:28 +00:00
|
|
|
return ErrChannelClosed
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2019-03-28 21:44:07 +00:00
|
|
|
switch msg.Method {
|
|
|
|
case "Page.frameClearedScheduledNavigation",
|
|
|
|
"Page.frameScheduledNavigation":
|
|
|
|
// These events are now deprecated, and UnmarshalMessage panics
|
|
|
|
// when they are received from Chrome. For now, to avoid panics
|
|
|
|
// and compile errors, and to fix chromedp v0 when installed via
|
|
|
|
// 'go get -u', skip the events here.
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// unmarshal
|
2017-12-27 02:30:28 +00:00
|
|
|
ev, err := cdproto.UnmarshalMessage(msg)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
switch ev.(type) {
|
2017-01-24 15:09:23 +00:00
|
|
|
case *inspector.EventDetached:
|
|
|
|
return nil
|
|
|
|
case *dom.EventDocumentUpdated:
|
2019-03-20 12:08:50 +00:00
|
|
|
t.documentUpdated(ctx)
|
2017-01-24 15:09:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
switch msg.Method.Domain() {
|
2017-01-24 15:09:23 +00:00
|
|
|
case "Page":
|
2019-03-20 12:06:52 +00:00
|
|
|
t.pageEvent(ev)
|
2017-01-24 15:09:23 +00:00
|
|
|
case "DOM":
|
2019-03-20 12:06:52 +00:00
|
|
|
t.domEvent(ev)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// documentUpdated handles the document updated event, retrieving the document
|
|
|
|
// root for the root frame.
|
2019-03-20 12:08:50 +00:00
|
|
|
func (t *Target) documentUpdated(ctx context.Context) {
|
2019-03-15 17:17:57 +00:00
|
|
|
f := t.cur
|
2017-01-24 15:09:23 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
// invalidate nodes
|
|
|
|
if f.Root != nil {
|
|
|
|
close(f.Root.Invalidated)
|
|
|
|
}
|
|
|
|
|
2017-01-26 07:28:34 +00:00
|
|
|
f.Nodes = make(map[cdp.NodeID]*cdp.Node)
|
2019-03-15 17:17:57 +00:00
|
|
|
var err error
|
2019-03-20 12:08:50 +00:00
|
|
|
f.Root, err = dom.GetDocument().WithPierce(true).Do(ctx, t)
|
2019-03-15 17:17:57 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return // TODO: perhaps not necessary, but useful to keep the tests less noisy
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2019-03-15 17:17:57 +00:00
|
|
|
t.errf("could not retrieve document root for %s: %v", f.ID, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Root.Invalidated = make(chan struct{})
|
|
|
|
walk(f.Nodes, f.Root)
|
|
|
|
}
|
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
// emptyObj is an empty JSON object message.
|
|
|
|
var emptyObj = easyjson.RawMessage([]byte(`{}`))
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// pageEvent handles incoming page events.
|
2019-03-20 12:06:52 +00:00
|
|
|
func (t *Target) pageEvent(ev interface{}) {
|
2017-01-26 07:28:34 +00:00
|
|
|
var id cdp.FrameID
|
2017-02-12 07:08:40 +00:00
|
|
|
var op frameOp
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
switch e := ev.(type) {
|
|
|
|
case *page.EventFrameNavigated:
|
2019-03-15 17:17:57 +00:00
|
|
|
t.frames[e.Frame.ID] = e.Frame
|
|
|
|
t.cur = e.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case *page.EventFrameAttached:
|
|
|
|
id, op = e.FrameID, frameAttached(e.ParentFrameID)
|
|
|
|
|
|
|
|
case *page.EventFrameDetached:
|
|
|
|
id, op = e.FrameID, frameDetached
|
|
|
|
|
|
|
|
case *page.EventFrameStartedLoading:
|
2019-03-28 21:21:52 +00:00
|
|
|
id, op = e.FrameID, frameStartedLoading
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
case *page.EventFrameStoppedLoading:
|
|
|
|
id, op = e.FrameID, frameStoppedLoading
|
|
|
|
|
2017-12-18 04:03:22 +00:00
|
|
|
// ignored events
|
2019-03-28 21:44:07 +00:00
|
|
|
case *page.EventFrameRequestedNavigation:
|
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
case *page.EventDomContentEventFired:
|
|
|
|
return
|
|
|
|
case *page.EventLoadEventFired:
|
|
|
|
return
|
|
|
|
case *page.EventFrameResized:
|
|
|
|
return
|
2017-12-18 04:03:22 +00:00
|
|
|
case *page.EventLifecycleEvent:
|
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
default:
|
2019-03-15 17:17:57 +00:00
|
|
|
t.errf("unhandled page event %T", ev)
|
2017-02-13 09:00:25 +00:00
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
f := t.frames[id]
|
2019-03-28 21:21:52 +00:00
|
|
|
if f == nil {
|
|
|
|
// This can happen if a frame is attached or starts loading
|
|
|
|
// before it's ever navigated to. We won't have all the frame
|
|
|
|
// details just yet, but that's okay.
|
|
|
|
f = &cdp.Frame{ID: id}
|
|
|
|
t.frames[id] = f
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
op(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// domEvent handles incoming DOM events.
|
2019-03-20 12:06:52 +00:00
|
|
|
func (t *Target) domEvent(ev interface{}) {
|
2019-03-15 17:17:57 +00:00
|
|
|
f := t.cur
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-01-26 07:28:34 +00:00
|
|
|
var id cdp.NodeID
|
2017-02-12 07:08:40 +00:00
|
|
|
var op nodeOp
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
switch e := ev.(type) {
|
|
|
|
case *dom.EventSetChildNodes:
|
|
|
|
id, op = e.ParentID, setChildNodes(f.Nodes, e.Nodes)
|
|
|
|
|
|
|
|
case *dom.EventAttributeModified:
|
|
|
|
id, op = e.NodeID, attributeModified(e.Name, e.Value)
|
|
|
|
|
|
|
|
case *dom.EventAttributeRemoved:
|
|
|
|
id, op = e.NodeID, attributeRemoved(e.Name)
|
|
|
|
|
|
|
|
case *dom.EventInlineStyleInvalidated:
|
2017-03-28 00:42:47 +00:00
|
|
|
if len(e.NodeIds) == 0 {
|
2017-03-27 23:59:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
id, op = e.NodeIds[0], inlineStyleInvalidated(e.NodeIds[1:])
|
|
|
|
|
|
|
|
case *dom.EventCharacterDataModified:
|
|
|
|
id, op = e.NodeID, characterDataModified(e.CharacterData)
|
|
|
|
|
|
|
|
case *dom.EventChildNodeCountUpdated:
|
|
|
|
id, op = e.NodeID, childNodeCountUpdated(e.ChildNodeCount)
|
|
|
|
|
|
|
|
case *dom.EventChildNodeInserted:
|
|
|
|
id, op = e.ParentNodeID, childNodeInserted(f.Nodes, e.PreviousNodeID, e.Node)
|
|
|
|
|
|
|
|
case *dom.EventChildNodeRemoved:
|
|
|
|
id, op = e.ParentNodeID, childNodeRemoved(f.Nodes, e.NodeID)
|
|
|
|
|
|
|
|
case *dom.EventShadowRootPushed:
|
|
|
|
id, op = e.HostID, shadowRootPushed(f.Nodes, e.Root)
|
|
|
|
|
|
|
|
case *dom.EventShadowRootPopped:
|
|
|
|
id, op = e.HostID, shadowRootPopped(f.Nodes, e.RootID)
|
|
|
|
|
|
|
|
case *dom.EventPseudoElementAdded:
|
|
|
|
id, op = e.ParentID, pseudoElementAdded(f.Nodes, e.PseudoElement)
|
|
|
|
|
|
|
|
case *dom.EventPseudoElementRemoved:
|
|
|
|
id, op = e.ParentID, pseudoElementRemoved(f.Nodes, e.PseudoElementID)
|
|
|
|
|
|
|
|
case *dom.EventDistributedNodesUpdated:
|
|
|
|
id, op = e.InsertionPointID, distributedNodesUpdated(e.DistributedNodes)
|
|
|
|
|
|
|
|
default:
|
2019-03-15 17:17:57 +00:00
|
|
|
t.errf("unhandled node event %T", ev)
|
2017-02-13 09:00:25 +00:00
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
n, ok := f.Nodes[id]
|
|
|
|
if !ok {
|
|
|
|
// Node ID has been invalidated. Nothing to do.
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
op(n)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
type TargetOption func(*Target)
|