browser-commander 0.17.0

Universal browser automation library that supports multiple browser engines with a unified API
Documentation
// @generated by scripts/generate-puppeteer-bindings.mjs from
// rust/protocol/puppeteer/api.json. Do not edit by hand.

#![allow(clippy::all, missing_docs, unused_imports, non_snake_case)]

use serde_json::Value;

use super::*;
use crate::puppeteer::bridge::{binary, remote_type, BridgeError, JsFunction, Remote};

remote_type!(
    /// Puppeteer's `WebWorker` (puppeteer-core 25.10.0), a remote object
    /// served by `browser-commander serve --stdio`.
    ///
    /// Extends [`EventEmitter`]; the inherited methods are repeated here.
    WebWorker
);

impl WebWorker {
    /// `EventEmitter.on`
    ///
    /// ```text
    /// on<Key extends keyof EventsWithWildcard<Events>>(type: Key, handler: Handler<EventsWithWildcard<Events>[Key]>): this
    /// ```
    pub async fn on(&self, type_: Value, handler: impl Into<JsFunction>) -> Result<WebWorker, BridgeError> {
        self.remote.call("on", vec![Some(type_), Some(handler.into().to_wire())]).await
    }

    /// `EventEmitter.off`
    ///
    /// ```text
    /// off<Key extends keyof EventsWithWildcard<Events>>(type: Key, handler?: Handler<EventsWithWildcard<Events>[Key]>): this
    /// ```
    pub async fn off(&self, type_: Value, handler: Option<JsFunction>) -> Result<WebWorker, BridgeError> {
        self.remote.call("off", vec![Some(type_), handler.map(|x| x.to_wire())]).await
    }

    /// `EventEmitter.emit`
    ///
    /// ```text
    /// emit<Key extends keyof EventsWithWildcard<Events>>(type: Key, event: EventsWithWildcard<Events>[Key]): boolean
    /// ```
    pub async fn emit(&self, type_: Value, event: Value) -> Result<bool, BridgeError> {
        self.remote.call("emit", vec![Some(type_), Some(event)]).await
    }

    /// `EventEmitter.once`
    ///
    /// ```text
    /// once<Key extends keyof EventsWithWildcard<Events>>(type: Key, handler: Handler<EventsWithWildcard<Events>[Key]>): this
    /// ```
    pub async fn once(&self, type_: Value, handler: impl Into<JsFunction>) -> Result<WebWorker, BridgeError> {
        self.remote.call("once", vec![Some(type_), Some(handler.into().to_wire())]).await
    }

    /// `EventEmitter.listenerCount`
    ///
    /// ```text
    /// listenerCount(type: keyof EventsWithWildcard<Events>): number
    /// ```
    pub async fn listener_count(&self, type_: Value) -> Result<f64, BridgeError> {
        self.remote.call("listenerCount", vec![Some(type_)]).await
    }

    /// `EventEmitter.removeAllListeners`
    ///
    /// ```text
    /// removeAllListeners(type?: keyof EventsWithWildcard<Events>): this
    /// ```
    pub async fn remove_all_listeners(&self, type_: Option<Value>) -> Result<WebWorker, BridgeError> {
        self.remote.call("removeAllListeners", vec![type_]).await
    }

    /// `WebWorker.url`
    ///
    /// ```text
    /// url(): string
    /// ```
    pub async fn url(&self) -> Result<String, BridgeError> {
        self.remote.call("url", vec![]).await
    }

    /// `WebWorker.client`
    ///
    /// ```text
    /// get client(): CDPSession
    /// ```
    pub async fn client(&self) -> Result<CDPSession, BridgeError> {
        self.remote.get("client").await
    }

    /// `WebWorker.evaluate`
    ///
    /// ```text
    /// evaluate<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(func: Func | string, ...args: Params): Promise<Awaited<ReturnType<Func>>>
    /// ```
    pub async fn evaluate(&self, func: impl Into<JsFunction>, args: &[Value]) -> Result<Value, BridgeError> {
        let mut call_args: Vec<Option<Value>> = vec![Some(func.into().to_wire())];
        call_args.extend(args.iter().map(|v| Some(v.clone())));
        self.remote.call("evaluate", call_args).await
    }

    /// `WebWorker.evaluateHandle`
    ///
    /// ```text
    /// evaluateHandle<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(func: Func | string, ...args: Params ): Promise<HandleFor<Awaited<ReturnType<Func>>>>
    /// ```
    pub async fn evaluate_handle(&self, func: impl Into<JsFunction>, args: &[Value]) -> Result<JSHandle, BridgeError> {
        let mut call_args: Vec<Option<Value>> = vec![Some(func.into().to_wire())];
        call_args.extend(args.iter().map(|v| Some(v.clone())));
        self.remote.call("evaluateHandle", call_args).await
    }

    /// `WebWorker.waitForFunction`
    ///
    /// ```text
    /// waitForFunction<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(workerFunction: Func | string, options?: { polling?: number; timeout?: number; signal?: AbortSignal; }, ...args: Params ): Promise<HandleFor<Awaited<ReturnType<Func>>>>
    /// ```
    pub async fn wait_for_function(&self, worker_function: impl Into<JsFunction>, options: Option<Value>, args: &[Value]) -> Result<JSHandle, BridgeError> {
        let mut call_args: Vec<Option<Value>> = vec![Some(worker_function.into().to_wire()), options];
        call_args.extend(args.iter().map(|v| Some(v.clone())));
        self.remote.call("waitForFunction", call_args).await
    }

    /// `WebWorker.close`
    ///
    /// ```text
    /// close(): Promise<void>
    /// ```
    pub async fn close(&self) -> Result<(), BridgeError> {
        self.remote.call("close", vec![]).await
    }
}