Documentation ¶
Overview ¶
Package chrome implements the Chrome DevTools protocol. It can be used to control a headless Chrome instance.
For more on this protocol, see: https://chromedevtools.github.io/devtools-protocol/
Index ¶
- Constants
- Variables
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) ConsoleLog() []string
- func (c *Conn) DispatchKeyEvent(ctx context.Context, k *KeyEvent) (err error)
- func (c *Conn) DispatchMouseEvent(ctx context.Context, m *MouseEvent) (err error)
- func (c *Conn) Error() error
- func (c *Conn) EvalPromise(ctx context.Context, code string, retObj interface{}) (err error)
- func (c *Conn) NavigateSafe(ctx context.Context, urlStr string) (err error)
- func (c *Conn) NavigateSync(ctx context.Context, urlStr string) (err error)
- func (c *Conn) ScreenshotJPEG(ctx context.Context, quality int) (imageData []byte, err error)
- func (c *Conn) ScreenshotPNG(ctx context.Context) (imageData []byte, err error)
- type Endpoint
- type KeyEvent
- type KeyEventType
- type KeyModifier
- type MouseButton
- type MouseEvent
- type MouseEventType
Constants ¶
const ( KeyDown KeyEventType = "keyDown" KeyUp = "keyUp" RawKeyDown = "rawKeyDown" )
const ( ModifierAlt = 1 ModifierCtrl = 2 ModifierMeta = 4 ModifierShift = 8 )
const ( MousePressed MouseEventType = "mousePressed" MouseReleased = "mouseReleased" MouseMoved = "mouseMoved" )
Variables ¶
var KeyEvents = map[string]KeyEvent{
"Backquote": {Code: "Backquote", Text: "`", KeyIdentifier: "U+0060", Key: "`", WindowsVirtualKeyCode: 192, NativeVirtualKeyCode: 192},
"Digit1": {Code: "Digit1", Text: "1", KeyIdentifier: "U+0031", Key: "1", WindowsVirtualKeyCode: 49, NativeVirtualKeyCode: 49},
"Digit2": {Code: "Digit2", Text: "2", KeyIdentifier: "U+0032", Key: "2", WindowsVirtualKeyCode: 50, NativeVirtualKeyCode: 50},
"Digit3": {Code: "Digit3", Text: "3", KeyIdentifier: "U+0033", Key: "3", WindowsVirtualKeyCode: 51, NativeVirtualKeyCode: 51},
"Digit4": {Code: "Digit4", Text: "4", KeyIdentifier: "U+0034", Key: "4", WindowsVirtualKeyCode: 52, NativeVirtualKeyCode: 52},
"Digit5": {Code: "Digit5", Text: "5", KeyIdentifier: "U+0035", Key: "5", WindowsVirtualKeyCode: 53, NativeVirtualKeyCode: 53},
"Digit6": {Code: "Digit6", Text: "6", KeyIdentifier: "U+0036", Key: "6", WindowsVirtualKeyCode: 54, NativeVirtualKeyCode: 54},
"Digit7": {Code: "Digit7", Text: "7", KeyIdentifier: "U+0037", Key: "7", WindowsVirtualKeyCode: 55, NativeVirtualKeyCode: 55},
"Digit8": {Code: "Digit8", Text: "8", KeyIdentifier: "U+0038", Key: "8", WindowsVirtualKeyCode: 56, NativeVirtualKeyCode: 56},
"Digit9": {Code: "Digit9", Text: "9", KeyIdentifier: "U+0039", Key: "9", WindowsVirtualKeyCode: 57, NativeVirtualKeyCode: 57},
"Digit0": {Code: "Digit0", Text: "0", KeyIdentifier: "U+0030", Key: "0", WindowsVirtualKeyCode: 48, NativeVirtualKeyCode: 48},
"Minus": {Code: "Minus", Text: "-", KeyIdentifier: "U+002D", Key: "-", WindowsVirtualKeyCode: 189, NativeVirtualKeyCode: 189},
"Equal": {Code: "Equal", Text: "=", KeyIdentifier: "U+003D", Key: "=", WindowsVirtualKeyCode: 187, NativeVirtualKeyCode: 187},
"Backspace": {Code: "Backspace", Text: "", KeyIdentifier: "U+0008", Key: "Backspace", WindowsVirtualKeyCode: 8, NativeVirtualKeyCode: 8},
"Tab": {Code: "Tab", Text: "", KeyIdentifier: "U+0009", Key: "Tab", WindowsVirtualKeyCode: 9, NativeVirtualKeyCode: 9},
"KeyQ": {Code: "KeyQ", Text: "q", KeyIdentifier: "U+0051", Key: "q", WindowsVirtualKeyCode: 81, NativeVirtualKeyCode: 81},
"KeyW": {Code: "KeyW", Text: "w", KeyIdentifier: "U+0057", Key: "w", WindowsVirtualKeyCode: 87, NativeVirtualKeyCode: 87},
"KeyE": {Code: "KeyE", Text: "e", KeyIdentifier: "U+0045", Key: "e", WindowsVirtualKeyCode: 69, NativeVirtualKeyCode: 69},
"KeyR": {Code: "KeyR", Text: "r", KeyIdentifier: "U+0052", Key: "r", WindowsVirtualKeyCode: 82, NativeVirtualKeyCode: 82},
"KeyT": {Code: "KeyT", Text: "t", KeyIdentifier: "U+0054", Key: "t", WindowsVirtualKeyCode: 84, NativeVirtualKeyCode: 84},
"KeyY": {Code: "KeyY", Text: "y", KeyIdentifier: "U+0059", Key: "y", WindowsVirtualKeyCode: 89, NativeVirtualKeyCode: 89},
"KeyU": {Code: "KeyU", Text: "u", KeyIdentifier: "U+0055", Key: "u", WindowsVirtualKeyCode: 85, NativeVirtualKeyCode: 85},
"KeyI": {Code: "KeyI", Text: "i", KeyIdentifier: "U+0049", Key: "i", WindowsVirtualKeyCode: 73, NativeVirtualKeyCode: 73},
"KeyO": {Code: "KeyO", Text: "o", KeyIdentifier: "U+004F", Key: "o", WindowsVirtualKeyCode: 79, NativeVirtualKeyCode: 79},
"KeyP": {Code: "KeyP", Text: "p", KeyIdentifier: "U+0050", Key: "p", WindowsVirtualKeyCode: 80, NativeVirtualKeyCode: 80},
"BracketLeft": {Code: "BracketLeft", Text: "[", KeyIdentifier: "U+005B", Key: "[", WindowsVirtualKeyCode: 219, NativeVirtualKeyCode: 219},
"BracketRight": {Code: "BracketRight", Text: "]", KeyIdentifier: "U+005D", Key: "]", WindowsVirtualKeyCode: 221, NativeVirtualKeyCode: 221},
"Backslash": {Code: "Backslash", Text: "\\", KeyIdentifier: "U+005C", Key: "\\", WindowsVirtualKeyCode: 220, NativeVirtualKeyCode: 220},
"KeyA": {Code: "KeyA", Text: "a", KeyIdentifier: "U+0041", Key: "a", WindowsVirtualKeyCode: 65, NativeVirtualKeyCode: 65},
"KeyS": {Code: "KeyS", Text: "s", KeyIdentifier: "U+0053", Key: "s", WindowsVirtualKeyCode: 83, NativeVirtualKeyCode: 83},
"KeyD": {Code: "KeyD", Text: "d", KeyIdentifier: "U+0044", Key: "d", WindowsVirtualKeyCode: 68, NativeVirtualKeyCode: 68},
"KeyF": {Code: "KeyF", Text: "f", KeyIdentifier: "U+0046", Key: "f", WindowsVirtualKeyCode: 70, NativeVirtualKeyCode: 70},
"KeyG": {Code: "KeyG", Text: "g", KeyIdentifier: "U+0047", Key: "g", WindowsVirtualKeyCode: 71, NativeVirtualKeyCode: 71},
"KeyH": {Code: "KeyH", Text: "h", KeyIdentifier: "U+0048", Key: "h", WindowsVirtualKeyCode: 72, NativeVirtualKeyCode: 72},
"KeyJ": {Code: "KeyJ", Text: "j", KeyIdentifier: "U+004A", Key: "j", WindowsVirtualKeyCode: 74, NativeVirtualKeyCode: 74},
"KeyK": {Code: "KeyK", Text: "k", KeyIdentifier: "U+004B", Key: "k", WindowsVirtualKeyCode: 75, NativeVirtualKeyCode: 75},
"KeyL": {Code: "KeyL", Text: "l", KeyIdentifier: "U+004C", Key: "l", WindowsVirtualKeyCode: 76, NativeVirtualKeyCode: 76},
"Semicolon": {Code: "Semicolon", Text: ";", KeyIdentifier: "U+003B", Key: ";", WindowsVirtualKeyCode: 186, NativeVirtualKeyCode: 186},
"Quote": {Code: "Quote", Text: "'", KeyIdentifier: "U+0027", Key: "'", WindowsVirtualKeyCode: 222, NativeVirtualKeyCode: 222},
"Enter": {Code: "Enter", Text: "", KeyIdentifier: "Enter", Key: "Enter", WindowsVirtualKeyCode: 13, NativeVirtualKeyCode: 13},
"KeyZ": {Code: "KeyZ", Text: "z", KeyIdentifier: "U+005A", Key: "z", WindowsVirtualKeyCode: 90, NativeVirtualKeyCode: 90},
"KeyX": {Code: "KeyX", Text: "x", KeyIdentifier: "U+0058", Key: "x", WindowsVirtualKeyCode: 88, NativeVirtualKeyCode: 88},
"KeyC": {Code: "KeyC", Text: "c", KeyIdentifier: "U+0043", Key: "c", WindowsVirtualKeyCode: 67, NativeVirtualKeyCode: 67},
"KeyV": {Code: "KeyV", Text: "v", KeyIdentifier: "U+0056", Key: "v", WindowsVirtualKeyCode: 86, NativeVirtualKeyCode: 86},
"KeyB": {Code: "KeyB", Text: "b", KeyIdentifier: "U+0042", Key: "b", WindowsVirtualKeyCode: 66, NativeVirtualKeyCode: 66},
"KeyN": {Code: "KeyN", Text: "n", KeyIdentifier: "U+004E", Key: "n", WindowsVirtualKeyCode: 78, NativeVirtualKeyCode: 78},
"KeyM": {Code: "KeyM", Text: "m", KeyIdentifier: "U+004D", Key: "m", WindowsVirtualKeyCode: 77, NativeVirtualKeyCode: 77},
"Comma": {Code: "Comma", Text: ",", KeyIdentifier: "U+002C", Key: ",", WindowsVirtualKeyCode: 188, NativeVirtualKeyCode: 188},
"Period": {Code: "Period", Text: ".", KeyIdentifier: "U+002E", Key: ".", WindowsVirtualKeyCode: 190, NativeVirtualKeyCode: 190},
"Slash": {Code: "Slash", Text: "/", KeyIdentifier: "U+002F", Key: "/", WindowsVirtualKeyCode: 191, NativeVirtualKeyCode: 191},
"ShiftRight": {Code: "ShiftRight", Text: "", KeyIdentifier: "Shift", Key: "Shift", WindowsVirtualKeyCode: 16, NativeVirtualKeyCode: 16},
"ShiftLeft": {Code: "ShiftLeft", Text: "", KeyIdentifier: "Shift", Key: "Shift", WindowsVirtualKeyCode: 16, NativeVirtualKeyCode: 16},
"ControlLeft": {Code: "ControlLeft", Text: "", KeyIdentifier: "Control", Key: "Control", WindowsVirtualKeyCode: 17, NativeVirtualKeyCode: 17},
"AltLeft": {Code: "AltLeft", Text: "", KeyIdentifier: "Alt", Key: "Alt", WindowsVirtualKeyCode: 18, NativeVirtualKeyCode: 18},
"MetaLeft": {Code: "MetaLeft", Text: "", KeyIdentifier: "Meta", Key: "Meta", WindowsVirtualKeyCode: 91, NativeVirtualKeyCode: 91},
"Space": {Code: "Space", Text: " ", KeyIdentifier: "U+0020", Key: " ", WindowsVirtualKeyCode: 32, NativeVirtualKeyCode: 32},
"MetaRight": {Code: "MetaRight", Text: "", KeyIdentifier: "Meta", Key: "Meta", WindowsVirtualKeyCode: 93, NativeVirtualKeyCode: 93},
"AltRight": {Code: "AltRight", Text: "", KeyIdentifier: "Alt", Key: "Alt", WindowsVirtualKeyCode: 18, NativeVirtualKeyCode: 18},
"ArrowLeft": {Code: "ArrowLeft", Text: "", KeyIdentifier: "Left", Key: "ArrowLeft", WindowsVirtualKeyCode: 37, NativeVirtualKeyCode: 37},
"ArrowDown": {Code: "ArrowDown", Text: "", KeyIdentifier: "Down", Key: "ArrowDown", WindowsVirtualKeyCode: 40, NativeVirtualKeyCode: 40},
"ArrowUp": {Code: "ArrowUp", Text: "", KeyIdentifier: "Up", Key: "ArrowUp", WindowsVirtualKeyCode: 38, NativeVirtualKeyCode: 38},
"ArrowRight": {Code: "ArrowRight", Text: "", KeyIdentifier: "Right", Key: "ArrowRight", WindowsVirtualKeyCode: 39, NativeVirtualKeyCode: 39},
}
KeyEvents maps key codes to KeyEvent objects.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a connection to a protocol endpoint.
func (*Conn) ConsoleLog ¶
ConsoleLog returns a copy of the console log messages. This may be cleared on page refresh.
func (*Conn) DispatchKeyEvent ¶
DispatchKeyEvent triggers a keyboard event.
func (*Conn) DispatchMouseEvent ¶
func (c *Conn) DispatchMouseEvent(ctx context.Context, m *MouseEvent) (err error)
DispatchMouseEvent triggers a mouse event.
func (*Conn) Error ¶
Error returns the first connection error encountered. If no errors have been encountered, nil is returned.
func (*Conn) EvalPromise ¶
EvalPromise evaluates a piece of JavaScript code which returns a Promise.
If retObj is non-nil, it is overwritten with the unmarshalled result of the Promise. Unmarshalling works like it does in encoding/json.
func (*Conn) NavigateSafe ¶
NavigateSafe is like NavigateSync, but it checks for network related errors in the console and retries if such errors are encountered.
This is meant to deal with a bug in Chromium when running under Docker where a net::ERR_NETWORK_CHANGED error is triggered. It is yet to be clear whether the error originates due to Docker networking or due to Chromium.
func (*Conn) NavigateSync ¶
NavigateSync navigates to the given page and waits for it to finish loading.
func (*Conn) ScreenshotJPEG ¶
ScreenshotJPEG captures a compressed screenshot of the page and returns the raw JPEG image data.
The quality is an integer in the range [0, 100].
type Endpoint ¶
type Endpoint struct { Description string `json:"description"` FrontendURL string `json:"devtoolsFrontendUrl"` FaviconURL string `json:"faviconUrl"` ID string `json:"id"` Title string `json:"title"` // Options: page, service_worker, background_page, // iframe, and possibly others. Type string `json:"type"` URL string `json:"url"` WebSocketURL string `json:"webSocketDebuggerUrl"` }
Endpoint stores information about an active page, extension, iframe, or any other API endpoint.
type KeyEvent ¶
type KeyEvent struct { Type KeyEventType `json:"type" bson:"Type"` // Bitmask of key modifiers. Modifiers KeyModifier `json:"modifiers" bson:"Modifiers"` // Timestamp of event (defaults to current time). Timestamp int `json:"timestamp,omitempty" bson:"Timestamp"` // Text generated by the event. // Not needed for KeyUp and RawKeyDown. Text string `json:"text" bson:"Text"` // Same as Text but without modifier keys. UnmodifiedText string `json:"unmodifiedText" bson:"UnmodifiedText"` // Unique key identifier. KeyIdentifier string `json:"keyIdentifier" bson:"KeyIdentifier"` // DOM key code. Code string `json:"code" bson:"Code"` // DOM code for key & modifiers. Key string `json:"key" bson:"Key"` WindowsVirtualKeyCode int `json:"windowsVirtualKeyCode" bson:"WindowsVirtualKeyCode"` NativeVirtualKeyCode int `json:"nativeVirtualKeyCode" bson:"NativeVirtualKeyCode"` // Information about source of key event. AutoRepeat bool `json:"autoRepeat" bson:"AutoRepeat"` IsKeypad bool `json:"isKeypad" bson:"IsKeypad"` IsSystemKey bool `json:"isSystemKey" bson:"IsSystemKey"` }
KeyEvent is a JavaScript keyboard event.
type MouseButton ¶
type MouseButton string
MouseButton is a button identifier for a mouse event.
const ( LeftButton MouseButton = "left" MiddleButton MouseButton = "middle" RightButton MouseButton = "right" )
type MouseEvent ¶
type MouseEvent struct { Type MouseEventType `json:"type" bson:"Type"` X int `json:"x" bson:"X"` Y int `json:"y" bson:"Y"` // May be "" for movement events. Button MouseButton `json:"button,omitempty" bson:"Button"` ClickCount int `json:"clickCount" bson:"ClickCount"` }
MouseEvent stores information about a JavaScript mouse event.