main.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { app, BrowserWindow } = require('electron')
  2. // const path = require('path');
  3. // const app_path = path.resolve(__dirname, './build/index.html')
  4. // console.log(app_path)
  5. let win;
  6. function createWindow () {
  7. // 创建浏览器窗口。
  8. win = new BrowserWindow({
  9. width: 1400,
  10. height: 800,
  11. webPreferences: {
  12. nodeIntegration: true,
  13. // webSecurity: false,
  14. }
  15. })
  16. // 加载index.html文件
  17. win.loadFile('./build/index.html');
  18. // 打开开发者工具
  19. // win.webContents.openDevTools()
  20. // 当 window 被关闭,这个事件会被触发。
  21. win.on('closed', () => {
  22. // 取消引用 window 对象,如果你的应用支持多窗口的话,
  23. // 通常会把多个 window 对象存放在一个数组里面,
  24. // 与此同时,你应该删除相应的元素。
  25. win = null
  26. })
  27. }
  28. // Electron 会在初始化后并准备
  29. // 创建浏览器窗口时,调用这个函数。
  30. // 部分 API 在 ready 事件触发后才能使用。
  31. app.on('ready', createWindow)
  32. // 当全部窗口关闭时退出。
  33. app.on('window-all-closed', () => {
  34. // 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
  35. // 否则绝大部分应用及其菜单栏会保持激活。
  36. if (process.platform !== 'darwin') {
  37. app.quit()
  38. }
  39. })
  40. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  41. // On certificate error we disable default behaviour (stop loading the page)
  42. // and we then say "it is all fine - true" to the callback
  43. event.preventDefault();
  44. callback(true);
  45. });
  46. app.on('activate', () => {
  47. // 在macOS上,当单击dock图标并且没有其他窗口打开时,
  48. // 通常在应用程序中重新创建一个窗口。
  49. if (win === null) {
  50. createWindow()
  51. }
  52. })