2017-01-24 15:09:23 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mailru/easyjson"
|
|
|
|
|
2017-01-26 07:28:34 +00:00
|
|
|
"github.com/knq/chromedp/cdp"
|
2017-02-18 08:36:24 +00:00
|
|
|
"github.com/knq/chromedp/cdp/cdputil"
|
2017-01-24 15:09:23 +00:00
|
|
|
"github.com/knq/chromedp/cdp/css"
|
|
|
|
"github.com/knq/chromedp/cdp/dom"
|
|
|
|
"github.com/knq/chromedp/cdp/inspector"
|
|
|
|
logdom "github.com/knq/chromedp/cdp/log"
|
|
|
|
"github.com/knq/chromedp/cdp/page"
|
|
|
|
rundom "github.com/knq/chromedp/cdp/runtime"
|
|
|
|
"github.com/knq/chromedp/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TargetHandler manages a Chrome Debugging Protocol target.
|
|
|
|
type TargetHandler struct {
|
|
|
|
conn client.Transport
|
|
|
|
|
|
|
|
// frames is the set of encountered frames.
|
2017-01-26 07:28:34 +00:00
|
|
|
frames map[cdp.FrameID]*cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// cur is the current top level frame.
|
2017-01-26 07:28:34 +00:00
|
|
|
cur *cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// qcmd is the outgoing message queue.
|
2017-01-26 07:28:34 +00:00
|
|
|
qcmd chan *cdp.Message
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// qres is the incoming command result queue.
|
2017-01-26 07:28:34 +00:00
|
|
|
qres chan *cdp.Message
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// qevents is the incoming event queue.
|
2017-01-26 07:28:34 +00:00
|
|
|
qevents chan *cdp.Message
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// detached is closed when the detached event is received.
|
|
|
|
detached chan *inspector.EventDetached
|
|
|
|
|
|
|
|
pageWaitGroup, domWaitGroup *sync.WaitGroup
|
|
|
|
|
|
|
|
// last is the last sent message identifier.
|
|
|
|
last int64
|
|
|
|
lastm sync.Mutex
|
|
|
|
|
|
|
|
// res is the id->result channel map.
|
2017-02-15 14:46:07 +00:00
|
|
|
res map[int64]chan *cdp.Message
|
2017-01-24 15:09:23 +00:00
|
|
|
resrw sync.RWMutex
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
// logging funcs
|
|
|
|
logf, debugf, errorf LogFunc
|
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// NewTargetHandler creates a new handler for the specified client target.
|
2017-02-13 09:00:25 +00:00
|
|
|
func NewTargetHandler(t client.Target, logf, debugf, errorf LogFunc) (*TargetHandler, error) {
|
2017-01-24 15:09:23 +00:00
|
|
|
conn, err := client.Dial(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
return &TargetHandler{
|
|
|
|
conn: conn,
|
|
|
|
logf: logf,
|
|
|
|
debugf: debugf,
|
|
|
|
errorf: errorf,
|
|
|
|
}, nil
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Run starts the processing of commands and events of the client target
|
2017-01-24 15:09:23 +00:00
|
|
|
// provided to NewTargetHandler.
|
|
|
|
//
|
|
|
|
// Callers can stop Run by closing the passed context.
|
|
|
|
func (h *TargetHandler) Run(ctxt context.Context) error {
|
|
|
|
// reset
|
|
|
|
h.Lock()
|
2017-01-26 07:28:34 +00:00
|
|
|
h.frames = make(map[cdp.FrameID]*cdp.Frame)
|
|
|
|
h.qcmd = make(chan *cdp.Message)
|
|
|
|
h.qres = make(chan *cdp.Message)
|
|
|
|
h.qevents = make(chan *cdp.Message)
|
2017-02-15 14:46:07 +00:00
|
|
|
h.res = make(map[int64]chan *cdp.Message)
|
2017-01-24 15:09:23 +00:00
|
|
|
h.detached = make(chan *inspector.EventDetached)
|
|
|
|
h.pageWaitGroup = new(sync.WaitGroup)
|
|
|
|
h.domWaitGroup = new(sync.WaitGroup)
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
// run
|
|
|
|
go h.run(ctxt)
|
|
|
|
|
|
|
|
// enable domains
|
|
|
|
for _, a := range []Action{
|
|
|
|
logdom.Enable(),
|
|
|
|
rundom.Enable(),
|
|
|
|
//network.Enable(),
|
|
|
|
inspector.Enable(),
|
|
|
|
page.Enable(),
|
|
|
|
dom.Enable(),
|
|
|
|
css.Enable(),
|
|
|
|
} {
|
2017-11-25 20:20:52 +00:00
|
|
|
if err := a.Do(ctxt, h); err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
return fmt.Errorf("unable to execute %s: %v", reflect.TypeOf(a), err)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
|
|
|
|
// get page resources
|
|
|
|
tree, err := page.GetResourceTree().Do(ctxt, h)
|
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
return fmt.Errorf("unable to get resource tree: %v", err)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.frames[tree.Frame.ID] = tree.Frame
|
|
|
|
h.cur = tree.Frame
|
|
|
|
|
|
|
|
for _, c := range tree.ChildFrames {
|
|
|
|
h.frames[c.Frame.ID] = c.Frame
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
h.documentUpdated(ctxt)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run handles the actual message processing to / from the web socket connection.
|
|
|
|
func (h *TargetHandler) run(ctxt context.Context) {
|
|
|
|
defer h.conn.Close()
|
|
|
|
|
|
|
|
// add cancel to context
|
|
|
|
ctxt, cancel := context.WithCancel(ctxt)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
msg, err := h.read()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case msg.Method != "":
|
|
|
|
h.qevents <- msg
|
|
|
|
|
|
|
|
case msg.ID != 0:
|
|
|
|
h.qres <- msg
|
|
|
|
|
|
|
|
default:
|
2017-02-13 09:00:25 +00:00
|
|
|
h.errorf("ignoring malformed incoming message (missing id or method): %#v", msg)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-h.detached:
|
|
|
|
// FIXME: should log when detached, and reason
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// process queues
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-h.qevents:
|
2017-11-25 20:20:52 +00:00
|
|
|
err := h.processEvent(ctxt, ev)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not process event %s: %v", ev.Method, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case res := <-h.qres:
|
2017-11-25 20:20:52 +00:00
|
|
|
err := h.processResult(res)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not process result for message %d: %v", res.ID, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case cmd := <-h.qcmd:
|
2017-11-25 20:20:52 +00:00
|
|
|
err := h.processCommand(cmd)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not process command message %d: %v", cmd.ID, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read reads a message from the client connection.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) read() (*cdp.Message, error) {
|
2017-01-24 15:09:23 +00:00
|
|
|
// read
|
|
|
|
buf, err := h.conn.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
h.debugf("-> %s", string(buf))
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
// unmarshal
|
2017-01-26 07:28:34 +00:00
|
|
|
msg := new(cdp.Message)
|
2017-01-24 15:09:23 +00:00
|
|
|
err = easyjson.Unmarshal(buf, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processEvent processes an incoming event.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) processEvent(ctxt context.Context, msg *cdp.Message) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
if msg == nil {
|
2017-01-26 07:28:34 +00:00
|
|
|
return cdp.ErrChannelClosed
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal
|
2017-02-18 08:36:24 +00:00
|
|
|
ev, err := cdputil.UnmarshalMessage(msg)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e := ev.(type) {
|
|
|
|
case *inspector.EventDetached:
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
h.detached <- e
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *dom.EventDocumentUpdated:
|
|
|
|
h.domWaitGroup.Wait()
|
|
|
|
go h.documentUpdated(ctxt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d := msg.Method.Domain()
|
|
|
|
if d != "Page" && d != "DOM" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch d {
|
|
|
|
case "Page":
|
|
|
|
h.pageWaitGroup.Add(1)
|
|
|
|
go h.pageEvent(ctxt, ev)
|
|
|
|
|
|
|
|
case "DOM":
|
|
|
|
h.domWaitGroup.Add(1)
|
|
|
|
go h.domEvent(ctxt, ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// documentUpdated handles the document updated event, retrieving the document
|
|
|
|
// root for the root frame.
|
|
|
|
func (h *TargetHandler) documentUpdated(ctxt context.Context) {
|
2017-02-18 08:36:24 +00:00
|
|
|
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not get current frame: %v", err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-01-24 15:09:23 +00:00
|
|
|
f.Root, err = dom.GetDocument().WithPierce(true).Do(ctxt, h)
|
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// processResult processes an incoming command result.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) processResult(msg *cdp.Message) error {
|
2017-02-14 08:41:23 +00:00
|
|
|
h.resrw.RLock()
|
|
|
|
defer h.resrw.RUnlock()
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
ch, ok := h.res[msg.ID]
|
2017-01-24 15:09:23 +00:00
|
|
|
if !ok {
|
2017-02-14 08:41:23 +00:00
|
|
|
return fmt.Errorf("id %d not present in res map", msg.ID)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2017-02-14 08:41:23 +00:00
|
|
|
defer close(ch)
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
ch <- msg
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processCommand writes a command to the client connection.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) processCommand(cmd *cdp.Message) error {
|
2017-02-14 08:41:23 +00:00
|
|
|
// marshal
|
2017-01-24 15:09:23 +00:00
|
|
|
buf, err := easyjson.Marshal(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:00:25 +00:00
|
|
|
h.debugf("<- %s", string(buf))
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
return h.conn.Write(buf)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
// Execute executes commandType against the endpoint passed to Run, using the
|
|
|
|
// provided context and params, decoding the result of the command to res.
|
|
|
|
func (h *TargetHandler) Execute(ctxt context.Context, commandType cdp.MethodType, params easyjson.Marshaler, res easyjson.Unmarshaler) error {
|
|
|
|
var paramsBuf easyjson.RawMessage
|
|
|
|
if params == nil {
|
|
|
|
paramsBuf = emptyObj
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
paramsBuf, err = easyjson.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
id := h.next()
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
// save channel
|
2017-02-15 14:46:07 +00:00
|
|
|
ch := make(chan *cdp.Message, 1)
|
2017-02-14 08:41:23 +00:00
|
|
|
h.resrw.Lock()
|
|
|
|
h.res[id] = ch
|
|
|
|
h.resrw.Unlock()
|
|
|
|
|
|
|
|
// queue message
|
|
|
|
h.qcmd <- &cdp.Message{
|
|
|
|
ID: id,
|
|
|
|
Method: commandType,
|
|
|
|
Params: paramsBuf,
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
errch := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(errch)
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
select {
|
2017-02-14 08:41:23 +00:00
|
|
|
case msg := <-ch:
|
2017-02-15 14:46:07 +00:00
|
|
|
switch {
|
|
|
|
case msg == nil:
|
|
|
|
errch <- cdp.ErrChannelClosed
|
|
|
|
|
|
|
|
case msg.Error != nil:
|
|
|
|
errch <- msg.Error
|
|
|
|
|
|
|
|
case res != nil:
|
|
|
|
errch <- easyjson.Unmarshal(msg.Result, res)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
2017-02-14 08:41:23 +00:00
|
|
|
errch <- ctxt.Err()
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
2017-02-14 08:41:23 +00:00
|
|
|
|
|
|
|
h.resrw.Lock()
|
|
|
|
defer h.resrw.Unlock()
|
|
|
|
|
|
|
|
delete(h.res, id)
|
2017-01-24 15:09:23 +00:00
|
|
|
}()
|
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
return <-errch
|
|
|
|
}
|
|
|
|
|
|
|
|
// next returns the next message id.
|
|
|
|
func (h *TargetHandler) next() int64 {
|
|
|
|
h.lastm.Lock()
|
|
|
|
defer h.lastm.Unlock()
|
|
|
|
h.last++
|
|
|
|
return h.last
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoot returns the current top level frame's root document node.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) GetRoot(ctxt context.Context) (*cdp.Node, error) {
|
|
|
|
var root *cdp.Node
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
for {
|
2017-01-26 07:28:34 +00:00
|
|
|
var cur *cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
h.RLock()
|
|
|
|
cur = h.cur
|
|
|
|
if cur != nil {
|
|
|
|
cur.RLock()
|
|
|
|
root = cur.Root
|
|
|
|
cur.RUnlock()
|
|
|
|
}
|
|
|
|
h.RUnlock()
|
|
|
|
|
|
|
|
if cur != nil && root != nil {
|
2017-11-25 20:15:28 +00:00
|
|
|
return root, nil
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(DefaultCheckDuration)
|
|
|
|
|
|
|
|
case <-ctxt.Done():
|
|
|
|
return nil, ctxt.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetActive sets the currently active frame after a successful navigation.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) SetActive(ctxt context.Context, id cdp.FrameID) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// get frame
|
|
|
|
f, err := h.WaitFrame(ctxt, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
h.cur = f
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitFrame waits for a frame to be loaded using the provided context.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) WaitFrame(ctxt context.Context, id cdp.FrameID) (*cdp.Frame, error) {
|
2017-01-24 15:09:23 +00:00
|
|
|
// TODO: fix this
|
|
|
|
timeout := time.After(10 * time.Second)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
2017-01-26 07:28:34 +00:00
|
|
|
var f *cdp.Frame
|
2017-01-24 15:09:23 +00:00
|
|
|
var ok bool
|
|
|
|
|
|
|
|
h.RLock()
|
2017-02-18 08:36:24 +00:00
|
|
|
if id == cdp.EmptyFrameID {
|
2017-01-24 15:09:23 +00:00
|
|
|
f, ok = h.cur, h.cur != nil
|
|
|
|
} else {
|
|
|
|
f, ok = h.frames[id]
|
|
|
|
}
|
|
|
|
h.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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, fmt.Errorf("timeout waiting for frame `%s`", id)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitNode waits for a node to be loaded using the provided context.
|
2017-01-26 07:28:34 +00:00
|
|
|
func (h *TargetHandler) WaitNode(ctxt context.Context, f *cdp.Frame, id cdp.NodeID) (*cdp.Node, error) {
|
2017-01-24 15:09:23 +00:00
|
|
|
// TODO: fix this
|
|
|
|
timeout := time.After(10 * time.Second)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
2017-01-26 07:28:34 +00:00
|
|
|
var n *cdp.Node
|
2017-01-24 15:09:23 +00:00
|
|
|
var ok bool
|
|
|
|
|
|
|
|
f.RLock()
|
|
|
|
n, ok = f.Nodes[id]
|
|
|
|
f.RUnlock()
|
|
|
|
|
|
|
|
if n != nil && ok {
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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, fmt.Errorf("timeout waiting for node `%d`", id)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pageEvent handles incoming page events.
|
|
|
|
func (h *TargetHandler) pageEvent(ctxt context.Context, ev interface{}) {
|
|
|
|
defer h.pageWaitGroup.Done()
|
|
|
|
|
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:
|
|
|
|
h.Lock()
|
|
|
|
h.frames[e.Frame.ID] = e.Frame
|
2017-05-08 12:36:03 +00:00
|
|
|
if h.cur != nil && h.cur.ID == e.Frame.ID {
|
|
|
|
h.cur = e.Frame
|
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
h.Unlock()
|
|
|
|
return
|
|
|
|
|
|
|
|
case *page.EventFrameAttached:
|
|
|
|
id, op = e.FrameID, frameAttached(e.ParentFrameID)
|
|
|
|
|
|
|
|
case *page.EventFrameDetached:
|
|
|
|
id, op = e.FrameID, frameDetached
|
|
|
|
|
|
|
|
case *page.EventFrameStartedLoading:
|
|
|
|
id, op = e.FrameID, frameStartedLoading
|
|
|
|
|
|
|
|
case *page.EventFrameStoppedLoading:
|
|
|
|
id, op = e.FrameID, frameStoppedLoading
|
|
|
|
|
|
|
|
case *page.EventFrameScheduledNavigation:
|
|
|
|
id, op = e.FrameID, frameScheduledNavigation
|
|
|
|
|
|
|
|
case *page.EventFrameClearedScheduledNavigation:
|
|
|
|
id, op = e.FrameID, frameClearedScheduledNavigation
|
|
|
|
|
2017-12-18 04:03:22 +00:00
|
|
|
// ignored events
|
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:
|
2017-02-13 09:00:25 +00:00
|
|
|
h.errorf("unhandled page event %s", reflect.TypeOf(ev))
|
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := h.WaitFrame(ctxt, id)
|
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not get frame %s: %v", id, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
op(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// domEvent handles incoming DOM events.
|
|
|
|
func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
|
|
|
|
defer h.domWaitGroup.Done()
|
|
|
|
|
|
|
|
// wait current frame
|
2017-02-18 08:36:24 +00:00
|
|
|
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
h.errorf("could not process DOM event %s: %v", reflect.TypeOf(ev), err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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:
|
2017-02-18 08:36:24 +00:00
|
|
|
if e.PreviousNodeID != cdp.EmptyNodeID {
|
2017-01-24 15:09:23 +00:00
|
|
|
_, err = h.WaitNode(ctxt, f, e.PreviousNodeID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
2017-02-13 09:00:25 +00:00
|
|
|
h.errorf("unhandled node event %s", reflect.TypeOf(ev))
|
|
|
|
return
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve node
|
|
|
|
n, err := h.WaitNode(ctxt, f, id)
|
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
s := strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(op).Pointer()).Name(), ".func1")
|
|
|
|
i := strings.LastIndex(s, ".")
|
|
|
|
if i != -1 {
|
|
|
|
s = s[i+1:]
|
|
|
|
}
|
|
|
|
h.errorf("could not perform (%s) operation on node %d (wait node): %v", s, id, err)
|
2017-01-24 15:09:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
op(n)
|
|
|
|
}
|
2017-02-12 04:59:33 +00:00
|
|
|
|
|
|
|
// Listen creates a listener for the specified event types.
|
|
|
|
func (h *TargetHandler) Listen(eventTypes ...cdp.MethodType) <-chan interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases a channel returned from Listen.
|
|
|
|
func (h *TargetHandler) Release(ch <-chan interface{}) {
|
|
|
|
|
|
|
|
}
|