Skip to content

Commit

Permalink
emulate_media_type function (#232)
Browse files Browse the repository at this point in the history
* Added emulate_media_type function

See: https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setEmulatedMedia
(media is unused in function emulate_media_features)

Also See: https://pptr.dev/api/puppeteer.page.emulatemediatype
This is basically the newly implemented function

* rustfmt

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Nathan-Mossaad and mattsse authored Aug 12, 2024
1 parent 6968e92 commit ebff8d7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,21 @@ impl Page {
Ok(self)
}

/// Changes the CSS media type of the page
// Based on https://pptr.dev/api/puppeteer.page.emulatemediatype
pub async fn emulate_media_type(
&self,
media_type: impl Into<MediaTypeParams>,
) -> Result<&Self> {
self.execute(
SetEmulatedMediaParams::builder()
.media(media_type.into())
.build(),
)
.await?;
Ok(self)
}

/// Overrides default host system timezone
pub async fn emulate_timezone(
&self,
Expand Down Expand Up @@ -1348,3 +1363,23 @@ impl From<CaptureScreenshotParams> for ScreenshotParams {
}
}
}

#[derive(Debug, Clone, Copy, Default)]
pub enum MediaTypeParams {
/// Default CSS media type behavior for page and print
#[default]
Null,
/// Force screen CSS media type for page and print
Screen,
/// Force print CSS media type for page and print
Print,
}
impl From<MediaTypeParams> for String {
fn from(media_type: MediaTypeParams) -> Self {
match media_type {
MediaTypeParams::Null => "null".to_string(),
MediaTypeParams::Screen => "screen".to_string(),
MediaTypeParams::Print => "print".to_string(),
}
}
}

0 comments on commit ebff8d7

Please sign in to comment.