adding custom java script to project
🚨 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
https://stackoverflow.com/questions/72035902/how-to-add-custom-java-script-code-to-electronhosthook-in-electronnet-asp-net-mv
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;
}
}
`
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.
-
So Basically your gonna want to create a type script file using the index.ts file as a template
-
once you have a type script file place your custom JS in the onHostRead() part of the script
-
build it
-
this will create the js file and make it look similar to the other example files.
-
create a controller for your custom js like hosthook.cs, this is called the mainfunction in the api demo
-
add front facing logic to your software. ....so still testing idk If i got it right just yet
-
This did not work in visual studio code , I used visual studio 2022
-
dont install the type script nuget package visual studio recommends , its not in the documentation, will break build.
-
sometimes the people capable are too busy to help so dive deep in the code and get good (talking to myself here)
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();
}
}
} `
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;
}
}`
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