我在博客文章后回购后花了大约一个小时阅读要点,但似乎无法弄清楚如何做到这一点.
我有一个BrowserWindow实例加载一个URL(我控制),nodeIntegration:false.
从主进程,我想与渲染的URL进行通信.我对preload脚本,BrowserWindow.send和executeJavascript范例感到困惑.
我想发送的数据非常大(例如,文件上传量在50kb到10mb之间).
最好的方法是什么?您可能知道的任何示例/教程都会有所帮助.谢谢!
解决方法:
// main.js
const path = require('path')
const electron = require('electron')
const { app, BrowserWindow, ipcMain } = electron
const window = new BrowserWindow({
minWidth: 1200,
minHeight: 700,
autoHideMenuBar: true,
resizable: true,
show: false,
scrollBounce: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
}
})
window.webContents.loadURL('https://xxx.xxx.com') // load your web page
ipcMain.on('ping', (event, msg) => {
console.log(msg) // msg from web page
window.webContents.send('pong', 'hi') // send to web page
})
// preload.js
const { ipcRenderer } = require('electron');
function init() {
// add global variables to your web page
window.isElectron = true
window.ipcRenderer = ipcRenderer
}
init();
// your web page
<script>
if (window.isElectron) {
window.ipcRenderrer.send('ping', 'hello')
window.ipcRenderrer.on('pong', (event, msg) => console.log(msg) )
}
</script>
原文地址:https://codeday.me/bug/20190823/1697508.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。