25
loading...
This website collects cookies to deliver better user experience
child_process.execSync
to run a command and capture its output.Buffer
into String
- this isn't done automatically, as the output could be some binary like an image, not a valid UTF-8 String. And then we'll trim extra newline.let child_process = require("child_process")
let runCommand = (command) => {
return child_process.execSync(command).toString().trim()
}
let sysInfo = {
os: runCommand("uname -s"),
cpu: runCommand("uname -m"),
hostname: runCommand("hostname -s"),
ip: runCommand("ipconfig getifaddr en0"),
}
Object
into a query string, so we'll need to roll our own!let toQueryString = (obj) => {
let q = []
for (let key in obj) {
q.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
}
return q.join("&")
}
loadFile
function before, but that doesn't have any easy way of passing an query string. But we can do a little trick, and use loadURL
with file:
protocol instead.let { app, BrowserWindow } = require("electron")
function createWindow() {
let win = new BrowserWindow({})
win.maximize()
win.loadURL(`file:${__dirname}/index.html?${toQueryString(sysInfo)}`)
}
app.on("ready", createWindow)
app.on("window-all-closed", () => {
app.quit()
})
let data = new URLSearchParams(document.location.search)
let info = document.querySelector("#info")
for (const [key, value] of data) {
let p = document.createElement("p")
p.append(`${key} = ${value}`)
info.append(p)
}
<!DOCTYPE html>
<html>
<body>
<h1>System information!</h1>
<div id="info"></div>
<script src="app.js"></script>
</body>
</html>