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.
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() .
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.
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.
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 }); });
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 }); }
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.
In newer versions, setUserAgent method will be deprecated. Instead, use this;
mainWindow.webContents.userAgent //to get
mainWindow.webContents.userAgent = "Something" //to set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With