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
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
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::*,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl BrowserContext {
// service_workers
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub enum Event {
// BackgroundPage for chromium persistent
// ServiceWorker
Expand All @@ -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
Expand Up @@ -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>
}
Expand Down
7 changes: 4 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
path::{Path, PathBuf, MAIN_SEPARATOR}
};

const DRIVER_VERSION: &str = "1.11.0-1620331022000";
// const DRIVER_VERSION: &str = "1.11.0-1620331022000";
const DRIVER_VERSION: &str = "1.12.2";
const NEXT: &str = "";

fn main() {
let out_dir: PathBuf = env::var_os("OUT_DIR").unwrap().into();
Expand Down Expand Up @@ -79,10 +81,9 @@ fn url(platform: PlaywrightPlatform) -> String {
// .contains("next")
// .then(|| "/next")
// .unwrap_or_default();
let next = "/next";
format!(
"https://playwright.azureedge.net/builds/driver{}/playwright-{}-{}.zip",
next, DRIVER_VERSION, platform
NEXT, DRIVER_VERSION, platform
)
}

Expand Down
19 changes: 15 additions & 4 deletions src/imp/browser_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)?;
Expand All @@ -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(())
Expand All @@ -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 {
Expand All @@ -278,7 +287,8 @@ impl EventEmitter for BrowserContext {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EventType {
Close,
Page
Page,
Request
}

impl IsEvent for Evt {
Expand All @@ -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,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/imp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ pub struct PdfMargins<'a, 'b, 'c, 'd> {
#[derive(Debug, Serialize, PartialEq)]
pub struct File {
pub name: String,
pub mime: String,
pub mimeType: String,
pub buffer: String
}

impl File {
pub fn new(name: String, mime: String, body: &[u8]) -> Self {
pub fn new(name: String, mimeType: String, body: &[u8]) -> Self {
let buffer = base64::encode(body);
Self { name, mime, buffer }
Self { name, mimeType, buffer }
}
}
/// Browser distribution channel.
Expand Down