Skip to content

Commit

Permalink
Merge 998707a into a39ef42
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 27, 2019
2 parents a39ef42 + 998707a commit 29624b2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/browserless/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ module.exports = ({
text: evaluate(page => page.evaluate(() => document.body.innerText), {
disableAnimations: false
}),
devices: goto.devices
getDevice: goto.getDevice
}
}
6 changes: 5 additions & 1 deletion packages/devices/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"dependencies": {
"require-one-of": "~1.0.13"
},
"devDependencies": {
"ava": "latest",
"puppeteer-core": "latest"
},
"engines": {
"node": ">= 8"
},
Expand All @@ -37,7 +41,7 @@
],
"scripts": {
"coverage": "exit 0",
"test": "exit 0"
"test": "ava"
},
"license": "MIT"
}
19 changes: 14 additions & 5 deletions packages/devices/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const customDevices = require('./devices.json')

const getDevice = (devices, deviceName) =>
deviceName && devices.find(device => device.name.toLowerCase() === deviceName.toLowerCase())
const findDevice = (devices, deviceName) =>
devices.find(device => device.name.toLowerCase() === deviceName.toLowerCase())

module.exports = ({
puppeteerDevices = require('require-one-of')([
Expand All @@ -13,8 +13,17 @@ module.exports = ({
])
} = {}) => {
const devices = puppeteerDevices.concat(customDevices)
return {
devices,
getDevice: getDevice.bind(null, devices)

return ({ headers = {}, device: deviceId = '', viewport } = {}) => {
const device = findDevice(devices, deviceId)
return device
? {
userAgent: device.userAgent || headers['user-agent'],
viewport: { ...device.viewport, ...viewport }
}
: {
userAgent: headers['user-agent'],
viewport
}
}
}
37 changes: 37 additions & 0 deletions packages/devices/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const test = require('ava')

const getDevice = require('..')()

test('undefined as default', t => {
t.deepEqual(getDevice(), {
userAgent: undefined,
viewport: undefined
})
})

test('support user agent from headers', t => {
t.deepEqual(getDevice({ headers: { 'user-agent': 'googlebot' } }), {
userAgent: 'googlebot',
viewport: undefined
})
})

test('unify user agent from device', t => {
const device = getDevice({ device: 'iPad' })

t.deepEqual(getDevice({ device: 'iPad', headers: { 'user-agent': 'googlebot' } }), {
userAgent: device.userAgent,
viewport: device.viewport
})
})

test('case insensitive device support', t => {
const one = getDevice({ device: 'macbook pro 13' })
const two = getDevice({ device: 'Macbook Pro 13' })

t.true(typeof one === 'object')
t.true(typeof two === 'object')
t.deepEqual(one, two)
})
23 changes: 5 additions & 18 deletions packages/goto/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,11 @@ const doDisableAnimations = () => {
}

module.exports = deviceOpts => {
const { devices, getDevice } = createDevices(deviceOpts)
const getDevice = createDevices(deviceOpts)

const goto = async (
page,
{
url,
device,
media,
adblock = true,
headers = {},
waitFor = 0,
disableAnimations = true,
viewport: fallbackViewport,
...args
}
{ url, media, adblock = true, headers = {}, waitFor = 0, disableAnimations = true, ...args }
) => {
if (adblock) {
await engine.enableBlockingInPage(page)
Expand All @@ -78,17 +68,13 @@ module.exports = deviceOpts => {
await page.setCookie(...cookies)
}

const { userAgent: deviceUserAgent, viewport: deviceViewport } = getDevice(device) || {}

const userAgent = headers['user-agent'] || deviceUserAgent
const { userAgent, viewport } = getDevice({ headers, ...args })

if (userAgent) {
debug({ userAgent })
await page.setUserAgent(userAgent)
}

const viewport = { ...deviceViewport, ...fallbackViewport }

if (!isEmpty(viewport)) {
debug('viewport', viewport)
await page.setViewport(viewport)
Expand All @@ -112,7 +98,8 @@ module.exports = deviceOpts => {
return response
}

goto.devices = devices
goto.getDevice = getDevice

return goto
}

Expand Down

0 comments on commit 29624b2

Please sign in to comment.