【发布时间】:2016-12-02 16:09:27
【问题描述】:
在电子应用程序中操作 DOM 的最佳方式是什么?
我使用 ipc 和 webcontents 从文档中制作了一些教程,但没有成功
我的应用程序非常简单,我只想像控制台一样使用网络并显示来自多个同步函数(主进程)结果的消息(渲染进程)
我用真实代码更新了问题。
我要放另一个代码,更容易看,更容易测试(我认为),是真实的代码并且可以工作(但不像我想要的那样)
当我启动电子时,只写最后一条消息。 好的,响应真的很快,我可能看不到第一条消息,但要丢弃我也放了一个 setTimeout 和一个大的 for() 循环,以使大写函数需要更长的时间
index.js
const electron = require('electron');
const {app} = electron;
const {BrowserWindow} = electron;
const ipc = require('electron').ipcMain
app.on('ready', () => {
let win = new BrowserWindow({width: 800, height: 500});
win.loadURL('file://' + __dirname + '/index.html');
// Emitted when the window is closed.
win.on('closed', () => {
win = null;
});
// Event that return the arg in uppercase
ipc.on('uppercase', function (event, arg) {
event.returnValue = arg.toUpperCase()
})
});
index.html
<html>
<body>
<div id="konsole">...</div>
<script>
const ipc = require('electron').ipcRenderer
const konsole = document.getElementById('konsole')
// Functions
let reply, message
// First MSG
reply = ipc.sendSync('uppercase', 'Hi, I'm the first msg')
message = `Uppercase message reply: ${reply}`
document.getElementById('konsole').innerHTML = message
// Second MSG
reply = ipc.sendSync('uppercase', 'Hi, I'm the second msg')
message = `Uppercase message reply: ${reply}`
document.getElementById('konsole').innerHTML = message
</script>
</body>
</html>
【问题讨论】:
标签: javascript dom ipc render electron