electron工程的快速搭建
git clone https://github.com/electron/electron-quick-start
下载工程,然后在里面改改
electron 如何设置权限
在main.js时面:
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration : true,
contextIsolation: false ,
preload: path.join(__dirname, 'preload.js')
}
})
在index.html里面:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; ;script-src * 'unsafe-inline'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src * 'unsafe-inline'">
electron 如何实现 nodejs <-->webJs相互调用
方式1:
main.js中:
const ipcMain = require('electron').ipcMain
ipcMain.on('callJs',( event , args)=>{
console.log(args , event);
var tm = ( new Date()).getTime();
event.sender.send('respCallJs' , tm)
console.log('now:'+tm);
})
index.html中:
const ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.send('callJs' , "dadada")
-----------------------------------------------------------------------------------
ipcRenderer.on('respCallJs' , ( event , data)=>{
console.log( event);
console.log('return:'+data)
})
方式2:
main.js中:
win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL(`file://${__dirname}/index.html`)
win.webContents.on('did-finish-load', () => {
win.webContents.send('ping', 'whoooooooh!')
})
【参考:https://blog.csdn.net/qianfeng_dashuju/article/details/108278653】