Show javascript data inside the rendering part of Electronjs

Asked

Viewed 29 times

0

I want to run a JS file along with Electron and return its result within the rendering part of Electron

Code I want to show inside the Electron in the HTML part:

const si = require('systeminformation');
si.cpu().then(data => console.log(data));

As I think it may be:

const { app, BrowserWindow } = require('electron')

function createWindow () {
  // Cria uma janela de navegação.
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // e carregar o index.html do aplicativo.
  win.loadFile('index.html')
}

function informationSystem(){
    const si = require('systeminformation');
    si.cpu().then(data => console.log(data));   

}

informationSystem()
app.whenReady().then(createWindow)

1 answer

0

That’s what you want to do?

File inside main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {
  // Cria uma janela de navegação.
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })
  win.webContents.openDevTools()
  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

File within index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello</h1>
    <ul class="cpus"></ul>
    <script>
        const cpuInfos = require('os').cpus()
        const list = document.querySelector('ul.cpus')
        cpuInfos.map(cpu => {
            const element = document.createElement('li')
            element.innerHTML = `model: ${cpu.model}, speed: ${cpu.speed}`
            list.appendChild(element)
        })
    </script>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.