For the complete documentation index, see llms.txt. This page is also available as Markdown.

Adding a CDP method or Web API

Two recipes for the two most common extensions: a new CDP method, and a new JS Web API.

Adding a CDP method

Worked example: MyDomain.doThing that takes { name } and returns { ok }.

1. Add the handler

Create or edit a file under crates/obscura-cdp/src/domains/:

// crates/obscura-cdp/src/domains/my_domain.rs
use serde_json::{json, Value};
use crate::dispatch::CdpContext;

pub async fn do_thing(
    params: &Value,
    _ctx: &mut CdpContext,
    _session_id: &Option<String>,
) -> Result<Value, String> {
    let name = params.get("name")
        .and_then(|v| v.as_str())
        .ok_or("missing name")?;

    // do the work

    Ok(json!({ "ok": true, "name": name }))
}

2. Register in the dispatcher

In crates/obscura-cdp/src/dispatch.rs, add a match arm:

3. Test it

crates/obscura-cdp/tests/cdp_my_domain.rs:

Run:

Adding a Web API

Worked example: crypto.subtle.digest, real implementation backed by a Rust hash op.

1. Add the Rust op

In crates/obscura-js/src/ops.rs:

2. Register the op

In the same file, build_extension():

3. Add the JS shim

In crates/obscura-js/js/bootstrap.js:

4. Add a dependency if needed

crates/obscura-js/Cargo.toml:

5. Smoke test

Tips

  • Keep the JS shim thin. All side effects go through ops.

  • Use Promise.resolve to keep async-shaped APIs callable from sync ops.

  • Match the spec: Web API names and shapes are checked by Puppeteer / Playwright wrappers.

  • DOM mutations go through op_dom, not new ops.

  • For events that need to fire across handlers, use the existing _makeListenerBox helper in bootstrap.js.

Worked examples in the tree

  • CDP method with intercept: crates/obscura-cdp/src/domains/page.rs do_navigate.

  • Web API with op + JS shim: crypto.subtle.digest (above).

  • Web API in pure JS (no op): DOMParser in bootstrap.js.

  • Web API with async event firing: WebSocket, IntersectionObserver in bootstrap.js.

Last updated

Was this helpful?