2019-03-11 23:13:46 +00:00
|
|
|
import { app, dialog, BrowserWindow, shell, ipcMain } from 'electron'
|
2019-02-06 18:27:20 +00:00
|
|
|
import * as path from 'path'
|
2019-01-10 19:54:34 +00:00
|
|
|
|
2019-02-06 18:27:20 +00:00
|
|
|
let mainWindow: BrowserWindow | null = null
|
2013-07-17 08:21:33 +00:00
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
2016-05-06 23:53:50 +00:00
|
|
|
app.on('window-all-closed', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.quit()
|
|
|
|
})
|
2013-07-17 08:21:33 +00:00
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
function decorateURL (url: string) {
|
|
|
|
// safely add `?utm_source=default_app
|
|
|
|
const parsedUrl = new URL(url)
|
|
|
|
parsedUrl.searchParams.append('utm_source', 'default_app')
|
|
|
|
return parsedUrl.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the shortest path to the electron binary
|
|
|
|
const absoluteElectronPath = process.execPath
|
|
|
|
const relativeElectronPath = path.relative(process.cwd(), absoluteElectronPath)
|
|
|
|
const electronPath = absoluteElectronPath.length < relativeElectronPath.length
|
|
|
|
? absoluteElectronPath
|
|
|
|
: relativeElectronPath
|
|
|
|
|
|
|
|
const indexPath = path.resolve(app.getAppPath(), 'index.html')
|
|
|
|
|
|
|
|
function isTrustedSender (webContents: Electron.WebContents) {
|
|
|
|
if (webContents !== (mainWindow && mainWindow.webContents)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedUrl = new URL(webContents.getURL())
|
2019-05-01 20:00:09 +00:00
|
|
|
const urlPath = process.platform === 'win32'
|
|
|
|
// Strip the prefixed "/" that occurs on windows
|
|
|
|
? path.resolve(parsedUrl.pathname.substr(1))
|
|
|
|
: parsedUrl.pathname
|
|
|
|
return parsedUrl.protocol === 'file:' && urlPath === indexPath
|
2019-03-11 23:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.on('bootstrap', (event) => {
|
|
|
|
try {
|
|
|
|
event.returnValue = isTrustedSender(event.sender) ? electronPath : null
|
|
|
|
} catch {
|
|
|
|
event.returnValue = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async function createWindow () {
|
2018-09-21 05:24:42 +00:00
|
|
|
await app.whenReady()
|
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
const options: Electron.BrowserWindowConstructorOptions = {
|
2018-09-21 05:24:42 +00:00
|
|
|
width: 900,
|
|
|
|
height: 600,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
backgroundColor: '#FFFFFF',
|
|
|
|
webPreferences: {
|
2019-03-11 23:13:46 +00:00
|
|
|
preload: path.resolve(__dirname, 'preload.js'),
|
2018-09-21 05:24:42 +00:00
|
|
|
contextIsolation: true,
|
2019-03-11 23:13:46 +00:00
|
|
|
sandbox: true,
|
|
|
|
enableRemoteModule: false
|
2018-09-21 05:24:42 +00:00
|
|
|
},
|
|
|
|
useContentSize: true,
|
|
|
|
show: false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
options.icon = path.join(__dirname, 'icon.png')
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow = new BrowserWindow(options)
|
2019-02-06 18:27:20 +00:00
|
|
|
mainWindow.on('ready-to-show', () => mainWindow!.show())
|
2018-09-21 05:24:42 +00:00
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
mainWindow.webContents.on('new-window', (event, url) => {
|
|
|
|
event.preventDefault()
|
|
|
|
shell.openExternal(decorateURL(url))
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.webContents.session.setPermissionRequestHandler((webContents, permission, done) => {
|
|
|
|
const parsedUrl = new URL(webContents.getURL())
|
|
|
|
|
|
|
|
const options: Electron.MessageBoxOptions = {
|
|
|
|
title: 'Permission Request',
|
|
|
|
message: `Allow '${parsedUrl.origin}' to access '${permission}'?`,
|
|
|
|
buttons: ['OK', 'Cancel'],
|
|
|
|
cancelId: 1
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:54:53 +00:00
|
|
|
dialog.showMessageBox(mainWindow!, options).then(({ response }) => {
|
2019-03-11 23:13:46 +00:00
|
|
|
done(response === 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return mainWindow
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadURL = async (appUrl: string) => {
|
|
|
|
mainWindow = await createWindow()
|
2018-09-21 05:24:42 +00:00
|
|
|
mainWindow.loadURL(appUrl)
|
|
|
|
mainWindow.focus()
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2019-03-11 23:13:46 +00:00
|
|
|
|
|
|
|
export const loadFile = async (appPath: string) => {
|
|
|
|
mainWindow = await createWindow()
|
|
|
|
mainWindow.loadFile(appPath)
|
|
|
|
mainWindow.focus()
|
|
|
|
}
|