9951 explained code solutions for 126 technologies


chrome-headlessHow to change user agent using Chrome Remote Interface


Execute this javascript file using nodejs:

const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');

chromeLauncher.launch({ port: 9222, chromeFlags: [ '--headless' ] }).then(function(chrome) {
  CDP(async (client) => {
    const {Network, Page} = client;
    Network.setUserAgentOverride({'userAgent': 'MyBot2.0'});
    await Page.enable();
    await Page.navigate({url: 'https://github.com'});
    await Page.loadEventFired();
    
    // Do something when page loads
    
    await client.close();
    chrome.kill();
  });
});ctrl + c
require('chrome-launcher')

Chrome-Launcher library to start/stop Chrome browser programmatically

require('chrome-remote-interface')

Chrome-Remote-Interface library to operate

chromeLauncher.launch

launch Chrome with specified params

Network.setUserAgentOverride

allows setting custom user agent

MyBot2.0

user agent value to use for page request

Page.navigate

go to specified URL