[ANN] cdp 0.1.0 - typed Chrome DevTools Protocol libraries for OCaml

Hi everyone,

I’m happy to announce the first release of ocaml-cdp - typed OCaml libraries for the Chrome DevTools Protocol.

Why

CDP lets a program control a real Chrome: open pages, run JavaScript, take screenshots, render PDFs, watch network traffic.
OCaml had no CDP library. ocaml-cdp fills that gap: it turns the official protocol definitions into typed OCaml modules, so the compiler checks every command, event, and field.

What you get

Three packages:

Package Where What it is
cdp opam Typed protocol domains: records, enums, commands, events, with JSON codecs. Transport-agnostic.
cdp-gen opam The generator CLI: protocol JSON in, OCaml out. Any revision, your own domain selection.
cdp-lwt GitHub only for now (see below) The client: launches or attaches to a Chrome and drives it over libcurl WebSockets.

cdp ships 10 domains (Browser, DOM, Debugger, Emulation, IO, Network, Page, Runtime, Security, Target); the generator covers all 58.

Here is the full round trip: launch a headless Chrome, open a page, read its title back:

let%lwt chrome = Cdp_lwt.Chrome.launch () in
let%lwt transport = Cdp_lwt.Curl_transport.connect ~url:chrome.ws_url () in
let connection = Cdp_lwt.Connection.create transport in
let call ?session command = Cdp_lwt.Connection.call connection ?session command in
let%lwt created =
  call (Cdp.Target.Create_target.command (Cdp.Target.Create_target.make_params ~url:"about:blank" ()))
in
let%lwt attached =
  call (Cdp.Target.Attach_to_target.command
          (Cdp.Target.Attach_to_target.make_params ~target_id:created.target_id ~flatten:true ()))
in
let session = attached.session_id in
let%lwt () = call ~session (Cdp.Page.Enable.command (Cdp.Page.Enable.make_params ())) in
let loaded = Cdp_lwt.Connection.next_event connection ~session Cdp.Page.Load_event_fired.event in
let%lwt _nav =
  call ~session (Cdp.Page.Navigate.command
                   (Cdp.Page.Navigate.make_params ~url:"https://ocaml.org" ()))
in
let%lwt _fired = loaded in
let%lwt evaluated =
  call ~session (Cdp.Runtime.Evaluate.command (Cdp.Runtime.Evaluate.make_params ~expression:"document.title" ()))
in

Why cdp-lwt is not on opam yet

cdp-lwt uses libcurl’s WebSocket API through the curl bindings. That API is on ocurl’s master branch, but not in any ocurl release yet. cdp-lwt builds fine from a clone (its opam file pins ocurl master). As soon as ocurl makes a release, cdp-lwt follows onto opam.

6 Likes

I am maintaining something similar for a few years already. Though its called reason-cdp, as it was written in reasonml first.

Its also not on opam, because the versions follow the release revisions of the cdp protocol, which sometimes change daily.

Its great to see something on opam now :slightly_smiling_face:

2 Likes