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

Add request event handling on BrowserContext #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Adds request event handling to BrowserContext
randall-coding committed Apr 25, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6019353db591bcd1baf1fbac84343099dc1a1c8b
12 changes: 8 additions & 4 deletions src/api/browser_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use crate::imp::browser_context::EventType;
use crate::{
api::{Browser, Page},
api::{Browser, Page, request::Request},
imp::{
browser_context::{BrowserContext as Impl, Evt},
core::*,
@@ -219,7 +219,7 @@ impl BrowserContext {
// service_workers
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub enum Event {
// BackgroundPage for chromium persistent
// ServiceWorker
@@ -242,14 +242,18 @@ pub enum Event {
/// ]);
/// console.log(await newPage.evaluate('location.href'));
/// ```
Page(Page)
Page(Page),
/// Emitted when a page issues a request. The request object is read-only. In order to intercept and mutate requests, see
/// [`method: Page.route`] or [`method: BrowserContext.route`].
Request(Request),
}

impl From<Evt> for Event {
fn from(e: Evt) -> Event {
match e {
Evt::Close => Event::Close,
Evt::Page(w) => Event::Page(Page::new(w))
Evt::Page(w) => Event::Page(Page::new(w)),
Evt::Request(w) => Event::Request(Request::new(w)),
}
}
}
2 changes: 1 addition & 1 deletion src/api/request.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ use crate::{
///
/// If request gets a 'redirect' response, the request is successfully finished with the 'requestfinished' event, and a new
/// request is issued to a redirected url.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Request {
inner: Weak<Impl>
}
19 changes: 15 additions & 4 deletions src/imp/browser_context.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ use crate::imp::{
core::*,
page::Page,
prelude::*,
utils::{Cookie, Geolocation, Header, StorageState}
utils::{Cookie, Geolocation, Header, StorageState},
request::Request,
};

#[derive(Debug)]
@@ -244,6 +245,7 @@ impl RemoteObject for BrowserContext {
method: Str<Method>,
params: Map<String, Value>
) -> Result<(), Error> {
println!("BrowserContext method: {}", method.as_str()); //DEBUG!!
match method.as_str() {
"page" => {
let first = first_object(&params).ok_or(Error::InvalidParams)?;
@@ -255,6 +257,12 @@ impl RemoteObject for BrowserContext {
"close" => self.on_close(ctx)?,
"bindingCall" => {}
"route" => self.on_route(ctx, params)?,
"request" => {
let last = params.get("request").ok_or(Error::InvalidParams)?;
let OnlyGuid { guid } = serde_json::from_value((*last).clone())?;
let request = get_object!(ctx, &guid, Request)?;
self.emit_event(Evt::Request(request));
}
_ => {}
}
Ok(())
@@ -264,7 +272,8 @@ impl RemoteObject for BrowserContext {
#[derive(Debug, Clone)]
pub(crate) enum Evt {
Close,
Page(Weak<Page>)
Page(Weak<Page>),
Request(Weak<Request>),
}

impl EventEmitter for BrowserContext {
@@ -278,7 +287,8 @@ impl EventEmitter for BrowserContext {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EventType {
Close,
Page
Page,
Request
}

impl IsEvent for Evt {
@@ -287,7 +297,8 @@ impl IsEvent for Evt {
fn event_type(&self) -> Self::EventType {
match self {
Self::Close => EventType::Close,
Self::Page(_) => EventType::Page
Self::Page(_) => EventType::Page,
Self::Request(_) => EventType::Request,
}
}
}