1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::path::Path;
use std::sync::Arc;
use futures::channel::oneshot::channel as oneshot_channel;
use futures::SinkExt;
use chromiumoxide_cdp::cdp::browser_protocol;
use chromiumoxide_cdp::cdp::browser_protocol::dom::*;
use chromiumoxide_cdp::cdp::browser_protocol::network::{
Cookie, GetCookiesParams, SetUserAgentOverrideParams,
};
use chromiumoxide_cdp::cdp::browser_protocol::page::*;
use chromiumoxide_cdp::cdp::browser_protocol::target::{ActivateTargetParams, SessionId, TargetId};
use chromiumoxide_cdp::cdp::js_protocol;
use chromiumoxide_cdp::cdp::js_protocol::debugger::GetScriptSourceParams;
use chromiumoxide_cdp::cdp::js_protocol::runtime::{EvaluateParams, RemoteObject, ScriptId};
use chromiumoxide_types::*;
use crate::element::Element;
use crate::error::{CdpError, Result};
use crate::handler::target::TargetMessage;
use crate::handler::PageInner;
use crate::layout::Point;
#[derive(Debug)]
pub struct Page {
inner: Arc<PageInner>,
}
impl Page {
pub async fn execute<T: Command>(&self, cmd: T) -> Result<CommandResponse<T::Response>> {
Ok(self.inner.execute(cmd).await?)
}
pub async fn wait_for_navigation(&self) -> Result<&Self> {
self.inner.wait_for_navigation().await?;
Ok(self)
}
pub async fn goto(&self, params: impl Into<NavigateParams>) -> Result<&Self> {
let res = self.execute(params.into()).await?;
if let Some(err) = res.result.error_text {
return Err(CdpError::ChromeMessage(err));
}
Ok(self)
}
pub fn target_id(&self) -> &TargetId {
self.inner.target_id()
}
pub fn session_id(&self) -> &SessionId {
self.inner.session_id()
}
pub async fn url(&self) -> Result<Option<String>> {
let (tx, rx) = oneshot_channel();
self.inner
.sender()
.clone()
.send(TargetMessage::Url(tx))
.await?;
Ok(rx.await?)
}
pub async fn mainframe(&self) -> Result<Option<FrameId>> {
let (tx, rx) = oneshot_channel();
self.inner
.sender()
.clone()
.send(TargetMessage::MainFrame(tx))
.await?;
Ok(rx.await?)
}
pub async fn set_user_agent(
&self,
params: impl Into<SetUserAgentOverrideParams>,
) -> Result<&Self> {
self.execute(params.into()).await?;
Ok(self)
}
pub async fn get_document(&self) -> Result<Node> {
let resp = self.execute(GetDocumentParams::default()).await?;
Ok(resp.result.root)
}
pub async fn find_element(&self, selector: impl Into<String>) -> Result<Element> {
let root = self.get_document().await?.node_id;
let node_id = self.inner.find_element(selector, root).await?;
Ok(Element::new(Arc::clone(&self.inner), node_id).await?)
}
pub async fn find_elements(&self, selector: impl Into<String>) -> Result<Vec<Element>> {
let root = self.get_document().await?.node_id;
let node_ids = self.inner.find_elements(selector, root).await?;
Ok(Element::from_nodes(&self.inner, &node_ids).await?)
}
pub async fn describe_node(&self, node_id: NodeId) -> Result<Node> {
let resp = self
.execute(
DescribeNodeParams::builder()
.node_id(node_id)
.depth(100)
.build(),
)
.await?;
Ok(resp.result.node)
}
pub async fn close(self) {
todo!()
}
pub async fn move_mouse_to_point(&self, point: Point) -> Result<&Self> {
self.inner.move_mouse_to_point(point).await?;
Ok(self)
}
pub async fn click(&self, point: Point) -> Result<&Self> {
self.inner.click(point).await?;
Ok(self)
}
pub async fn pdf(&self, opts: PrintToPdfParams) -> Result<Vec<u8>> {
let res = self.execute(opts).await?;
Ok(base64::decode(&res.data)?)
}
pub async fn save_pdf(
&self,
opts: PrintToPdfParams,
output: impl AsRef<Path>,
) -> Result<Vec<u8>> {
let pdf = self.pdf(opts).await?;
async_std::fs::write(output.as_ref(), &pdf).await?;
Ok(pdf)
}
pub async fn enable_log(&self) -> Result<&Self> {
self.execute(browser_protocol::log::EnableParams::default())
.await?;
Ok(self)
}
pub async fn disable_log(&self) -> Result<&Self> {
self.execute(browser_protocol::log::DisableParams::default())
.await?;
Ok(self)
}
pub async fn enable_runtime(&self) -> Result<&Self> {
self.execute(js_protocol::runtime::EnableParams::default())
.await?;
Ok(self)
}
pub async fn disable_runtime(&self) -> Result<&Self> {
self.execute(js_protocol::runtime::DisableParams::default())
.await?;
Ok(self)
}
pub async fn enable_debugger(&self) -> Result<&Self> {
self.execute(js_protocol::debugger::EnableParams::default())
.await?;
Ok(self)
}
pub async fn disable_debugger(&self) -> Result<&Self> {
self.execute(js_protocol::debugger::DisableParams::default())
.await?;
Ok(self)
}
pub async fn activate(&self) -> Result<&Self> {
self.execute(ActivateTargetParams::new(self.inner.target_id().clone()))
.await?;
Ok(self)
}
pub async fn get_cookies(&self) -> Result<Vec<Cookie>> {
Ok(self
.execute(GetCookiesParams::default())
.await?
.result
.cookies)
}
pub async fn get_title(&self) -> Result<Option<String>> {
let remote_object = self.evaluate("document.title").await?;
let title: String = serde_json::from_value(
remote_object
.value
.ok_or_else(|| CdpError::msg("No title found"))?,
)?;
if title.is_empty() {
Ok(None)
} else {
Ok(Some(title))
}
}
pub async fn evaluate(&self, evaluate: impl Into<EvaluateParams>) -> Result<RemoteObject> {
Ok(self.execute(evaluate.into()).await?.result.result)
}
pub async fn content(&self) -> Result<String> {
let resp = self
.evaluate(
"{
let retVal = '';
if (document.doctype) {
retVal = new XMLSerializer().serializeToString(document.doctype);
}
if (document.documentElement) {
retVal += document.documentElement.outerHTML;
}
retVal
}
",
)
.await?;
let value = resp.value.ok_or(CdpError::NotFound)?;
Ok(serde_json::from_value(value)?)
}
pub async fn get_script_source(&self, script_id: impl Into<String>) -> Result<String> {
Ok(self
.execute(GetScriptSourceParams::new(ScriptId::from(script_id.into())))
.await?
.result
.script_source)
}
}
impl From<Arc<PageInner>> for Page {
fn from(inner: Arc<PageInner>) -> Self {
Self { inner }
}
}