-
-
Notifications
You must be signed in to change notification settings - Fork 741
Custom_main
This guide explains how to include and use a custom_main.js file in your Electron.NET application for advanced Electron/Node.js customization.
- 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
Place your custom logic in electron/custom_main.js:
module.exports.onStartup = function(host) {
// Example: Register a global shortcut for opening dev tools
const { app, globalShortcut, BrowserWindow } = require('electron');
app.on('ready', () => {
const ret = globalShortcut.register('Control+Shift+I', () => {
BrowserWindow.getAllWindows().forEach(win => win.webContents.openDevTools());
console.log('Ctrl+Shift+I is pressed: DevTools opened!');
});
});
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});
return true;
};Add this to your .csproj file:
<ItemGroup>
<None Update="electron\custom_main.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>.electron\custom_main.js</TargetPath>
</None>
</ItemGroup>Use the standard build/run commands:
dotnet build
dotnet runElectron.NET will automatically load and execute your custom_main.js before initializing the .NET backend.
Use environment variables to control features:
const env = process.env.ASPNETCORE_ENVIRONMENT || 'Production';
if (env === 'Development') { /* enable dev features */ }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:
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;
};-
custom_main.jsmust use CommonJS syntax (module.exports.onStartup = ...). - Place the file in your source directory and copy it to
.electronusing.csproj. - Electron.NET will abort startup if
onStartupreturnsfalse. -
onStartupruns 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
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.
-
Configuration
-
Startup-Methods
-
Debugging
-
Package Building
-
Secure Communication
-
Adding a
custom_main.js
-
API Overview
-
Electron.App
-
Electron.Dialog
-
Electron.Menu
-
Electron.WindowManager
-
Electron.Shell
-
Electron.Screen
-
Electron.Notification
-
Electron.Tray
-
Electron.Clipboard
-
Electron.Dock
-
Electron.HostHook
-
Electron.IpcMain
-
Electron.GlobalShortcut
-
Electron.AutoUpdater
-
Electron.NativeTheme
-
Electron.PowerMonitor
custom_main.js