-
-
Notifications
You must be signed in to change notification settings - Fork 746
Add custom_main.js guide to documentation #964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FlorianRappl
merged 4 commits into
ElectronNET:main
from
niteshsinghal85:custom_main_example
Dec 7, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d95b41f
Add custom_main.js guide to documentation
niteshsinghal85 42fecbd
Update docs/_Sidebar.md
niteshsinghal85 2389ae3
Update Custom_main.md to clarify usage of custom_main.js
niteshsinghal85 bbc7c79
Merge branch 'custom_main_example' of https://github.com/niteshsingha…
niteshsinghal85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| <ItemGroup> | ||
| <None Update="electron\custom_main.js"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| <TargetPath>.electron\custom_main.js</TargetPath> | ||
| </None> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ### 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.