如何设置电子用户代理

我需要在电子中设置 UserAgent 以包含 touch 标志,因为我正在为触摸屏编写应用程序,并且它似乎没有自动检测到它正在触摸屏上运行.

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.

任何帮助都会很好,我已经尝试在 BrowserWindow.loadURL 选项参数中设置它.

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

推荐答案

可以在主进程中使用onBeforeSendHeaders:

import { session } from 'electron';

session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
  details.requestHeaders['User-Agent'] = 'SuperDuperAgent';
  callback({ cancel: false, requestHeaders: details.requestHeaders });
});

相关文章