Skip to content

Commit

Permalink
Merge pull request chromedp#2 from dennwc/expose_events
Browse files Browse the repository at this point in the history
expose browser events via a hook function
  • Loading branch information
terorie committed Mar 1, 2019
2 parents ef7441d + ac5b86a commit 97f7786
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TargetHandler struct {

// qevents is the incoming event queue.
qevents chan *cdproto.Message
onEvent EventFunc

// detached is closed when the detached event is received.
detached chan *inspector.EventDetached
Expand Down Expand Up @@ -221,6 +222,14 @@ func (h *TargetHandler) read() (*cdproto.Message, error) {
return msg, nil
}

// EventFunc is a type for a function that accepts browser events.
type EventFunc func(ev interface{})

// OnEvent sets a hook for incoming browser events.
func (h *TargetHandler) OnEvent(hook EventFunc) {
h.onEvent = hook
}

// processEvent processes an incoming event.
func (h *TargetHandler) processEvent(ctxt context.Context, msg *cdproto.Message) error {
if msg == nil {
Expand All @@ -232,6 +241,9 @@ func (h *TargetHandler) processEvent(ctxt context.Context, msg *cdproto.Message)
if err != nil {
return err
}
if h.onEvent != nil {
go h.onEvent(ev)
}

switch e := ev.(type) {
case *inspector.EventDetached:
Expand Down

0 comments on commit 97f7786

Please sign in to comment.