default_app.ts 2.9 KB

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