main.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { app, BrowserWindow, shell } = require('electron');
  2. const path = require('path');
  3. const url = require('url');
  4. let win = null;
  5. // 创建窗口
  6. const createWindow = () => {
  7. win = new BrowserWindow({
  8. // width: 1200,
  9. // height: 800,
  10. show: false, // 是否显示窗口
  11. autoHideMenuBar: true, // 隐藏菜单栏
  12. webPreferences: {
  13. nodeIntegration: true, // 是否集成 Node.js
  14. // enableRemoteModule: true, // 是否启用 remote 模块
  15. webSecurity: false, // 是否禁用同源策略
  16. preload: path.join(__dirname, 'preload.js'), // 预加载脚本
  17. },
  18. });
  19. win.loadURL(
  20. url.format({
  21. pathname: path.join(__dirname, './dist', 'index.html'),
  22. protocol: 'file:',
  23. slashes: true, // true: file://, false: file:
  24. hash: '/login', // /image_change 图片切换页
  25. }),
  26. );
  27. win.once('ready-to-show', () => {
  28. win.maximize();
  29. win.show();
  30. });
  31. // 拦截当前窗口的导航,防止外部链接在应用内打开
  32. win.webContents.on('will-navigate', (event, url) => {
  33. if (url.startsWith('http://') || url.startsWith('https://')) {
  34. event.preventDefault();
  35. shell.openExternal(url);
  36. }
  37. });
  38. };
  39. // 当 Electron 完成初始化并准备创建浏览器窗口时调用此方法
  40. app.whenReady().then(() => {
  41. createWindow();
  42. app.on('activate', () => {
  43. if (BrowserWindow.getAllWindows().length === 0) {
  44. createWindow();
  45. }
  46. });
  47. });
  48. // 当所有窗口都已关闭时退出
  49. app.on('window-all-closed', () => {
  50. if (process.platform !== 'darwin') {
  51. app.quit();
  52. }
  53. });
  54. app.on('activate', () => {
  55. if (BrowserWindow.getAllWindows().length === 0) {
  56. createWindow();
  57. }
  58. });