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

feat(api): add page.emulateMedia{Type,Features} #5012

Merged
merged 6 commits into from Oct 23, 2019

Conversation

mathiasbynens
Copy link
Member

@mathiasbynens mathiasbynens commented Oct 7, 2019

Note: this PR is just to illustrate what the API could look like. The goal is to get feedback and iterate on the approach.

CDP supports emulating CSS media features (e.g. prefers-color-scheme, prefers-reduced-motion) in addition to media types (e.g. screen, print) as of Chromium v79.0.3929.0+.

An open question is, how do we expose this functionality as a Puppeteer API? IMHO, there's two main options:

  1. Provide two separate APIs: one for media types (page.emulateMediaType(type)), and one for media features: page.emulateMediaFeatures(features)
  2. Provide a single overloaded API that supports media types and media features: page.emulateMedia(typeOrFeatures)

This PR implements the first option, which feels cleanest to me. Note that in this WIP PR, page.emulateMedia(mediaType) has been renamed to page.emulateMediaType(type) (a breaking change!) to make the distinction between the two APIs more clear. We could of course decide to keep the old API for the time being as an alias.

Thoughts?

Ref. #4906.

Blocked on a Chromium roll to v79.0.3929.0 (r701361).

lib/Page.js Show resolved Hide resolved
@mathiasbynens
Copy link
Member Author

Added the old page.emulateMedia back as an alias.

Copy link
Collaborator

@szuend szuend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! lgtm

docs/api.md Outdated Show resolved Hide resolved
lib/Page.js Show resolved Hide resolved
lib/Page.js Show resolved Hide resolved
@aslushnikov aslushnikov mentioned this pull request Oct 23, 2019
13 tasks
@mathiasbynens
Copy link
Member Author

Is there a tracking issue documenting that we should remove deprecate emulateMedia for 2.0?

#2079

@mathiasbynens mathiasbynens merged commit a86363f into master Oct 23, 2019
@mathiasbynens mathiasbynens deleted the emulate-css-media-features branch October 23, 2019 11:55
Comment on lines +1302 to +1312
await page.emulateMediaFeatures([{ name: 'prefers-reduced-motion', value: 'reduce' }]);
await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches));
// → true
await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches));
// → false

await page.emulateMediaFeatures([{ name: 'prefers-reduced-motion', value: 'reduce' }]);
await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches));
// → true
await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches));
// → false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems these blocks are duplicate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemozhetbyt Wanna submit a patch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope it's right: #5072

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants