zchrykng zchrykng - 1 year ago 541
Javascript Question

How to set electron UserAgent

I need to set the UserAgent in electron to include the

flag since I am writing the application for touch screens and it doesn't seem to auto detect that it is running on a touch screen.

Any help would be nice, I already tried setting it in the BrowserWindow.loadURL options param.

Answer Source

You can set the User-Agent header in the main process using onBeforeSendHeaders:

import { session } from 'electron';

session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
  details.requestHeaders['User-Agent'] = 'SuperDuperAgent';
  callback({ cancel: false, requestHeaders: details.requestHeaders });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download