cdp:   Chrome Dev  Tools Protocol
1 Quick start
2 Client
cdp
cdp-launch
cdp-close
with-cdp
cdp-call
cdp-evaluate
cdp-wait
cdp-navigate
cdp-screenshot
a4-paper
cdp-print-to-pdf
3 Browser discovery and command-line helpers
find-browser
browser-error
path->file-url
with-temp-directory
run-process
headless-screenshot
headless-print-pdf
4 Web  Socket
websocket
ws-connect
ws-connect-url
ws-send!
ws-recv!
ws-send-raw!
ws-close!
ws-accept-key
5 License
9.3

cdp: Chrome DevTools Protocol🔗ℹ

turinglambdaai

 (require cdp) package: cdp

The "cdp" package drives the Chrome DevTools Protocol of the Chromium-based browser already on the machine (Chrome, Edge, Chromium) from Racket: launch it headless with a throw-away profile, attach to a page, evaluate JavaScript, navigate, wait, take screenshots and print to PDF with a custom header or footer — something the plain –print-to-pdf switch cannot do.

Everything is implemented in pure Racket: no FFI, no third-party package, no bundled browser. The WebSocket transport (RFC 6455) is written by hand in cdp/websocket.

1 Quick start🔗ℹ

Example:
> (hash-ref a4-paper 'paperWidth)

8.27

#lang racket/base
(require cdp)
 
(with-cdp
 (lambda (c)
   (cdp-navigate c "https://racket-lang.org/")
   (cdp-wait c "document.readyState === 'complete'")
   (displayln (cdp-evaluate c "document.title"))
   (with-output-to-file "racket.pdf"
     (lambda () (write-bytes (cdp-screenshot c))))))

2 Client🔗ℹ

struct

(struct cdp (ws proc profile session next-id))

  ws : websocket?
  proc : subprocess?
  profile : path-string?
  session : (or/c string? #f)
  next-id : exact-nonnegative-integer?
One attached page. session is the flattened session id of the page target; next-id is the id of the next message, so two clients never share a counter.

procedure

(cdp-launch [#:browser browser    
  #:args args    
  #:headless? headless?    
  #:timeout-ms timeout-ms]) → cdp?
  browser : (or/c path-string? #f) = #f
  args : (listof string?) = '()
  headless? : boolean? = #t
  timeout-ms : exact-nonnegative-integer? = 30000
Launches the browser with a scratch profile and a DevTools port, waits for DevToolsActivePort, connects, creates a page target, attaches to it with a flattened session and enables Page and Runtime. browser defaults to find-browser; args adds command-line switches; headless? #f opens a visible window.

procedure

(cdp-close c) → void?

  c : cdp?
Closes the browser, the socket and removes the profile.

procedure

(with-cdp body    
  [#:browser browser    
  #:args args]) → any
  body : (-> cdp? any)
  browser : (or/c path-string? #f) = #f
  args : (listof string?) = '()
Evaluates (body c) with a fresh client; the browser is closed afterwards, even when body raises.

procedure

(cdp-call c method [params session]) → jsexpr?

  c : cdp?
  method : string?
  params : jsexpr? = (hasheq)
  session : (or/c string? #f) = (cdp-session c)
Sends one command and waits for the reply with the same id; events that arrive meanwhile are dropped. Protocol errors and page script errors raise exn:fail. Set the CDP_DEBUG environment variable to trace every message on stderr.

procedure

(cdp-evaluate c expr) → jsexpr?

  c : cdp?
  expr : string?
Evaluates expr in the page with returnByValue and awaitPromise; returns the JSON value.

procedure

(cdp-wait c expr [timeout-ms]) → boolean?

  c : cdp?
  expr : string?
  timeout-ms : exact-nonnegative-integer? = 30000
Polls expr every 250 ms until it evaluates truthy; #f when the timeout passes first. Errors while polling count as “not yet”.

procedure

(cdp-navigate c url) → (or/c string? #f)

  c : cdp?
  url : string?
Loads url in the page; returns the frame id.

procedure

(cdp-screenshot c [#:full-page? full-page?]) → bytes?

  c : cdp?
  full-page? : boolean? = #f
PNG bytes of the viewport (or of the whole page).

value

a4-paper : jsexpr?

A4 portrait in inches, for cdp-print-to-pdf.

procedure

(cdp-print-to-pdf c    
  out-path    
  [#:paper paper    
  #:landscape? landscape?    
  #:margins margins    
  #:header-footer? header-footer?    
  #:header-template header-template    
  #:footer-template footer-template]) → void?
  c : cdp?
  out-path : path-string?
  paper : jsexpr? = a4-paper
  landscape? : boolean? = #f
  margins : jsexpr?
   = (hasheq 'marginTop 1 'marginBottom 1 'marginLeft 1 'marginRight 1)
  header-footer? : boolean? = #f
  header-template : string? = "<div></div>"
  footer-template : string? = "<div></div>"
Prints the page with backgrounds on. paper and margins are hashes of the Page.printToPDF keys of the same names; the templates are HTML with the DevTools placeholder classes (pageNumber, title, date, ...) and are only drawn when header-footer? is true.

3 Browser discovery and command-line helpers🔗ℹ

procedure

(find-browser) → (or/c path-string? #f)

Path of a usable browser, or #f. The CDP_BROWSER environment variable (path to an executable) overrides discovery; otherwise known install locations are tried, then the PATH.

procedure

(browser-error) → any

Raises exn:fail explaining that no browser was found.

procedure

(path->file-url p) → string?

  p : path-string?
The file:// URL of p, for navigating to local files.

procedure

(with-temp-directory body) → any

  body : (-> path? any)
Evaluates (body dir) with a fresh temporary directory that is removed afterwards.

procedure

(run-process exe args [#:timeout timeout])

 → 
(or/c exact-integer? #f) string?
  exe : path-string?
  args : (listof string?)
  timeout : real? = 30
Runs exe; returns the exit code (#f when killed after timeout seconds) and stderr. All three streams go to temp files so the browser inherits no console handles.

procedure

(headless-screenshot url    
  out-png    
  width    
  height    
  [#:background background    
  #:timeout timeout]) → void?
  url : string?
  out-png : path-string?
  width : exact-positive-integer?
  height : exact-positive-integer?
  background : (or/c string? #f) = #f
  timeout : real? = 20
Screenshot through the plain –screenshot switch — no protocol needed. The viewport is exactly width by height CSS px at device scale 1; background #f keeps transparency, otherwise a #RRGGBB string.

procedure

(headless-print-pdf url    
  out-pdf    
  [#:timeout timeout]) → void?
  url : string?
  out-pdf : path-string?
  timeout : real? = 60
Prints through the plain –print-to-pdf switch; page size and margins come from the page’s @page CSS.

4 WebSocket🔗ℹ

struct

(struct websocket (in out))

  in : input-port?
  out : output-port?
One open connection.

procedure

(ws-connect host port ws-path) → websocket?

  host : string?
  port : exact-nonnegative-integer?
  ws-path : string?
Opens a TCP connection and performs the opening handshake.

procedure

(ws-connect-url host port url) → websocket?

  host : string?
  port : exact-nonnegative-integer?
  url : string?
Like ws-connect with the path taken from a URL.

procedure

(ws-send! ws payload) → void?

  ws : websocket?
  payload : bytes?
Sends one masked text frame.

procedure

(ws-recv! ws) → bytes?

  ws : websocket?
Reads one message, reassembling fragments and answering pings; a close frame raises exn:fail:websocket.

procedure

(ws-send-raw! ws op payload) → void?

  ws : websocket?
  op : exact-nonnegative-integer?
  payload : bytes?
Sends one masked frame of any opcode (1 text, 2 binary, 8 close, 9 ping, 10 pong).

procedure

(ws-close! ws) → void?

  ws : websocket?
Sends a close frame and closes the ports.

procedure

(ws-accept-key key) → bytes?

  key : bytes?
The Sec-WebSocket-Accept value for a Sec-WebSocket-Key, for tests that act as the server.

5 License🔗ℹ

MIT.