default_app.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { shell } from 'electron/common';
  2. import { app, dialog, BrowserWindow, ipcMain } from 'electron/main';
  3. import * as path from 'node:path';
  4. import * as url from 'node:url';
  5. let mainWindow: BrowserWindow | null = null;
  6. // Quit when all windows are closed.
  7. app.on('window-all-closed', () => {
  8. app.quit();
  9. });
  10. function decorateURL (url: string) {
  11. // safely add `?utm_source=default_app
  12. const parsedUrl = new URL(url);
  13. parsedUrl.searchParams.append('utm_source', 'default_app');
  14. return parsedUrl.toString();
  15. }
  16. // Find the shortest path to the electron binary
  17. const absoluteElectronPath = process.execPath;
  18. const relativeElectronPath = path.relative(process.cwd(), absoluteElectronPath);
  19. const electronPath = absoluteElectronPath.length < relativeElectronPath.length
  20. ? absoluteElectronPath
  21. : relativeElectronPath;
  22. const indexPath = path.resolve(app.getAppPath(), 'index.html');
  23. function isTrustedSender (webContents: Electron.WebContents) {
  24. if (webContents !== (mainWindow && mainWindow.webContents)) {
  25. return false;
  26. }
  27. try {
  28. return url.fileURLToPath(webContents.getURL()) === indexPath;
  29. } catch {
  30. return false;
  31. }
  32. }
  33. ipcMain.handle('bootstrap', (event) => {
  34. return isTrustedSender(event.sender) ? electronPath : null;
  35. });
  36. async function createWindow (backgroundColor?: string) {
  37. await app.whenReady();
  38. const options: Electron.BrowserWindowConstructorOptions = {
  39. width: 960,
  40. height: 620,
  41. autoHideMenuBar: true,
  42. backgroundColor,
  43. webPreferences: {
  44. preload: url.fileURLToPath(new URL('preload.js', import.meta.url)),
  45. contextIsolation: true,
  46. sandbox: true,
  47. nodeIntegration: false
  48. },
  49. useContentSize: true,
  50. show: false
  51. };
  52. if (process.platform === 'linux') {
  53. options.icon = url.fileURLToPath(new URL('icon.png', import.meta.url));
  54. }
  55. mainWindow = new BrowserWindow(options);
  56. mainWindow.on('ready-to-show', () => mainWindow!.show());
  57. mainWindow.webContents.setWindowOpenHandler(details => {
  58. shell.openExternal(decorateURL(details.url));
  59. return { action: 'deny' };
  60. });
  61. mainWindow.webContents.session.setPermissionRequestHandler((webContents, permission, done) => {
  62. const parsedUrl = new URL(webContents.getURL());
  63. const options: Electron.MessageBoxOptions = {
  64. title: 'Permission Request',
  65. message: `Allow '${parsedUrl.origin}' to access '${permission}'?`,
  66. buttons: ['OK', 'Cancel'],
  67. cancelId: 1
  68. };
  69. dialog.showMessageBox(mainWindow!, options).then(({ response }) => {
  70. done(response === 0);
  71. });
  72. });
  73. return mainWindow;
  74. }
  75. export const loadURL = async (appUrl: string) => {
  76. mainWindow = await createWindow();
  77. mainWindow.loadURL(appUrl);
  78. mainWindow.focus();
  79. };
  80. export const loadFile = async (appPath: string) => {
  81. mainWindow = await createWindow(appPath === 'index.html' ? '#2f3241' : undefined);
  82. mainWindow.loadFile(appPath);
  83. mainWindow.focus();
  84. };