Updates the README, CLI and library references, how-to guides, architecture notes and the camoufox-browse skill for upload, record/replay, launch --profile-dir, external subcommands, CAMOUFOX_SOCKET/CAMOUFOX_JSON, error kinds and Browser::default_context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 KiB
Rust Library API Reference: camoufox
Reference for the public modules and types exported by the camoufox crate.
Crate Layout & Module Overview
camoufox/
├── api/ # High-level browser automation (Browser, BrowserContext, MainFrame)
├── codec/ # Wire encoding/decoding (NUL-delimited JSON RPC frames)
├── config/ # Launch arguments, profile layout, environment variables
├── obs/ # Observability, protocol tracing, structured logging
├── process/ # Unix process management (spawn, pipe setup, readiness, lifecycle)
├── protocol/ # Juggler protocol types, error kinds, event demuxing, session state
├── transport/ # Reader/writer transport abstractions
└── cli/ # Daemon and client CLI infrastructure (feature = "cli")
1. camoufox::api
High-level browser automation layer.
Browser
Represents a running browser connection attached to the root Juggler session.
# fn doc_test(conn: camoufox::protocol::client::Connection, root: camoufox::protocol::client::Session) -> Result<(), Box<dyn std::error::Error>> {
use camoufox::api::browser::{Browser, BrowserOptions};
use camoufox::api::context::ContextOptions;
let browser = Browser::connect(conn, root, BrowserOptions::default())?;
let context = browser.new_context(ContextOptions::default())?;
browser.close()?;
# Ok(())
# }
Key methods:
Browser::connect(connection, root_session, options): Connect to an established Juggler connection.browser.new_context(options): Create a new isolated profile context.browser.default_context(options): Configure and return theBrowserContextfor the browser's persistent on-disk profile. Requires connecting withBrowserOptions::attach_to_default_context: trueto receive page and popup events. Itsclose()only drops the handle; callBrowser::close()to close pages and flush profile storage.browser.version(): Returns the browser version string if queried.browser.user_agent(): Returns the browser User-Agent string.browser.close(): Consumes the handle and gracefully terminates the browser.
BrowserContext
Represents an isolated browsing context or the browser's persistent default
context, with its own cookies and storage. new_context creates a private
context; default_context uses the on-disk launch profile.
# fn doc_test(context: &camoufox::api::context::BrowserContext) -> Result<(), Box<dyn std::error::Error>> {
use camoufox::api::context::CookieOptions;
// Open a new page pinned to its top frame
let page = context.new_main_frame()?;
// Inspect cookies
let cookies = context.get_cookies()?;
// Set cookies
let cookie = CookieOptions {
name: "session".into(),
value: "abc".into(),
url: None,
domain: Some(".example.com".into()),
path: Some("/".into()),
secure: None,
http_only: None,
same_site: None,
expires: None,
};
context.set_cookies(&[cookie])?;
// Clear cookies
context.clear_cookies()?;
// Retrieve newly opened popups adopted by the registry
let popups = context.take_popups();
# Ok(())
# }
Key methods:
context.context_id(): Returns the context's unique ID string. For the default context, this is empty ("").context.new_main_frame(): Creates a new tab/page and returns aMainFramehandle pinned to the top frame.context.get_cookies(): Retrieves all cookies stored in the context.context.set_cookies(cookies): Sets one or more cookies.context.clear_cookies(): Clears all cookies from the context.context.take_popups(): Returns and drains any popup pages adopted by the context registry.context.close(): Consumes the handle, closes and removes a private context. For the default context, it only drops the handle and returnsOk(()); useBrowser::close()to close pages and flush storage.
MainFrame
Represents an open page handle structurally pinned to the top frame.
# fn doc_test(page: &camoufox::api::main_frame::MainFrame) -> Result<(), Box<dyn std::error::Error>> {
use camoufox::api::dialog::DialogPolicy;
use camoufox::api::main_frame::{NavigateOptions, NavigateOutcome};
use std::time::Duration;
let timeout = Duration::from_secs(10);
// Navigate
let outcome: NavigateOutcome = page.navigate("https://example.com", NavigateOptions::default(), timeout)?;
let _status = outcome.status_code;
// JavaScript evaluation
let raw_title: serde_json::Value = page.evaluate("document.title", timeout)?;
let main_world_val: serde_json::Value = page.evaluate_main_world("window.location.href", timeout)?;
// Dialog handling
page.set_dialog_policy(DialogPolicy::Manual);
if let Some(d) = page.pending_dialog() {
page.handle_dialog(&d.id, true, None);
}
// History & reload
page.reload()?;
page.go_back()?;
page.go_forward()?;
// Frame inspection
let top_id = page.frame_id();
let frames = page.frames();
// Close page
page.close()?;
# Ok(())
# }
Key methods:
page.navigate(url, options, timeout): Navigates to a URL. ReturnsResult<camoufox::api::main_frame::NavigateOutcome, ProtocolError>containingnav_id: Option<String>andstatus_code: Option<u16>.page.evaluate(expr, timeout): Evaluates JavaScript in the isolated world. Returns rawResult<serde_json::Value, ProtocolError>matching the JugglerRuntime.evaluateresponse (value is underval["result"]["value"]). Page exceptions returnOkcontaining anexceptionDetailsobject.page.evaluate_main_world(expr, timeout): Evaluates JavaScript in the main page world. Unwraps and returns the evaluated value directly asResult<serde_json::Value, ProtocolError>.page.evaluate_in_frame(frame_id, expr, timeout): Evaluates JavaScript scoped to a child frame. Returns rawResult<serde_json::Value, ProtocolError>matchingevaluate.page.resolve_child_frame(parent_id, selector, timeout): Resolves a child frame ID from a CSS selector.page.set_file_input_files(frame_id, object_id, files): Sets files on a file input element with a default 60-second timeout. Dispatchesinputandchangeevents.page.set_file_input_files_with_timeout(frame_id, object_id, files, timeout): Sets files on a file input element within an explicit command timeout. Dispatchesinputandchangeevents.page.insert_text(text): Inserts text into the currently focused element.page.dispatch_key_event(params): Dispatches a raw keyboard event.page.dispatch_mouse_event(params): Dispatches a raw mouse event.page.dispatch_wheel_event(params): Dispatches a raw wheel scroll event.page.screenshot(options): Captures a PNG or JPEG screenshot.page.pending_dialog(): Returns the currently pending dialog, if any.page.handle_dialog(dialog_id, accept, prompt_text): Responds to a dialog.page.set_dialog_policy(policy): Sets dialog handling policy (Manual,Accept,Dismiss).page.frames(): Returns tracked frame IDs and their parent frame IDs.page.is_closed(): Returns true if the page session was closed or detached.page.close(): Closes the page target.
File-input methods take frame_id: &str, object_id: &str, and files: &[&str];
the timeout variant also takes std::time::Duration. The object ID must refer to
the file input in the target frame, obtained through Runtime.evaluate with
returnByValue: false. Release it with page.dispose_object(context_id, object_id)
after use. These methods send the protocol request directly; the CLI's upload
adds selector/frame/shadow resolution and validates file paths and input attributes.
2. camoufox::config
Process and launch configuration.
LaunchConfig
Configuration struct passed to process::unix::spawn:
use camoufox::config::LaunchConfig;
use std::path::PathBuf;
let config = LaunchConfig {
executable: PathBuf::from("/path/to/camoufox"),
profile_dir: Some(PathBuf::from("/tmp/profile")),
headless: true,
args: vec![],
env: std::collections::HashMap::new(),
persistent: false,
timeout: std::time::Duration::from_secs(180),
close_timeout: std::time::Duration::from_secs(30),
};
Note: profile_dir must be Some(...). LaunchConfig::validate() returns Err(ConfigError::MissingProfile) when it is None; process::unix::spawn runs that validation and surfaces it as a ProcessError wrapping an io::Error with the message "profile_dir must be set before launch".
default_executable()
let exe: std::path::PathBuf = camoufox::config::default_executable();
Where the CLI and the integration tests find the browser when no path is given:
$CAMOUFOX_BIN, if set and non-empty.- The install marked active in
<cache>/config.json(active_version). - The newest
<cache>/browsers/<repo>/<version>/camoufox. <cache>/camoufox(legacy layout), returned even if missing.
<cache> is $XDG_CACHE_HOME/camoufox when that is an absolute path, else $HOME/.cache/camoufox. This matches where the Python camoufox fetch installs on Linux; on macOS, set CAMOUFOX_BIN.
3. camoufox::process (Unix Only)
Unix process spawning, readiness detection, and lifecycle management.
process::unix::spawn(config): Launches the browser process with anonymous pipes on fd 3 and fd 4. Returns aLaunchedProcesscontainingchild,command_pipe, andresponse_pipe.process::readiness::wait_for_ready(child, timeout): Watches stdout and stderr concurrently for the Juggler readiness banner.
4. camoufox::protocol
Low-level Juggler protocol types, error handling, and session state.
ProtocolErrorKind
Non-exhaustive enum classifying protocol error variants:
Response: Server returned a protocol error object.FrameDetached: Scoped frame was detached or navigated away.ScriptException: JavaScript evaluation threw an exception.NotIframe: Matched element is not an iframe.FrameNotFound: Selector did not match any iframe.ShadowRoot: Element has no open shadow root.Closed: Target or connection was closed.Crashed: Renderer process crashed.DialogOpened: Interrupted by an unhandled JavaScript dialog.Transport: Pipe or socket I/O error.Timeout: Request deadline expired.NavigationBecameDownload: Navigation diverted into a file download.
DownloadInfo
Structured metadata attached to ProtocolError when kind is NavigationBecameDownload:
url: Stringframe_id: Option of Stringdownload_id: Option of String
5. camoufox::obs
Observability and structured protocol logging.
ProtocolLogger: Unit struct (pub struct ProtocolLogger;) and logging helpers for tracing incoming and outgoing protocol traffic atTRACElevel.
6. camoufox::cli (feature = "cli")
InstanceManager::upload(instance_id, page_id, selector, paths, frames, timeout): Requires absolute regular-file paths, validated before page access. Returns basenames and anInteractionOutcomethat may contain a dialog.InstanceManager::{record_start, record_stop, replay_start, replay_stop}: Manage per-page HTTP cassettes, including subframes. Start methods take a cassette directory; recording also takes URL regex filters, replay amatch_bodyboolean. Stop methods return JSON counts and retained repeat results.LaunchOptions::profile_dir: Optional persistent profile path. UseLaunchOptions::default()followed by field assignment to configure it.prepare_profile_dir(path): Validates an absolute empty directory or profile containingprefs.jsortimes.json, creates missing directories with mode 0700, removes group/other permissions, and returns the canonical path.Instance::persistent_profile_dir(): Returns the canonical persistent path, orNonefor temporary profiles.commands::{RecordAction, ReplayAction}: CLI start/stop actions.commands::find_external_subcommand(name): Finds an executablecamoufox-<name>in absolutePATHentries; built-in dispatch takes precedence.ipc::DaemonResponse:ok, optionaldata, optionalerror, and optional errorkind. Useok,ok_empty, anderrconstructors. Success omitskind;client::send_requestbackfills missing error kinds from older daemon messages and preserves daemon-provided values, including unknown values.error_kind::ErrorKind: Stable snake_case categories for selection, timeouts, dialogs, frames, navigation, instance/page lookup, arguments, I/O, daemon, and other errors. Treat unknown wire values asother. CLI execution failures exit 1; client argument validation exits 2 withinvalid_argument, while local path-resolution failures exit 1 withio. Clap parsing errors exit 2 with text diagnostics. External subcommands forward their own exit status.
ErrorKind, DaemonRequest, DaemonResponse, and LaunchOptions are
#[non_exhaustive]; enum matches require a wildcard. ManagedMainFrame.main_frame
is Arc<MainFrame>; private fields prevent struct-literal construction and require
.. when destructuring. See the CLI reference for cassette
format, scope, lifecycle, and external-subcommand environment conventions.