Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #784 from gmetais/viewport
Add support for viewport option
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Provides the --viewport option to set any device resolution and pixel density ratio. | ||
* If the user sets a viewport size as well as a device option (--phone, --tablet, ...), | ||
* we assume that he/she wants to overwrite the device values. | ||
* | ||
* Two syntaxes are supported: | ||
* - 1200x800 will set a 1x pixel density ratio | ||
* - 1200x800x2 will set the given ratio (float values such as 1.5 are accepted) | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = function(phantomas) { | ||
|
||
const viewport = phantomas.getParam('viewport'); | ||
|
||
if (viewport === undefined) { | ||
phantomas.log('No viewport option specified, will use the device default viewport'); | ||
return; | ||
} | ||
|
||
phantomas.log('Viewport: %s provided', viewport); | ||
|
||
const viewportValues = viewport.split('x'); | ||
const options = { | ||
width: parseInt(viewportValues[0], 10), | ||
height: parseInt(viewportValues[1], 10), | ||
deviceScaleFactor: parseFloat(viewportValues[2]) || 1 | ||
}; | ||
|
||
phantomas.on('init', async page => { | ||
|
||
// @see https://github.com/puppeteer/puppeteer/blob/v1.11.0/docs/api.md#pagesetviewportviewport | ||
await page.setViewport(options); | ||
phantomas.log('page.setViewport() called with options %j', options); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<body> | ||
<script> | ||
(phantomas => { | ||
phantomas.setMetric('userAgent', navigator.userAgent); | ||
phantomas.setMetric('viewport', window.innerWidth + 'x' + window.innerHeight); | ||
phantomas.setMetric('devicePixelRatio', window.devicePixelRatio); | ||
})(window.__phantomas); | ||
</script> | ||
</body> |