main.mjs 914 B

123456789101112131415161718192021222324252627282930313233
  1. import { app, BrowserWindow } from 'electron'
  2. import { fileURLToPath } from 'node:url'
  3. import { dirname, join } from 'node:path';
  4. async function createWindow() {
  5. const mainWindow = new BrowserWindow({
  6. show: false,
  7. webPreferences: {
  8. preload: fileURLToPath(new URL('preload.mjs', import.meta.url)),
  9. sandbox: false,
  10. contextIsolation: false
  11. }
  12. })
  13. await mainWindow.loadFile('index.html')
  14. const importMetaPreload = await mainWindow.webContents.executeJavaScript('window.importMetaPath');
  15. const expected = join(dirname(fileURLToPath(import.meta.url)), 'preload.mjs');
  16. process.exit(importMetaPreload === expected ? 0 : 1);
  17. }
  18. app.whenReady().then(() => {
  19. createWindow()
  20. app.on('activate', function () {
  21. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  22. })
  23. })
  24. app.on('window-all-closed', function () {
  25. if (process.platform !== 'darwin') app.quit()
  26. })