案发现场
背景
electron 相信大家并不陌生,很多应用都是通过 electron内嵌了一个盒子,生成客户端,这导致electron 和html页面通信极其困难,并且ipcRenderer,ipcMain 和webContents.send并不好用。这时候,骚操作出现了!
解决办法
'use strict';
const APP = {
version: "1.0.0",
author: "uffy@qq.com",
root: __dirname,
debug: true
};
const electron = require("electron");
const {BrowserWindow, ipcMain} = electron;
electron.app.on("ready", () => {
ipcMain.on("app.quit", function (event) {
electron.app.quit();
});
let win = new BrowserWindow();
win.loadURL("http://www.baidu.com");
win.webContents.executeJavaScript(`
document.getElementById("lg").addEventListener("click", function(){
require("electron").ipcRenderer.send("app.quit");
});
`)
});
总结
又学到了