adding custom java script to project #787

Closed
opened 2026-01-29 16:48:36 +00:00 by claunia · 6 comments
Owner

Originally created by @danammeansbear on GitHub (Apr 27, 2022).

🚨 The issue tracker is not for questions 🚨

The issue tracker is not for questions. Please ask questions on https://stackoverflow.com/questions/tagged/electron.net or via chat in https://gitter.im/ElectronNET/community.

how to use add custom js code?

var os = require("os");

var bytesAvailable = os.totalmem(); // returns number in bytes
// 1 mb = 1048576 bytes
console.log("Total memory available MB :" + (bytesAvailable/1048576) );

the code above is what i want to get

Originally created by @danammeansbear on GitHub (Apr 27, 2022). 🚨 The issue tracker is not for questions 🚨 The issue tracker is not for questions. Please ask questions on https://stackoverflow.com/questions/tagged/electron.net or via chat in https://gitter.im/ElectronNET/community. how to use add custom js code? ``` var os = require("os"); var bytesAvailable = os.totalmem(); // returns number in bytes // 1 mb = 1048576 bytes console.log("Total memory available MB :" + (bytesAvailable/1048576) ); ``` the code above is what i want to get
claunia added the question label 2026-01-29 16:48:36 +00:00
Author
Owner
@danammeansbear commented on GitHub (Apr 27, 2022): https://stackoverflow.com/questions/72035902/how-to-add-custom-java-script-code-to-electronhosthook-in-electronnet-asp-net-mv
Author
Owner

@danammeansbear commented on GitHub (Apr 28, 2022):

okay so i got this far from reading the example code but i am still confused to go from here but if i figure this out before consider this a tutorial

`
// @ts-ignore
import * as Electron from "electron";
import { Connector } from "./connector";
import { IPAddress } from "./ipAddress";

export class HookService extends Connector {
constructor(socket: SocketIO.Socket, public app: Electron.App) {
super(socket, app);
}

onHostReady(): void {
    // execute your own JavaScript Host logic here
    var os = require("os");
    var result = console.log(os.networkInterfaces);
        
    return result;
}

}

`

@danammeansbear commented on GitHub (Apr 28, 2022): okay so i got this far from reading the example code but i am still confused to go from here but if i figure this out before consider this a tutorial ` // @ts-ignore import * as Electron from "electron"; import { Connector } from "./connector"; import { IPAddress } from "./ipAddress"; export class HookService extends Connector { constructor(socket: SocketIO.Socket, public app: Electron.App) { super(socket, app); } onHostReady(): void { // execute your own JavaScript Host logic here var os = require("os"); var result = console.log(os.networkInterfaces); return result; } } `
Author
Owner

@danammeansbear commented on GitHub (Apr 28, 2022):

FYI everyone looking at this, the developer made a decent tutorial for this but lets just go with im the type of developer who is kinda dumb but competent.

  1. So Basically your gonna want to create a type script file using the index.ts file as a template

  2. once you have a type script file place your custom JS in the onHostRead() part of the script

  3. build it

  4. this will create the js file and make it look similar to the other example files.

  5. create a controller for your custom js like hosthook.cs, this is called the mainfunction in the api demo

  6. add front facing logic to your software. ....so still testing idk If i got it right just yet

  7. This did not work in visual studio code , I used visual studio 2022

  8. dont install the type script nuget package visual studio recommends , its not in the documentation, will break build.

  9. sometimes the people capable are too busy to help so dive deep in the code and get good (talking to myself here)

@danammeansbear commented on GitHub (Apr 28, 2022): FYI everyone looking at this, the developer made a decent tutorial for this but lets just go with im the type of developer who is kinda dumb but competent. 1. So Basically your gonna want to create a type script file using the index.ts file as a template 2. once you have a type script file place your custom JS in the onHostRead() part of the script 3. build it 4. this will create the js file and make it look similar to the other example files. 5. create a controller for your custom js like hosthook.cs, this is called the mainfunction in the api demo 6. add front facing logic to your software. ....so still testing idk If i got it right just yet 1. This did not work in visual studio code , I used visual studio 2022 2. dont install the type script nuget package visual studio recommends , its not in the documentation, will break build. 3. sometimes the people capable are too busy to help so dive deep in the code and get good (talking to myself here)
Author
Owner

@danammeansbear commented on GitHub (Apr 28, 2022):

code for IP Controller

`using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Linq;

namespace ElectronNET_API_Demos.Controllers
{
public class IPController : Controller
{
public IActionResult Index()
{
if (HybridSupport.IsElectronActive)
{
Electron.IpcMain.On("start-hoosthook", async (args) =>
{
var mainWindow = Electron.WindowManager.BrowserWindows.First();
var options = new OpenDialogOptions
{
Properties = new OpenDialogProperty[]
{
OpenDialogProperty.openDirectory
}
};
var folderPath = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options);

                var resultFromTypeScript = await Electron.HostHook.CallAsync<string>("get-ip-address", folderPath);
                Electron.IpcMain.Send(mainWindow, "ip-address-found", resultFromTypeScript);
            });
        }

        return View();
    }
}

}
`

@danammeansbear commented on GitHub (Apr 28, 2022): code for IP Controller `using ElectronNET.API; using ElectronNET.API.Entities; using Microsoft.AspNetCore.Mvc; using System.Linq; namespace ElectronNET_API_Demos.Controllers { public class IPController : Controller { public IActionResult Index() { if (HybridSupport.IsElectronActive) { Electron.IpcMain.On("start-hoosthook", async (args) => { var mainWindow = Electron.WindowManager.BrowserWindows.First(); var options = new OpenDialogOptions { Properties = new OpenDialogProperty[] { OpenDialogProperty.openDirectory } }; var folderPath = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options); var resultFromTypeScript = await Electron.HostHook.CallAsync<string>("get-ip-address", folderPath); Electron.IpcMain.Send(mainWindow, "ip-address-found", resultFromTypeScript); }); } return View(); } } } `
Author
Owner

@danammeansbear commented on GitHub (Apr 28, 2022):

code for ipAddress.ts

`// @ts-ignore
import * as Electron from "electron";
import { Connector } from "./connector";
import { IPAddress } from "./ipAddress";

export class HookService extends Connector {
constructor(socket: SocketIO.Socket, public app: Electron.App) {
super(socket, app);
}

onHostReady(): void {
    // execute your own JavaScript Host logic here
    var os = require("os");
    var result = console.log(os.networkInterfaces);
        
    return result;
}

}`

@danammeansbear commented on GitHub (Apr 28, 2022): code for ipAddress.ts `// @ts-ignore import * as Electron from "electron"; import { Connector } from "./connector"; import { IPAddress } from "./ipAddress"; export class HookService extends Connector { constructor(socket: SocketIO.Socket, public app: Electron.App) { super(socket, app); } onHostReady(): void { // execute your own JavaScript Host logic here var os = require("os"); var result = console.log(os.networkInterfaces); return result; } }`
Author
Owner

@danammeansbear commented on GitHub (Apr 28, 2022):

ipAddress.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HookService = void 0;
const connector_1 = require("./connector");
class HookService extends connector_1.Connector {
constructor(socket, app) {
super(socket, app);
this.app = app;
}
onHostReady() {
// execute your own JavaScript Host logic here
var os = require("os");
var result = console.log(os.networkInterfaces);
return result;
}
}
exports.HookService = HookService;
//# sourceMappingURL=ipAddress.js.map

@danammeansbear commented on GitHub (Apr 28, 2022): ipAddress.js "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HookService = void 0; const connector_1 = require("./connector"); class HookService extends connector_1.Connector { constructor(socket, app) { super(socket, app); this.app = app; } onHostReady() { // execute your own JavaScript Host logic here var os = require("os"); var result = console.log(os.networkInterfaces); return result; } } exports.HookService = HookService; //# sourceMappingURL=ipAddress.js.map
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#787