From d5da1dbc9b9d60ac983730b7f8ce8a53d5219d59 Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Fri, 4 Sep 2026 15:05:55 +0200 Subject: [PATCH] Fixed #1029 --- Changelog.md | 1 + docs/Using/Custom_main.md | 20 ++++++++++++++++ src/ElectronNET.Host/main.js | 45 ++++++++++++++++++------------------ 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Changelog.md b/Changelog.md index 6f97b88..11e3fa2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,6 +11,7 @@ - Fixed resolution of unrelated target (#1099) @epsnm - Added target framework customization (#1095) @epsnm - Added configurable Electron root directory for custom packaging layouts (#1106) @DYH1319 +- Added ability for `custom_main.js` to modify command line switches (#1029) @AeonSake - Added `ElectronExecutableName` to separate the product name from the executable name (#1003) @AeonSake - Added build extensibility properties `ElectronSkipExecCommands` and `ElectronIntermediatePublishDir` (#1106) @DYH1319 diff --git a/docs/Using/Custom_main.md b/docs/Using/Custom_main.md index dac300f..7a3f1ec 100644 --- a/docs/Using/Custom_main.md +++ b/docs/Using/Custom_main.md @@ -7,6 +7,7 @@ This guide explains how to include and use a `custom_main.js` file in your Elect - Register custom protocol handlers (e.g., `myapp://`) — protocols must be registered before the app is fully initialized - Integrate Node.js modules (e.g., telemetry, OS APIs) - Control startup logic (abort, environment checks) +- Modify Chromium / Electron.NET command line switches before they are evaluated - Set up IPC messaging or preload scripts ## Step-by-Step Process @@ -65,10 +66,29 @@ Use environment variables to control features: if (env === 'Development') { /* enable dev features */ } ``` +### Modifying Command Line Switches + +`onStartup` is invoked before Electron.NET reads any of its own switches (`manifest`, `unpackedelectron`, `unpackeddotnet`, `dotnetpacked`, `electronforcedport`, `electronurl`), so the hook can append or override them: + +```javascript +module.exports.onStartup = function (host) { + const { app } = require('electron'); + + // Force a fixed socket bridge port + app.commandLine.appendSwitch('electronforcedport', '8000'); + + // Chromium switches, e.g. to disable the GPU sandbox + app.commandLine.appendSwitch('disable-gpu-sandbox'); + + return true; +}; +``` + ## Notes - `custom_main.js` must use CommonJS syntax (`module.exports.onStartup = ...`). - Place the file in your source directory and copy it to `.electron` using `.csproj`. - Electron.NET will abort startup if `onStartup` returns `false`. +- `onStartup` runs before the Electron.NET command line switches are evaluated, so switches set there are picked up by the host. ### Complete example is available here [ElectronNetSampleApp](https://github.com/niteshsinghal85/ElectronNetSampleApp) \ No newline at end of file diff --git a/src/ElectronNET.Host/main.js b/src/ElectronNET.Host/main.js index d610da8..c182ef3 100644 --- a/src/ElectronNET.Host/main.js +++ b/src/ElectronNET.Host/main.js @@ -29,32 +29,11 @@ let electronforcedport; let electronUrl; let authToken = randomUUID().split('-').join(''); -if (app.commandLine.hasSwitch('manifest')) { - manifestJsonFileName = app.commandLine.getSwitchValue('manifest'); -} - -if (app.commandLine.hasSwitch('unpackedelectron')) { - unpackedelectron = true; -} -else if (app.commandLine.hasSwitch('unpackeddotnet')) { - unpackeddotnet = true; -} -else if (app.commandLine.hasSwitch('dotnetpacked')) { - dotnetpacked = true; -} - -if (app.commandLine.hasSwitch('electronforcedport')) { - electronforcedport = +app.commandLine.getSwitchValue('electronforcedport'); -} - // Store in global for access by browser windows global.authToken = authToken; -if (app.commandLine.hasSwitch('electronurl')) { - electronUrl = app.commandLine.getSwitchValue('electronurl'); -} - // Custom startup hook: look for custom_main.js and invoke its onStartup(host) if present. +// Runs before any command line switch is evaluated so the hook can still modify them. // If the hook returns false, abort Electron startup. try { const fs = require('fs'); @@ -77,6 +56,28 @@ try { console.error('Error while executing custom_main.js:', err); } +if (app.commandLine.hasSwitch('manifest')) { + manifestJsonFileName = app.commandLine.getSwitchValue('manifest'); +} + +if (app.commandLine.hasSwitch('unpackedelectron')) { + unpackedelectron = true; +} +else if (app.commandLine.hasSwitch('unpackeddotnet')) { + unpackeddotnet = true; +} +else if (app.commandLine.hasSwitch('dotnetpacked')) { + dotnetpacked = true; +} + +if (app.commandLine.hasSwitch('electronforcedport')) { + electronforcedport = +app.commandLine.getSwitchValue('electronforcedport'); +} + +if (app.commandLine.hasSwitch('electronurl')) { + electronUrl = app.commandLine.getSwitchValue('electronurl'); +} + const currentPath = __dirname; let currentBinPath = path.join(currentPath.replace('app.asar', ''), 'bin'); let manifestJsonFilePath = path.join(currentPath, manifestJsonFileName);