Skip to content

Instantly share code, notes, and snippets.

@uchcode
Last active August 23, 2017 03:42
Show Gist options
  • Save uchcode/f96faf175aa0e931ab23415ddad7b592 to your computer and use it in GitHub Desktop.
Save uchcode/f96faf175aa0e931ab23415ddad7b592 to your computer and use it in GitHub Desktop.
HyperShell example
<script type="text/javascript">
function electron_child_process_spawn(cmd = '', arg = []) {
let proc = require('child_process').spawn(cmd, arg, {detached:true})
proc.unref()
proc.stderr.on('data', (data) => {
proc.kill()
window.alert(`electron_child_process_spawn error:\n\n ${data}`)
window.close()
})
window.addEventListener('beforeunload', () => { proc.kill() })
return proc
}
</script>
<script type="text/javascript">
function childNodeJS(scpt) {
return electron_child_process_spawn('/usr/local/bin/node', ['-e', scpt])
}
</script>
<title>App Server</title>
<style>
#browser {
position: fixed;
top:0; right:0;
bottom:0; left:0;
}
</style>
<body>
<webview id="browser"></webview>
</body>
<script id="server" type="text/plain">
require('http')
.createServer()
.on('request', (req, res) => {
res.writeHead(200,{'content-Type':'text/html'})
res.write('<a href="https://www.google.com/">google</a>')
res.end()
})
.listen(3030,'127.0.0.1')
console.log('Server running at http://127.0.0.1:3030/')
</script>
<script type="text/javascript">
window.state('io.github.uchcode.electron-shell.example.server')
let script = query('#server').innerText
let proc = childNodeJS(script)
proc.stdout.on('data', () => {
query('#browser').setAttribute('src','http://127.0.0.1:3030/')
})
// require('electron').remote.getCurrentWindow().toggleDevTools()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment