Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
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
–
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 });
–
–
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
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.