Electron 电子快速启动、ipcMain、CSP块和#x27;评估';在Javascript中

Electron 电子快速启动、ipcMain、CSP块和#x27;评估';在Javascript中,electron,ipc,content-security-policy,Electron,Ipc,Content Security Policy,我克隆了electron快速入门演示,并测试了它的主要功能。 按照文档在main.js中添加 const{ipcMain}=require('electron')) ipcMain.on('asynchronous-message',(事件,参数)=>{ console.log(arg)//打印“ping” event.reply('asynchronous-reply','pong') }) ipcMain.on('synchronous-message',(事件,参数)=>{ console

我克隆了electron快速入门演示,并测试了它的主要功能。 按照文档在main.js中添加

const{ipcMain}=require('electron'))
ipcMain.on('asynchronous-message',(事件,参数)=>{
console.log(arg)//打印“ping”
event.reply('asynchronous-reply','pong')
})
ipcMain.on('synchronous-message',(事件,参数)=>{
console.log(arg)//打印“ping”
event.returnValue='pong'
})
在preload.js中

const{ipcRenderer}=require('electron')
console.log(ipcrender.sendSync('synchronous-message','ping')//打印“pong”
ipcRenderer.on('asynchronous-reply',(事件,参数)=>{
console.log(arg)//打印“pong”
})
ipcRenderer.send('asynchronous-message','ping')
我无法添加renderer.js,因为它说

//index.html文件需要此文件,它将
//将在该窗口的渲染器进程中执行。
//此过程中没有可用的Node.js API,因为
//'nodeIntegration'已关闭。使用'preload.js'来
//有选择地启用渲染中所需的功能
//过程。
而且它找不到require(在renderer.js中)

问题是开发工具在这个时候无法在preload.js中获得价值

ipcRenderer.on('asynchronous-reply',(事件,arg)=>{
console.log(arg)//打印“pong”
})
ipcRenderer.send('asynchronous-message','ping')
my node.js终端可以获取“ping”字符串,这是开发工具的问题==>

electron快速入门演示文件包含CSP规则:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">

将其更改为:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-eval'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-eval'">

因为eval表达式在某些地方使用。是的,它“不安全”,但这只是演示。稍后,您可以整理出使用了什么“eval”构造并修复它们

此外,您还可以删除上述两个元标记,以避免在第一步使用内容安全策略时出现问题