Created
January 26, 2020 12:09
-
-
Save Richienb/df543d7b01a355d141f0f5737c896f58 to your computer and use it in GitHub Desktop.
Run code in an electron environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<script src="renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "a", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"scripts": { | |
"start": "electron app.js" | |
}, | |
"dependencies": { | |
"electron": "^7.1.10" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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