Skip to content

Commit 9a19387

Browse files
committed
Add userAgent support for screenshot
1 parent 2336684 commit 9a19387

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,25 @@ It takes an screenshot of the URL.
6161

6262
`opts` provided are passed to [page.screenshot](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions).
6363

64+
It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
65+
66+
If you want to customize where tmpStream live, pass [opts.tmpOptions](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
67+
6468
Additionally, you can setup:
6569

6670
#### device
6771

6872
Providing a valid [deviceDescriptor](https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js) object.
6973

70-
#### viewport
74+
#### userAgent
7175

72-
Providing a valid [page.setViewport](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) object.
76+
It will setup an user Agent using
7377

74-
It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
78+
Providing a valid [page.setUserAgent](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetuseragentuseragent) method.
7579

76-
If you want to customize where tmpStream live, pass [opts.tmpOptions](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
80+
#### viewport
81+
82+
Providing a valid [page.setViewport](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) object.
7783

7884
### .text(url, [options], [cb])
7985

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const createTempFile = require('create-temp-file2')
4+
const isEmpty = require('lodash.isempty')
45
const puppeteer = require('puppeteer')
56

67
const { devices, getDevice } = require('./devices')
@@ -47,8 +48,11 @@ module.exports = launchOpts => {
4748
deviceName
4849
)
4950

50-
await page.setUserAgent(userAgent || deviceUserAgent)
51-
await page.setViewport(Object.assign({}, deviceViewport, viewport))
51+
const pageUserAgent = isEmpty(userAgent) ? deviceUserAgent : userAgent
52+
await page.setUserAgent(pageUserAgent)
53+
54+
const pageViewport = Object.assign({}, deviceViewport, viewport)
55+
await page.setViewport(pageViewport)
5256

5357
await page.goto(url)
5458
await page.screenshot(Object.assign({ path, type }, opts))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
],
3131
"dependencies": {
3232
"create-temp-file2": "~2.0.0",
33+
"lodash.isempty": "~4.4.0",
3334
"puppeteer": "~0.12.0"
3435
},
3536
"devDependencies": {

0 commit comments

Comments
 (0)