Skip to content

Instantly share code, notes, and snippets.

@Richienb
Created January 26, 2020 12:09
Show Gist options
  • Save Richienb/df543d7b01a355d141f0f5737c896f58 to your computer and use it in GitHub Desktop.
Save Richienb/df543d7b01a355d141f0f5737c896f58 to your computer and use it in GitHub Desktop.
Run code in an electron environment
const { app, BrowserWindow, ipcMain: ipc } = require('electron')
let win
function createWindow() {
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
},
show: false
})
win.loadFile('index.html')
win.on('closed', () => {
win = null
})
const input = () => "abc"
ipc.on("ask", (e) => {
e.reply("input", `
(() => {
const fn = ${input.toString()}
return fn()
})()
`)
})
ipc.on("result", (_, res) => {
console.log(res)
app.quit()
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
if (win === null) createWindow()
})
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="renderer.js"></script>
</body>
</html>
{
"name": "a",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "electron app.js"
},
"dependencies": {
"electron": "^7.1.10"
}
}
const { ipcRenderer: ipc } = require('electron')
ipc.on("input", (_, input) => {
ipc.send("result", eval(input))
})
ipc.send('ask')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment