How to add custom Java script code to ElectronHostHook in electronnet asp.net mvc

315 Views Asked by At

I am trying to add this code snippet to the wed apis demo project but I tried and failed and there isnt much documentation on how to do it.

var os = require("os");

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

it needs to have a type script file and a javascript file according to the implamentation with the create excel.js demo but im not sure how to go about that process.

1

There are 1 best solutions below

0
On

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)

ipController.cs

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();
    }
}
}

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