browser_commander/puppeteer/api/
browser_context.rs1#![allow(clippy::all, missing_docs, unused_imports, non_snake_case)]
5
6use serde_json::Value;
7
8use super::*;
9use crate::puppeteer::bridge::{binary, remote_type, BridgeError, JsFunction, Remote};
10
11remote_type!(
12 BrowserContext
17);
18
19impl BrowserContext {
20 pub async fn on(&self, type_: Value, handler: impl Into<JsFunction>) -> Result<BrowserContext, BridgeError> {
26 self.remote.call("on", vec![Some(type_), Some(handler.into().to_wire())]).await
27 }
28
29 pub async fn off(&self, type_: Value, handler: Option<JsFunction>) -> Result<BrowserContext, BridgeError> {
35 self.remote.call("off", vec![Some(type_), handler.map(|x| x.to_wire())]).await
36 }
37
38 pub async fn emit(&self, type_: Value, event: Value) -> Result<bool, BridgeError> {
44 self.remote.call("emit", vec![Some(type_), Some(event)]).await
45 }
46
47 pub async fn once(&self, type_: Value, handler: impl Into<JsFunction>) -> Result<BrowserContext, BridgeError> {
53 self.remote.call("once", vec![Some(type_), Some(handler.into().to_wire())]).await
54 }
55
56 pub async fn listener_count(&self, type_: Value) -> Result<f64, BridgeError> {
62 self.remote.call("listenerCount", vec![Some(type_)]).await
63 }
64
65 pub async fn remove_all_listeners(&self, type_: Option<Value>) -> Result<BrowserContext, BridgeError> {
71 self.remote.call("removeAllListeners", vec![type_]).await
72 }
73
74 pub async fn targets(&self) -> Result<Vec<Target>, BridgeError> {
80 self.remote.call("targets", vec![]).await
81 }
82
83 pub async fn wait_for_target(&self, predicate: impl Into<JsFunction>, options: Option<Value>) -> Result<Target, BridgeError> {
89 self.remote.call("waitForTarget", vec![Some(predicate.into().to_wire()), options]).await
90 }
91
92 pub async fn pages(&self, include_all: Option<bool>) -> Result<Vec<Page>, BridgeError> {
98 self.remote.call("pages", vec![include_all.map(|x| Value::from(x))]).await
99 }
100
101 pub async fn override_permissions(&self, origin: &str, permissions: &[&str]) -> Result<(), BridgeError> {
107 self.remote.call("overridePermissions", vec![Some(Value::from(origin)), Some(Value::Array(permissions.iter().map(|v| Value::from(*v)).collect()))]).await
108 }
109
110 pub async fn set_permission(&self, origin: &str, permissions: &[Value]) -> Result<(), BridgeError> {
116 let mut call_args: Vec<Option<Value>> = vec![Some(Value::from(origin))];
117 call_args.extend(permissions.iter().map(|v| Some(v.clone())));
118 self.remote.call("setPermission", call_args).await
119 }
120
121 pub async fn clear_permission_overrides(&self) -> Result<(), BridgeError> {
127 self.remote.call("clearPermissionOverrides", vec![]).await
128 }
129
130 pub async fn new_page(&self, options: Option<Value>) -> Result<Page, BridgeError> {
136 self.remote.call("newPage", vec![options]).await
137 }
138
139 pub async fn browser(&self) -> Result<Browser, BridgeError> {
145 self.remote.call("browser", vec![]).await
146 }
147
148 pub async fn close(&self) -> Result<(), BridgeError> {
154 self.remote.call("close", vec![]).await
155 }
156
157 pub async fn cookies(&self) -> Result<Vec<Value>, BridgeError> {
163 self.remote.call("cookies", vec![]).await
164 }
165
166 pub async fn set_cookie(&self, cookies: &[Value]) -> Result<(), BridgeError> {
172 let mut call_args: Vec<Option<Value>> = vec![];
173 call_args.extend(cookies.iter().map(|v| Some(v.clone())));
174 self.remote.call("setCookie", call_args).await
175 }
176
177 pub async fn delete_cookie(&self, cookies: &[Value]) -> Result<(), BridgeError> {
183 let mut call_args: Vec<Option<Value>> = vec![];
184 call_args.extend(cookies.iter().map(|v| Some(v.clone())));
185 self.remote.call("deleteCookie", call_args).await
186 }
187
188 pub async fn delete_matching_cookies(&self, filters: &[Value]) -> Result<(), BridgeError> {
194 let mut call_args: Vec<Option<Value>> = vec![];
195 call_args.extend(filters.iter().map(|v| Some(v.clone())));
196 self.remote.call("deleteMatchingCookies", call_args).await
197 }
198
199 pub async fn closed(&self) -> Result<bool, BridgeError> {
205 self.remote.get("closed").await
206 }
207
208 pub async fn id(&self) -> Result<Option<String>, BridgeError> {
214 self.remote.get("id").await
215 }
216}