Skip to content

chrome.socket.AddEventHandler

Michael Kenney edited this page Sep 29, 2018 · 1 revision

Event handlers are code that executes when a particular event is returned by the websocket listening to a Page (Tab). Event handlers are executed as goroutines and do not return data. To capture data from a handler the best strategy is to create a channel and use that to capture any output.

package main

import (
	chrome "github.com/mkenney/go-chrome/tot"
	"github.com/mkenney/go-chrome/tot/socket"
)

func main() {
	var err error

	// ... snip ... //

	// Create an event handler for the Page.loadEventFired event
	loadEventHandler := socket.NewEventHandler(
		"Page.loadEventFired",
		func(response *socket.Response) {
			// ... Code here will execute when the event fires ... //
		},
	)

	// Add the handler to the stack for this tab. Multiple handlers can
	// listen to the same event.
	tab.AddEventHandler(loadEventHandler)
}