-
Notifications
You must be signed in to change notification settings - Fork 14
/
renderer.js
52 lines (47 loc) · 1.77 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var electron = require('electron')
var domify = require('domify')
module.exports = function () {
electron.ipcRenderer.on('load-url', function (event, url) {
var webview = domify('<webview src="' + url + '" preload="./webview.js"></webview>')
document.body.innerHTML = ''
document.body.appendChild(webview)
webview.addEventListener('will-navigate', function (newUrl) {
electron.ipcRenderer.send('webview-event', 'will-navigate', newUrl)
})
webview.addEventListener('did-finish-load', function () {
electron.ipcRenderer.send('webview-event', 'did-finish-load')
electron.ipcRenderer.send('webview-did-finish-load')
})
webview.addEventListener('did-fail-load', function (error) {
electron.ipcRenderer.send('webview-event', 'did-fail-load', error)
electron.ipcRenderer.send('webview-did-finish-load', error)
})
webview.addEventListener('did-start-loading', function () {
electron.ipcRenderer.send('webview-event', 'did-start-loading')
})
webview.addEventListener('did-stop-loading', function () {
electron.ipcRenderer.send('webview-event', 'did-stop-loading')
})
})
electron.ipcRenderer.on('run', function (event, id, code) {
var webview = document.querySelector('webview')
webview.addEventListener('ipc-message', onIPC)
function onIPC (event) {
electron.ipcRenderer.send.apply(null, [id + '-' + event.channel].concat(event.args))
if (event.channel === 'done-running') {
webview.removeEventListener('ipc-message', onIPC)
}
}
webview.executeJavaScript(ipcWrap(code))
})
}
function ipcWrap (code) {
return `;(function () {
try {
(${code})(ELECTRON_MICROSCOPE_SEND, ELECTRON_MICROSCOPE_DONE)
} catch (err) {
ELECTRON_MICROSCOPE_DONE(err)
}
})();
`
}