cdp: Chrome DevTools Protocol
| (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
#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
ws : websocket? proc : subprocess? profile : path-string? session : (or/c string? #f) next-id : exact-nonnegative-integer?
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
procedure
(with-cdp body [ #:browser browser #:args args]) → any body : (-> cdp? any) browser : (or/c path-string? #f) = #f args : (listof string?) = '()
procedure
(cdp-call c method [params session]) → jsexpr?
c : cdp? method : string? params : jsexpr? = (hasheq) session : (or/c string? #f) = (cdp-session c)
procedure
(cdp-evaluate c expr) → jsexpr?
c : cdp? expr : string?
procedure
c : cdp? expr : string? timeout-ms : exact-nonnegative-integer? = 30000
procedure
(cdp-navigate c url) → (or/c string? #f)
c : cdp? url : string?
procedure
(cdp-screenshot c [#:full-page? full-page?]) → bytes?
c : cdp? full-page? : boolean? = #f
value
a4-paper : jsexpr?
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>"
3 Browser discovery and command-line helpers
procedure
(find-browser) → (or/c path-string? #f)
procedure
(browser-error) → any
procedure
(path->file-url p) → string?
p : path-string?
procedure
(with-temp-directory body) → any
body : (-> path? any)
procedure
(run-process exe args [#:timeout timeout])
→
(or/c exact-integer? #f) string? exe : path-string? args : (listof string?) timeout : real? = 30
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
procedure
(headless-print-pdf url out-pdf [ #:timeout timeout]) → void? url : string? out-pdf : path-string? timeout : real? = 60
4 WebSocket
struct
in : input-port? out : output-port?
procedure
(ws-connect host port ws-path) → websocket?
host : string? port : exact-nonnegative-integer? ws-path : string?
procedure
(ws-connect-url host port url) → websocket?
host : string? port : exact-nonnegative-integer? url : string?
procedure
ws : websocket? payload : bytes?
procedure
ws : websocket?
procedure
(ws-send-raw! ws op payload) → void?
ws : websocket? op : exact-nonnegative-integer? payload : bytes?
procedure
ws : websocket?
procedure
(ws-accept-key key) → bytes?
key : bytes?
5 License
MIT.