gochrome

package module
v1.0.0-rc9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2022 License: BSD-2-Clause Imports: 0 Imported by: 0

README

go-chrome

BSD-2-Clause Release Candidate Build status Coverage status Github issues Github pull requests GoDoc

This package aims to be a complete Chrome DevTools Protocol implementation. The primary use-case behind this project is interacting with headless Google Chrome in a container environment, but it should be appropriate for developing server side and desktop applications for any browser that supports the devtools protocol.

The API is fairly settled and basic code-coverage tests have been implemented but real-world testing is needed. Page.captureScreenshot and related calls are working well and are regularly used for validating the viability of code changes.

This implementation is based on the Tip-of-Tree documentation and may be prone to change. At some point stable versions will be implemented as well, hopefully beginning with v1.3.

Documentation and Examples

There are a few small examples of how to use the framework API on the wiki and in the /_examples directory. Additional documentation is available on the wiki as well.

TODO

Contributions of any kind are very welcome!

  • Resolve race condition issues. Any assistance is appreciated!

  • Add framework API examples to the /_examples directory and wiki to showcase various ways people are using the package.

    Any example scripts showing various ways people are using the framework would be outstanding! The screenshot script and several others are available there.

  • Refactoring to implement standard interfaces where applicable and review current use of interfaces in the API. Some aren't needed at all and others are used to support test mocks.

  • Add more tests, particularly for error cases.

  • Add integrated tests to stablize package interactions raised in various issues.

If you would like to contribute but aren't sure how, take a look at the issue tracker. Issues are labeled as bug reports, feature requests, feedback requests, help wanted, etc.

There are also always tests that could be written. There are many examples of tests in the package.

CHANGELOG

All notable changes to this project are documented in the CHANGELOG. The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Documentation

Overview

Package gochrome aims to be a complete Chrome DevTools Protocol Viewer implementation.

Versions

Versioned packages are available. Curently the only version is `tot` or Tip-of-Tree. Stable versions will be made available in the future.

import "github.com/mkenney/go-chrome/tot"

Work in progress

This is beta software and hasn't been well exercised in real-world applications.

Official documentation

See https://chromedevtools.github.io/devtools-protocol/

The Chrome DevTools Protocol allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers. Many existing projects currently use the protocol. The Chrome DevTools uses this protocol and the team maintains its API.

Instrumentation is divided into a number of domains (DOM, Debugger, Network etc.). Each domain defines a number of commands it supports and events it generates. Both commands and events are serialized JSON objects of a fixed structure. You can either debug over the wire using the raw messages as they are described in the corresponding domain documentation, or use extension JavaScript API.

Protocol API Docs

The latest (tip-of-tree) protocol (tot)

It changes frequently and can break at any time. However it captures the full capabilities of the Protocol, whereas the stable release is a subset. There is no backwards compatibility support guaranteed for the capabilities it introduces.

Resources

Basics: Using DevTools as protocol client

The Developer Tools front-end can attach to a remotely running Chrome instance for debugging. For this scenario to work, you should start your host Chrome instance with the remote-debugging-port command line switch:

chrome.exe --remote-debugging-port=9222

Then you can start a separate client Chrome instance, using a distinct user profile:

chrome.exe --user-data-dir=<some directory>

Now you can navigate to the given port from your client and attach to any of the discovered tabs for debugging: http://localhost:9222

You will find the Developer Tools interface identical to the embedded one and here is why:

  • When you navigate your client browser to the remote's Chrome port, Developer Tools front-end is being served from the host Chrome as a Web Application from the Web Server.
  • It fetches HTML, JavaScript and CSS files over HTTP
  • Once loaded, Developer Tools establishes a Web Socket connection to its host and starts exchanging JSON messages with it.

In this scenario, you can substitute Developer Tools front-end with your own implementation. Instead of navigating to the HTML page at http://localhost:9222, your application can discover available pages by requesting: http://localhost:9222/json and getting a JSON object with information about inspectable pages along with the WebSocket addresses that you could use in order to start instrumenting them.

Remote debugging is especially useful when debugging remote instances of the browser or attaching to the embedded devices. Blink port owners are responsible for exposing debugging connections to the external users.

Sniffing the protocol

This is especially handy to understand how the DevTools frontend makes use of the protocol. First, run Chrome with the debugging port open:

alias canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary" canary --remote-debugging-port=9222 http://localhost:9222 http://chromium.org

Then, select the Chromium Projects item in the Inspectable Pages list. Now that DevTools is up and fullscreen, open DevTools to inspect it. Cmd-R in the new inspector to make the first restart. Now head to Network Panel, filter by Websocket, select the connection and click the Frames tab. Now you can easily see the frames of WebSocket activity as you use the first instance of the DevTools.

DevTools protocol via Chrome extension

To allow chrome extensions to interact with the protocol, we introduced chrome.debugger extension API that exposes this JSON message transport interface. As a result, you can not only attach to the remotely running Chrome instance, but also instrument it from its own extension.

Chrome Debugger Extension API provides a higher level API where command domain, name and body are provided explicitly in the `sendCommand` call. This API hides request ids and handles binding of the request with its response, hence allowing `sendCommand` to report result in the callback function call. One can also use this API in combination with the other Extension APIs.

If you are developing a Web-based IDE, you should implement an extension that exposes debugging capabilities to your page and your IDE will be able to open pages with the target application, set breakpoints there, evaluate expressions in console, live edit JavaScript and CSS, display live DOM, network interaction and any other aspect that Developer Tools is instrumenting today.

Opening embedded Developer Tools will terminate the remote connection and thus detach the extension. https://chromedevtools.github.io/devtools-protocol/#simultaneous

Frequently Asked Questions

How is the protocol defined

The canonical protocol definitions live in the Chromium source tree: (browser_protocol.json and js_protocol.json). They are maintained manually by the DevTools engineering team. These files are mirrored (hourly) on GitHub in the devtools-protocol repo.

The declarative protocol definitions are used across tools. Within Chromium, a binding layer is created for the Chrome DevTools to interact with, and separately the protocol is used for Chrome Headless’s C++ interface.

What’s the protocol_externs file

It’s created via generate_protocol_externs.py and useful for tools using closure compiler. The TypeScript story is here.

Are the HTTP endpoints documented

Not yet. See bugger-daemon’s third-party docs. See also the endpoints implementation in Chromium. /json/protocol was added in Chrome 60.

https://github.com/buggerjs/bugger-daemon/blob/master/README.md#api
https://cs.chromium.org/search/?q=f:devtools_http_handler.cc+%22command+%3D%3D+%22&sq=package:chromium&type=cs

How do I access the browser target

The endpoint is exposed as webSocketDebuggerUrl in /json/version. Note the browser in the URL, rather than page. If Chrome was launched with --remote-debugging-port=0 and chose an open port, the browser endpoint is written to both stderr and the DevToolsActivePort file in browser profile folder.

Does the protocol support multiple simultaneous clients

Yes, as of Chrome 63! See Multi-client remote debugging support.

Upon disconnnection, the outgoing client will receive a detached event. For example:

{"method":"Inspector.detached","params":{"reason":"replaced_with_devtools"}}.

View the enum of possible reasons. (For reference: the original patch). After disconnection, some apps have chosen to pause their state and offer a reconnect button.

Directories

Path Synopsis
_examples
tot
Package chrome aims to be a complete Chrome DevTools Protocol Viewer implementation.
Package chrome aims to be a complete Chrome DevTools Protocol Viewer implementation.
accessibility
Package accessibility provides type definitions for use with the Chrome Accessibility protocol https://chromedevtools.github.io/devtools-protocol/tot/Accessibility/
Package accessibility provides type definitions for use with the Chrome Accessibility protocol https://chromedevtools.github.io/devtools-protocol/tot/Accessibility/
animation
Package animation provides type definitions for use with the Chrome Animation protocol https://chromedevtools.github.io/devtools-protocol/tot/Animation/
Package animation provides type definitions for use with the Chrome Animation protocol https://chromedevtools.github.io/devtools-protocol/tot/Animation/
application/cache
Package cache provides type definitions for use with the Chrome ApplicationCache protocol https://chromedevtools.github.io/devtools-protocol/tot/ApplicationCache/
Package cache provides type definitions for use with the Chrome ApplicationCache protocol https://chromedevtools.github.io/devtools-protocol/tot/ApplicationCache/
audits
Package audits provides type definitions for use with the Chrome Audits protocol https://chromedevtools.github.io/devtools-protocol/tot/Audits/
Package audits provides type definitions for use with the Chrome Audits protocol https://chromedevtools.github.io/devtools-protocol/tot/Audits/
browser
Package browser provides type definitions for use with the Chrome Browser protocol https://chromedevtools.github.io/devtools-protocol/tot/Browser/
Package browser provides type definitions for use with the Chrome Browser protocol https://chromedevtools.github.io/devtools-protocol/tot/Browser/
cache/storage
Package storage provides type definitions for use with the Chrome CacheStorage protocol https://chromedevtools.github.io/devtools-protocol/tot/CacheStorage/
Package storage provides type definitions for use with the Chrome CacheStorage protocol https://chromedevtools.github.io/devtools-protocol/tot/CacheStorage/
console
Package console provides type definitions for use with the Chrome Console protocol https://chromedevtools.github.io/devtools-protocol/tot/Console/ Package console provides type definitions for use with the Chrome Console protocol https://chromedevtools.github.io/devtools-protocol/tot/Console/
Package console provides type definitions for use with the Chrome Console protocol https://chromedevtools.github.io/devtools-protocol/tot/Console/ Package console provides type definitions for use with the Chrome Console protocol https://chromedevtools.github.io/devtools-protocol/tot/Console/
css
Package css provides type definitions for use with the Chrome CSS protocol https://chromedevtools.github.io/devtools-protocol/tot/CSS/
Package css provides type definitions for use with the Chrome CSS protocol https://chromedevtools.github.io/devtools-protocol/tot/CSS/
database
Package database provides type definitions for use with the Chrome Database protocol https://chromedevtools.github.io/devtools-protocol/tot/Database/
Package database provides type definitions for use with the Chrome Database protocol https://chromedevtools.github.io/devtools-protocol/tot/Database/
debugger
Package debugger provides type definitions for use with the Chrome Debugger protocol https://chromedevtools.github.io/devtools-protocol/tot/Debugger/
Package debugger provides type definitions for use with the Chrome Debugger protocol https://chromedevtools.github.io/devtools-protocol/tot/Debugger/
device/orientation
Package orientation provides type definitions for use with the Chrome DeviceOrientation protocol https://chromedevtools.github.io/devtools-protocol/tot/DeviceOrientation/
Package orientation provides type definitions for use with the Chrome DeviceOrientation protocol https://chromedevtools.github.io/devtools-protocol/tot/DeviceOrientation/
dom
Package dom provides type definitions for use with the Chrome DOM protocol https://chromedevtools.github.io/devtools-protocol/tot/DOM/
Package dom provides type definitions for use with the Chrome DOM protocol https://chromedevtools.github.io/devtools-protocol/tot/DOM/
dom/debugger
Package debugger provides type definitions for use with the Chrome DOMDebugger protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMDebugger/
Package debugger provides type definitions for use with the Chrome DOMDebugger protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMDebugger/
dom/snapshot
Package snapshot provides type definitions for use with the Chrome DOMSnapshot protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot/
Package snapshot provides type definitions for use with the Chrome DOMSnapshot protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot/
dom/storage
Package storage provides type definitions for use with the Chrome DOMStorage protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMStorage/
Package storage provides type definitions for use with the Chrome DOMStorage protocol https://chromedevtools.github.io/devtools-protocol/tot/DOMStorage/
emulation
Package emulation provides type definitions for use with the Chrome Emulation protocol https://chromedevtools.github.io/devtools-protocol/tot/Emulation/
Package emulation provides type definitions for use with the Chrome Emulation protocol https://chromedevtools.github.io/devtools-protocol/tot/Emulation/
headless/experimental
Package experimental provides type definitions for use with the Chrome HeadlessExperimental protocol https://chromedevtools.github.io/devtools-protocol/tot/HeadlessExperimental/
Package experimental provides type definitions for use with the Chrome HeadlessExperimental protocol https://chromedevtools.github.io/devtools-protocol/tot/HeadlessExperimental/
heap/profiler
Package profiler provides type definitions for use with the Chrome HeapProfiler protocol https://chromedevtools.github.io/devtools-protocol/tot/HeapProfiler/
Package profiler provides type definitions for use with the Chrome HeapProfiler protocol https://chromedevtools.github.io/devtools-protocol/tot/HeapProfiler/
indexed/db
Package db provides type definitions for use with the Chrome IndexedDB protocol https://chromedevtools.github.io/devtools-protocol/tot/IndexedDB/
Package db provides type definitions for use with the Chrome IndexedDB protocol https://chromedevtools.github.io/devtools-protocol/tot/IndexedDB/
input
Package input provides type definitions for use with the Chrome Input protocol https://chromedevtools.github.io/devtools-protocol/tot/Input/
Package input provides type definitions for use with the Chrome Input protocol https://chromedevtools.github.io/devtools-protocol/tot/Input/
io
Package io provides type definitions for use with the Chrome IO protocol https://chromedevtools.github.io/devtools-protocol/tot/IO/
Package io provides type definitions for use with the Chrome IO protocol https://chromedevtools.github.io/devtools-protocol/tot/IO/
layer/tree
Package tree provides type definitions for use with the Chrome LayerTree protocol https://chromedevtools.github.io/devtools-protocol/tot/LayerTree/
Package tree provides type definitions for use with the Chrome LayerTree protocol https://chromedevtools.github.io/devtools-protocol/tot/LayerTree/
log
Package log provides type definitions for use with the Chrome Log protocol https://chromedevtools.github.io/devtools-protocol/tot/Log/
Package log provides type definitions for use with the Chrome Log protocol https://chromedevtools.github.io/devtools-protocol/tot/Log/
memory
Package memory provides type definitions for use with the Chrome Memory protocol https://chromedevtools.github.io/devtools-protocol/tot/Memory/
Package memory provides type definitions for use with the Chrome Memory protocol https://chromedevtools.github.io/devtools-protocol/tot/Memory/
network
Package network provides type definitions for use with the Chrome Network protocol https://chromedevtools.github.io/devtools-protocol/tot/Network/
Package network provides type definitions for use with the Chrome Network protocol https://chromedevtools.github.io/devtools-protocol/tot/Network/
overlay
Package overlay provides type definitions for use with the Chrome Overlay protocol https://chromedevtools.github.io/devtools-protocol/tot/Overlay/
Package overlay provides type definitions for use with the Chrome Overlay protocol https://chromedevtools.github.io/devtools-protocol/tot/Overlay/
page
Package page provides type definitions for use with the Chrome Page protocol https://chromedevtools.github.io/devtools-protocol/tot/Page/
Package page provides type definitions for use with the Chrome Page protocol https://chromedevtools.github.io/devtools-protocol/tot/Page/
performance
Package performance provides type definitions for use with the Chrome Performance protocol https://chromedevtools.github.io/devtools-protocol/tot/Performance/
Package performance provides type definitions for use with the Chrome Performance protocol https://chromedevtools.github.io/devtools-protocol/tot/Performance/
profiler
Package profiler provides type definitions for use with the Chrome Profiler protocol https://chromedevtools.github.io/devtools-protocol/tot/Profiler/
Package profiler provides type definitions for use with the Chrome Profiler protocol https://chromedevtools.github.io/devtools-protocol/tot/Profiler/
runtime
Package runtime provides type definitions for use with the Chrome Runtime protocol https://chromedevtools.github.io/devtools-protocol/tot/Runtime/
Package runtime provides type definitions for use with the Chrome Runtime protocol https://chromedevtools.github.io/devtools-protocol/tot/Runtime/
schema
Package schema provides type definitions for use with the Chrome Schema protocol https://chromedevtools.github.io/devtools-protocol/tot/Schema/
Package schema provides type definitions for use with the Chrome Schema protocol https://chromedevtools.github.io/devtools-protocol/tot/Schema/
security
Package security provides type definitions for use with the Chrome Security protocol https://chromedevtools.github.io/devtools-protocol/tot/Security/
Package security provides type definitions for use with the Chrome Security protocol https://chromedevtools.github.io/devtools-protocol/tot/Security/
service/worker
Package worker provides type definitions for use with the Chrome ServiceWorker protocol https://chromedevtools.github.io/devtools-protocol/tot/ServiceWorker/
Package worker provides type definitions for use with the Chrome ServiceWorker protocol https://chromedevtools.github.io/devtools-protocol/tot/ServiceWorker/
socket
Package socket allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers.
Package socket allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers.
storage
Package storage provides type definitions for use with the Chrome Storage protocol https://chromedevtools.github.io/devtools-protocol/tot/Storage/
Package storage provides type definitions for use with the Chrome Storage protocol https://chromedevtools.github.io/devtools-protocol/tot/Storage/
system/info
Package info provides type definitions for use with the Chrome SystemInfo protocol https://chromedevtools.github.io/devtools-protocol/tot/SystemInfo/
Package info provides type definitions for use with the Chrome SystemInfo protocol https://chromedevtools.github.io/devtools-protocol/tot/SystemInfo/
target
Package target provides type definitions for use with the Chrome Target protocol https://chromedevtools.github.io/devtools-protocol/tot/Target/
Package target provides type definitions for use with the Chrome Target protocol https://chromedevtools.github.io/devtools-protocol/tot/Target/
tethering
Package tethering provides type definitions for use with the Chrome Tethering protocol https://chromedevtools.github.io/devtools-protocol/tot/Tethering/
Package tethering provides type definitions for use with the Chrome Tethering protocol https://chromedevtools.github.io/devtools-protocol/tot/Tethering/
tracing
Package tracing provides type definitions for use with the Chrome Tracing protocol https://chromedevtools.github.io/devtools-protocol/tot/Tracing/
Package tracing provides type definitions for use with the Chrome Tracing protocol https://chromedevtools.github.io/devtools-protocol/tot/Tracing/

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL