From d32bb1e561260ca042e8ecd7889168e4f1514cc1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:08:51 +0000 Subject: [PATCH] Update wiki ed6d24dbb171e7291c9227bb7a2b0f04b2234201 --- Custom_main.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ _Sidebar.md | 1 + 2 files changed, 74 insertions(+) create mode 100644 Custom_main.md diff --git a/Custom_main.md b/Custom_main.md new file mode 100644 index 0000000..f007b6c --- /dev/null +++ b/Custom_main.md @@ -0,0 +1,73 @@ + +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? + +- 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 + +## 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/_Sidebar.md b/_Sidebar.md index 705af5b..558297a 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -23,6 +23,7 @@ - [Startup-Methods](Startup-Methods) - [Debugging](Debugging) - [Package Building](Package-Building) +- [Adding a `custom_main.js`](Custom_main)