1use serde_json::json;
2
3use crate::client::CdpClient;
4use crate::error::Result;
5
6impl CdpClient {
7 pub async fn set_content(&self, html: &str) -> Result<()> {
8 let frame_id = {
9 let result = self.send_command("Page.getFrameTree", json!({})).await?;
10 result["frameTree"]["frame"]["id"]
11 .as_str()
12 .unwrap_or("")
13 .to_string()
14 };
15 self.send_command(
16 "Page.setDocumentContent",
17 json!({
18 "frameId": frame_id,
19 "html": html,
20 }),
21 )
22 .await?;
23 Ok(())
24 }
25
26 pub async fn print_to_pdf(&self, path: &str) -> Result<()> {
27 let result = self
28 .send_command(
29 "Page.printToPDF",
30 json!({
31 "printBackground": true,
32 }),
33 )
34 .await?;
35 let data = result["data"].as_str().unwrap_or("");
36 use base64::Engine;
37 let bytes = base64::engine::general_purpose::STANDARD
38 .decode(data)
39 .map_err(|e| crate::error::CdpError::Protocol(e.to_string()))?;
40 tokio::fs::write(path, bytes).await?;
41 Ok(())
42 }
43
44 pub async fn add_init_script(&self, source: &str) -> Result<String> {
45 let result = self
46 .send_command(
47 "Page.addScriptToEvaluateOnNewDocument",
48 json!({ "source": source }),
49 )
50 .await?;
51 Ok(result["identifier"].as_str().unwrap_or("").to_string())
52 }
53
54 pub async fn remove_init_script(&self, identifier: &str) -> Result<()> {
55 self.send_command(
56 "Page.removeScriptToEvaluateOnNewDocument",
57 json!({ "identifier": identifier }),
58 )
59 .await?;
60 Ok(())
61 }
62
63 pub async fn set_user_agent(&self, ua: &str) -> Result<()> {
64 self.send_command("Emulation.setUserAgentOverride", json!({ "userAgent": ua }))
65 .await?;
66 Ok(())
67 }
68
69 pub async fn set_geolocation(
70 &self,
71 latitude: f64,
72 longitude: f64,
73 accuracy: f64,
74 ) -> Result<()> {
75 self.send_command(
76 "Emulation.setGeolocationOverride",
77 json!({
78 "latitude": latitude,
79 "longitude": longitude,
80 "accuracy": accuracy,
81 }),
82 )
83 .await?;
84 Ok(())
85 }
86
87 pub async fn set_offline(&self, offline: bool) -> Result<()> {
88 self.send_command(
89 "Network.emulateNetworkConditions",
90 json!({
91 "offline": offline,
92 "latency": 0,
93 "downloadThroughput": -1,
94 "uploadThroughput": -1,
95 }),
96 )
97 .await?;
98 Ok(())
99 }
100
101 pub async fn set_attribute(&self, node_id: i64, name: &str, value: &str) -> Result<()> {
102 self.send_command(
103 "DOM.setAttributeValue",
104 json!({
105 "nodeId": node_id,
106 "name": name,
107 "value": value,
108 }),
109 )
110 .await?;
111 Ok(())
112 }
113
114 pub async fn set_outer_html(&self, node_id: i64, html: &str) -> Result<()> {
115 self.send_command(
116 "DOM.setOuterHTML",
117 json!({
118 "nodeId": node_id,
119 "outerHTML": html,
120 }),
121 )
122 .await?;
123 Ok(())
124 }
125
126 pub async fn remove_node(&self, node_id: i64) -> Result<()> {
127 self.send_command("DOM.removeNode", json!({ "nodeId": node_id }))
128 .await?;
129 Ok(())
130 }
131
132 pub async fn call_function_on(
133 &self,
134 object_id: &str,
135 function_declaration: &str,
136 ) -> Result<serde_json::Value> {
137 let result = self
138 .send_command(
139 "Runtime.callFunctionOn",
140 json!({
141 "objectId": object_id,
142 "functionDeclaration": function_declaration,
143 "returnByValue": true,
144 }),
145 )
146 .await?;
147 Ok(result["result"]["value"].clone())
148 }
149}