Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by StarOrbiter711

How can I programmatically disable Chromium’s 'Use browser default User-Agent' in Electron?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I am developing a custom Electron browser that dynamically modifies the User-Agent for specific domains. However, when I remove the Electron/* portion from the User-Agent for a demo gaming website, the site fails to load and throws JavaScript errors.

I intercept all outgoing requests in my Electron main.js using the following code:

JAVASCRIPT
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => { const originalUserAgent = details.requestHeaders['User-Agent']; const newUserAgent = getUserAgentForURL(originalUserAgent, details.url); console.log('========================'); console.log('Intercepted Request:'); console.log('URL:', details.url); console.log('Original User-Agent:', originalUserAgent); console.log('Modified User-Agent:', newUserAgent); console.log('========================'); details.requestHeaders['User-Agent'] = newUserAgent; callback({ cancel: false, requestHeaders: details.requestHeaders }); });

My user-agent.js module modifies the User-Agent as shown below, removing Electron-specific components while retaining Chrome info:

JAVASCRIPT
const { app } = require('electron'); const REMOVE_ELECTRON_COMPONENTS = [ / Electron\/([^ \s]+)/g, // Removes Electron ` ${app.name}/${app.getVersion()}`, // Removes app-specific info ]; function getUserAgentForURL(userAgent, url) { if (typeof userAgent !== 'string') { console.error(`Error: Expected userAgent to be a string, but got ${typeof userAgent}`); return userAgent || 'Mozilla/5.0'; } let componentsToRemove = [...REMOVE_ELECTRON_COMPONENTS]; // Remove Electron-specific components but keep Chrome componentsToRemove.forEach((x) => { if (userAgent) { userAgent = userAgent.replace(x, ''); } }); return userAgent; } module.exports = { getUserAgentForURL };

Debugging Output:

When trying to access the demo game website, the logs show:

BASH
======================== Intercepted Request: URL: https://[removed-domain]/game-assets/other_resources.json Original User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) my-electron-app/1.0.0 Chrome/132.0.6834.159 Electron/34.0.2 Safari/537.36 Modified User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.6834.159 Safari/537.36 ========================

However, the website does not load and throws these JavaScript errors:

BASH
[96308:0201/114327.613:INFO:CONSOLE(5657)] "Uncaught TypeError: Cannot read properties of undefined (reading 'split')", source: https://[removed-domain]/build.js (5657) [96308:0201/114327.616:INFO:CONSOLE(18588)] "Uncaught TypeError: Cannot read properties of null (reading 'length')", source: https://[removed-domain]/logo_info.js (18588)

When I do not modify the User-Agent, the website functions correctly. For example, a successful request logs the default User-Agent set by Electron:

BASH
======================== Intercepted Request: URL: https://[removed-domain]/gs2c/ge/v4/game Method: POST Headers: { "host": "[removed-domain]", "connection": "keep-alive", "content-length": "145", "accept": "*/*", "accept-encoding": "gzip, deflate, br, zstd", "accept-language": "en-US", "content-type": "application/x-www-form-urlencoded", "origin": "https://[removed-domain]", "referer": "https://[removed-domain]/gs2c/html5Game.do?extGame=1&symbol..", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-origin", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) my-electron-app/1.0.0 Chrome/132.0.6834.159 Electron/34.0.2 Safari/537.36", "sec-ch-ua": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"" } ========================

UPDATE

I tested using the "User-Agent Switcher for Chrome" extension but the game still failed to load, indicating that Chrome ignored the changes. I later discovered the "Use browser default User-Agent" setting in DevTools → Network Conditions:

  • Opened DevTools (F12) → Network Tab
  • Clicked the Wi-Fi icon (bottom left)
  • Found "Use browser default User-Agent" enabled by default

This setting causes conflicts: outgoing requests use the modified User-Agent, but the browser’s internal JavaScript runtime still uses the default, leading to errors. Unchecking this setting and manually entering the modified User-Agent allowed the game to load correctly.

My question is: How can I automatically disable the "Use browser default User-Agent" setting in Electron (via a command-line flag or API) so that the browser's internal behavior matches the modified User-Agent?

3

Comments 0

Answers 2

Views 59

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Short Answer:
There is currently no command‑line flag or public API in Electron or Chromium to disable the “Use browser default User-Agent” setting automatically.

Explanation:
Chromium’s “Use browser default User-Agent” setting in DevTools forces internal components (and scripts running in the page) to use the default UA even if you intercept and modify outgoing network requests. This behavior isn’t controlled by any Electron API or command‑line flag; it’s built into Chromium’s design to ensure consistency between what the browser “thinks” its UA is and what it sends in network requests. As a result, even if you change the UA header via onBeforeSendHeaders or use a custom BrowserWindow constructor userAgent, Chrome’s internal environment may still continue to use the default UA if that checkbox is enabled.

Workarounds:

  • Manual Change: In your testing environment, you can manually disable “Use browser default User-Agent” in DevTools → Network Conditions, as you already discovered.
  • User Guidance: If your custom browser is intended for end users, you might need to document that the user agent override may not work correctly if that setting is enabled in DevTools.
  • Avoid DevTools Overrides: You could potentially hide or restrict access to DevTools in your custom browser to prevent users from inadvertently enabling the “default user agent” behavior, though this might not be acceptable in every use case.

At this time, unless Chromium exposes an internal flag (which has not been documented or made available via Electron), you’ll have to work within these constraints.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by AstralNavigator205 1 month ago

0

I found that Chromium was forcing the default User-Agent internally, causing inconsistencies between requests and the browser’s JavaScript runtime. The fix was setting app.userAgentFallback, which I came across in Electron’s documentation. It was a hit-and-try approach, but it worked.

JAVASCRIPT
app.whenReady().then(() => { app.userAgentFallback = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36"; });

Run code snippetHide resultsExpand snippet

app.userAgentFallback

A string which is the user agent string Electron will use as a global fallback.

This is the user agent that will be used when no user agent is set at the webContents or session level. It is useful for ensuring that your entire app has the same user agent. Set to a custom value as early as possible in your app's initialization to ensure that your overridden value is used.

Source : Electron Documentation

No comments yet.

Discussion

No comments yet.