Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more Page methods #222

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ impl Handler {
.unwrap_or_else(|| self.default_browser_context.clone());
let target = Target::new(
event.target_info,
TargetConfig::new(
self.config.ignore_https_errors,
self.config.request_timeout,
self.config.viewport.clone(),
self.config.request_intercept,
self.config.cache_enabled,
),
TargetConfig {
ignore_https_errors: self.config.ignore_https_errors,
request_timeout: self.config.request_timeout,
viewport: self.config.viewport.clone(),
request_intercept: self.config.request_intercept,
cache_enabled: self.config.cache_enabled,
},
browser_ctx,
);
self.target_ids.push(target.target_id().clone());
Expand Down
9 changes: 8 additions & 1 deletion src/handler/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ pub struct PageHandle {
}

impl PageHandle {
pub fn new(target_id: TargetId, session_id: SessionId) -> Self {
pub fn new(target_id: TargetId, session_id: SessionId, opener_id: Option<TargetId>) -> Self {
let (commands, rx) = channel(1);
let page = PageInner {
target_id,
session_id,
opener_id,
sender: commands,
};
Self {
Expand All @@ -68,6 +69,7 @@ impl PageHandle {
pub(crate) struct PageInner {
target_id: TargetId,
session_id: SessionId,
opener_id: Option<TargetId>,
sender: Sender<TargetMessage>,
}

Expand Down Expand Up @@ -106,6 +108,11 @@ impl PageInner {
&self.session_id
}

/// The identifier of this page's target's opener target
pub fn opener_id(&self) -> &Option<TargetId> {
&self.opener_id
}

pub(crate) fn sender(&self) -> &Sender<TargetMessage> {
&self.sender
}
Expand Down
28 changes: 8 additions & 20 deletions src/handler/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use chromiumoxide_cdp::cdp::events::CdpEvent;
use chromiumoxide_cdp::cdp::CdpEventMessage;
use chromiumoxide_types::{Command, Method, Request, Response};

use crate::auth::Credentials;
use crate::cdp::browser_protocol::target::CloseTargetParams;
use crate::cmd::CommandChain;
use crate::cmd::CommandMessage;
Expand Down Expand Up @@ -161,7 +162,8 @@ impl Target {
fn create_page(&mut self) {
if self.page.is_none() {
if let Some(session) = self.session_id.clone() {
let handle = PageHandle::new(self.target_id().clone(), session);
let handle =
PageHandle::new(self.target_id().clone(), session, self.opener_id().cloned());
self.page = Some(handle);
}
}
Expand All @@ -186,7 +188,7 @@ impl Target {
}

/// Get the target that opened this target. Top-level targets return `None`.
pub fn opener(&self) -> Option<&TargetId> {
pub fn opener_id(&self) -> Option<&TargetId> {
self.info.opener_id.as_ref()
}

Expand Down Expand Up @@ -513,6 +515,9 @@ impl Target {
let _ = tx.send(None);
}
}
TargetMessage::Authenticate(credentials) => {
self.network_manager.authenticate(credentials);
}
}
}
}
Expand Down Expand Up @@ -599,24 +604,6 @@ pub struct TargetConfig {
pub cache_enabled: bool,
}

impl TargetConfig {
pub fn new(
ignore_https_errors: bool,
request_timeout: Duration,
viewport: Option<Viewport>,
request_intercept: bool,
cache_enabled: bool,
) -> Self {
Self {
ignore_https_errors,
request_timeout,
viewport,
request_intercept,
cache_enabled,
}
}
}
mattsse marked this conversation as resolved.
Show resolved Hide resolved

impl Default for TargetConfig {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -793,4 +780,5 @@ pub enum TargetMessage {
AddEventListener(EventListenerRequest),
/// Get the `ExecutionContext` if available
GetExecutionContext(GetExecutionContext),
Authenticate(Credentials),
}
16 changes: 16 additions & 0 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use chromiumoxide_cdp::cdp::js_protocol::runtime::{
use chromiumoxide_cdp::cdp::{browser_protocol, IntoEventKind};
use chromiumoxide_types::*;

use crate::auth::Credentials;
use crate::element::Element;
use crate::error::{CdpError, Result};
use crate::handler::commandfuture::CommandFuture;
Expand Down Expand Up @@ -325,6 +326,11 @@ impl Page {
self.inner.session_id()
}

/// The identifier of the `Session` target of this page is attached to
pub fn opener_id(&self) -> &Option<TargetId> {
self.inner.opener_id()
}

/// Returns the name of the frame
pub async fn frame_name(&self, frame_id: FrameId) -> Result<Option<String>> {
let (tx, rx) = oneshot_channel();
Expand All @@ -339,6 +345,16 @@ impl Page {
Ok(rx.await?)
}

pub async fn authenticate(&self, credentials: Credentials) -> Result<()> {
self.inner
.sender()
.clone()
.send(TargetMessage::Authenticate(credentials))
.await?;

Ok(())
}

/// Returns the current url of the page
pub async fn url(&self) -> Result<Option<String>> {
let (tx, rx) = oneshot_channel();
Expand Down
Loading