Skip to content

Commit

Permalink
Merge pull request #890 from alexwass-lr/feat-add-media-features
Browse files Browse the repository at this point in the history
Add support for media features
  • Loading branch information
AlexVanderbist authored Nov 25, 2024
2 parents 8f7d222 + fd2d6a4 commit 8b6a057
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ const callChrome = async pup => {
await page.emulateMediaType(request.options.emulateMedia);
}

if (request.options && request.options.emulateMediaFeatures) {
await page.emulateMediaFeatures(JSON.parse(request.options.emulateMediaFeatures));
}

if (request.options && request.options.viewport) {
await page.setViewport(request.options.viewport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ Browsershot::url('https://example.com')
->savePdf($pathToPdf);
```

You can also emulate [media features](https://www.w3.org/TR/mediaqueries-5/), such as dark mode or reduced motion.

```php
Browsershot::url('https://example.com')
->emulateMediaFeatures([
['name' => 'prefers-color-scheme', 'value' => 'dark']
])
->save($pathToImage);
```
5 changes: 5 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ public function emulateMedia(?string $media): static
return $this->setOption('emulateMedia', $media);
}

public function emulateMediaFeatures(array $features): static
{
return $this->setOption('emulateMediaFeatures', json_encode($features));
}

public function newHeadless(): self
{
return $this->setOption('newHeadless', true);
Expand Down
23 changes: 23 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,29 @@
], $command);
});

it('can set emulate media features', function () {
$command = Browsershot::url('https://example.com')
->emulateMediaFeatures([
['name' => 'prefers-color-scheme', 'value' => 'dark']
])
->createScreenshotCommand('screenshot.png');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'screenshot',
'options' => [
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'emulateMediaFeatures' => '[{"name":"prefers-color-scheme","value":"dark"}]',
'args' => [],
'type' => 'png',
],
], $command);
});

it('can use pipe', function () {
$command = Browsershot::url('https://example.com')
->usePipe()
Expand Down

0 comments on commit 8b6a057

Please sign in to comment.