Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set electron UserAgent

I need to set the UserAgent in electron to include the touch 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.

like image 625
zchrykng Avatar asked Feb 27 '16 16:02

zchrykng


People also ask

How do I set an electron user agent?

Just use an option object when loading the URL. For future reference: If you want to extend the user agent instead of overwriting it, you can get the original user agent via win. webContents. getUserAgent() .

What is electron agent?

An electron acceptor is a chemical entity that accepts electrons transferred to it from another compound. It is an oxidizing agent that, by virtue of its accepting electrons, is itself reduced in the process. Electron acceptors are sometimes mistakenly called electron receptors.

Whats is my user agent?

I bet you're thinking, “what is my user agent?” As an intermediary between you and the internet, a User Agent is unique to every person on the internet and holds technical information about your device and software.


3 Answers

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 }); }); 
like image 42
Vadim Macagon Avatar answered Sep 17 '22 02:09

Vadim Macagon


Just use an option object when loading the URL.

function createWindow () {    win = new BrowserWindow({width: 800, height: 600});    win.loadURL('http://www.whoishostingthis.com/tools/user-agent/',      {userAgent: 'Chrome'});     win.on('closed', () => {      win = null    }); } 
like image 60
Eric Dela Cruz Avatar answered Sep 21 '22 02:09

Eric Dela Cruz


Before loading file, you can call BrowserWindowInstance.webContents.setUserAgent()

mainWindow.webContents.setUserAgent(mainWindow.webContents.getUserAgent() + " Custom Value");
mainWindow.loadFile('renderer/index.html');

Works with electron 3.0.4 Previous solutions didn't work for me.


Electron 8.2.5 Update

In newer versions, setUserAgent method will be deprecated. Instead, use this;

mainWindow.webContents.userAgent //to get
mainWindow.webContents.userAgent = "Something" //to set
like image 26
siniradam Avatar answered Sep 21 '22 02:09

siniradam