OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
606 stars 41 forks source link

How to change a user agent string? #85

Closed ondrejmirtes closed 6 years ago

ondrejmirtes commented 6 years ago

Is there a way to simulate a different user agent string with Chromy? My usecase is that I want the application to recognize when Chromy is doing the request so that it can react to that, via a user agent string.

dotneet commented 6 years ago

Hi @ondrejmirtes,

as far as i know, there are two way to change user agent:

  1. pass the --user-agent flag to chromeFlags options when creating chromy instance.

  2. use custom device. (sorry currently it's undocumented.)

Chromy.addCustomDevice({
    name: 'iPhone6'
    width: 750,
    height: 1334,
    deviceScaleFactor: 2.0,
    pageScaleFactor: 1.0,
    mobile: true,
    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
})
const chromy = new Chromy()
chromy.emulate('iPhone6')
             .goto(url)
             .end()
ondrejmirtes commented 6 years ago

Thank you! πŸ‘

dotneet commented 6 years ago

@ondrejmirtes sorry, I had forgot another way.

  1. use .userAgent()
const chromy = new Chromy()
chromy.userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1')
             .goto(url)
             .end()