From d95b41fbaee24a4c937b4990251e380572daa209 Mon Sep 17 00:00:00 2001 From: Nitesh Singhal Date: Sun, 7 Dec 2025 15:00:28 +0530 Subject: [PATCH 1/3] Add custom_main.js guide to documentation --- docs/Using/Custom_main.md | 74 +++++++++++++++++++++++++++++++++++++++ docs/_Sidebar.md | 1 + 2 files changed, 75 insertions(+) create mode 100644 docs/Using/Custom_main.md diff --git a/docs/Using/Custom_main.md b/docs/Using/Custom_main.md new file mode 100644 index 00000000..2bf90977 --- /dev/null +++ b/docs/Using/Custom_main.md @@ -0,0 +1,74 @@ +# Using custom_main.js + +This guide explains how to include and use a `custom_main.js` file in your Electron.NET application for advanced Electron/Node.js customization. + +## Why use custom_main.js? + +- Add custom Electron features (global shortcuts, tray icons, menus, etc.) +- Integrate Node.js modules (e.g., telemetry, OS APIs) +- Control startup logic (abort, environment checks) +- Set up IPC messaging or preload scripts + +## Step-by-Step Process + +### 1. Create the custom_main.js file + +Place your custom logic in `electron/custom_main.js`: + +```javascript +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; +}; +``` + +### 2. Configure your .csproj to copy custom_main.js to output + +Add this to your `.csproj` file: + +```xml + + + PreserveNewest + .electron\custom_main.js + + +``` + +### 3. Build and run your app + +Use the standard build/run commands: + +```powershell +dotnet build +dotnet run +``` + +Electron.NET will automatically load and execute your `custom_main.js` before initializing the .NET backend. + +## Advanced Usage + +Use environment variables to control features: + + ```javascript + const env = process.env.ASPNETCORE_ENVIRONMENT || 'Production'; + if (env === 'Development') { /* enable dev features */ } + ``` + +## 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`. + +### Complete example is available here [ElectronNetSampleApp](https://github.com/niteshsinghal85/ElectronNetSampleApp) \ No newline at end of file diff --git a/docs/_Sidebar.md b/docs/_Sidebar.md index 4f1db543..410ef4ff 100644 --- a/docs/_Sidebar.md +++ b/docs/_Sidebar.md @@ -23,6 +23,7 @@ - [Startup-Methods](Using/Startup-Methods.md) - [Debugging](Using/Debugging.md) - [Package Building](Using/Package-Building.md) +- [Custom Main](Using/Custom_main.md) # API Reference From 42fecbdc9858b0b2c404e8080d2777a44c069264 Mon Sep 17 00:00:00 2001 From: Nitesh Singhal <40641126+niteshsinghal85@users.noreply.github.com> Date: Sun, 7 Dec 2025 18:10:16 +0530 Subject: [PATCH 2/3] Update docs/_Sidebar.md Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com> --- docs/_Sidebar.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_Sidebar.md b/docs/_Sidebar.md index 410ef4ff..b76d5afd 100644 --- a/docs/_Sidebar.md +++ b/docs/_Sidebar.md @@ -23,7 +23,7 @@ - [Startup-Methods](Using/Startup-Methods.md) - [Debugging](Using/Debugging.md) - [Package Building](Using/Package-Building.md) -- [Custom Main](Using/Custom_main.md) +- [Adding a `custom_main.js`](Using/Custom_main.md) # API Reference From 2389ae32bd2f0497e62d70d3a0264c50efe4c1d2 Mon Sep 17 00:00:00 2001 From: Nitesh Singhal Date: Sun, 7 Dec 2025 18:16:48 +0530 Subject: [PATCH 3/3] Update Custom_main.md to clarify usage of custom_main.js --- docs/Using/Custom_main.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Using/Custom_main.md b/docs/Using/Custom_main.md index 2bf90977..dac300fb 100644 --- a/docs/Using/Custom_main.md +++ b/docs/Using/Custom_main.md @@ -4,7 +4,7 @@ This guide explains how to include and use a `custom_main.js` file in your Elect ## Why use custom_main.js? -- Add custom Electron features (global shortcuts, tray icons, menus, etc.) +- 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) - Set up IPC messaging or preload scripts