OnTheRoad
OnTheRoad

Reputation: 613

How to overwrite the User-Agent Client Hints in puppeteer?

I am facing an issue with dealing with the user-agent-client-hints. If you have no idea what User-agent-client-hints are then think of them as some request headers. (See the image below) enter image description here

I tried to overwrite the request headers with the code below but it does not work

await page.setRequestInterception(true)
await page.on('request', (req) => {
    const headers = req.headers()
    headers['sec-ch-ua'] = 'some thing new'
    req.continue({
        headers
    })
})

Upvotes: 0

Views: 4047

Answers (1)

theDavidBarton
theDavidBarton

Reputation: 8841

you can set in page.setUserAgent's second argument navigator.userAgentData and associated Sec-CH-UA* headers.

page.setUserAgent(userAgent[, userAgentMetadata])

  • userAgent <[string]> Specific user agent to use in this page
  • userAgentMetadata <[Object]> Optional user agent data to use in this page. Any values not provided will use the client's default.
    • brands <[Array]<[Object]>> Optional brand information
      • brand <[string]> Browser or client brand name.
      • version <[string]> Browser or client major version.
    • fullVersion <[string]> Optional browser or client full version.
    • platform <[string]> Operating system name.
    • platformVersion <[string]> Operating system version.
    • architecture <[string]> CPU architecture.
    • model <[string]> Device model.
    • mobile <[boolean]> Indicate if this is a mobile device.

Example:

const page = await browser.newPage();
await page.setUserAgent('MyBrowser', {
  architecture: 'My1',
  mobile: false,
  model: 'Mybook',
  platform: 'MyOS',
  platformVersion: '3.1',
});

Docs: https://pptr.dev/api/puppeteer.page.setuseragent (but you will find more info in this Github commit: https://github.com/puppeteer/puppeteer/pull/7378/files)

Upvotes: 4

Related Questions