electron/default_app/default_app.ts

103 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { app, dialog, BrowserWindow, shell, ipcMain } from 'electron';
import * as path from 'path';
2020-03-20 20:28:31 +00:00
let mainWindow: BrowserWindow | null = null;
// Quit when all windows are closed.
app.on('window-all-closed', () => {
2020-03-20 20:28:31 +00:00
app.quit();
});
function decorateURL (url: string) {
// safely add `?utm_source=default_app
2020-03-20 20:28:31 +00:00
const parsedUrl = new URL(url);
parsedUrl.searchParams.append('utm_source', 'default_app');
return parsedUrl.toString();
}
// Find the shortest path to the electron binary
2020-03-20 20:28:31 +00:00
const absoluteElectronPath = process.execPath;
const relativeElectronPath = path.relative(process.cwd(), absoluteElectronPath);
const electronPath = absoluteElectronPath.length < relativeElectronPath.length
? absoluteElectronPath
2020-03-20 20:28:31 +00:00
: relativeElectronPath;
2020-03-20 20:28:31 +00:00
const indexPath = path.resolve(app.getAppPath(), 'index.html');
function isTrustedSender (webContents: Electron.WebContents) {
if (webContents !== (mainWindow && mainWindow.webContents)) {
2020-03-20 20:28:31 +00:00
return false;
}
2020-03-20 20:28:31 +00:00
const parsedUrl = new URL(webContents.getURL());
const urlPath = process.platform === 'win32'
// Strip the prefixed "/" that occurs on windows
? path.resolve(parsedUrl.pathname.substr(1))
2020-03-20 20:28:31 +00:00
: parsedUrl.pathname;
return parsedUrl.protocol === 'file:' && urlPath === indexPath;
}
ipcMain.handle('bootstrap', (event) => {
2020-03-20 20:28:31 +00:00
return isTrustedSender(event.sender) ? electronPath : null;
});
async function createWindow () {
2020-03-20 20:28:31 +00:00
await app.whenReady();
const options: Electron.BrowserWindowConstructorOptions = {
width: 960,
height: 620,
autoHideMenuBar: true,
backgroundColor: '#2f3241',
webPreferences: {
preload: path.resolve(__dirname, 'preload.js'),
contextIsolation: true,
sandbox: true,
enableRemoteModule: false
},
useContentSize: true,
show: false
2020-03-20 20:28:31 +00:00
};
if (process.platform === 'linux') {
2020-03-20 20:28:31 +00:00
options.icon = path.join(__dirname, 'icon.png');
}
2020-03-20 20:28:31 +00:00
mainWindow = new BrowserWindow(options);
mainWindow.on('ready-to-show', () => mainWindow!.show());
mainWindow.webContents.on('new-window', (event, url) => {
2020-03-20 20:28:31 +00:00
event.preventDefault();
shell.openExternal(decorateURL(url));
});
mainWindow.webContents.session.setPermissionRequestHandler((webContents, permission, done) => {
2020-03-20 20:28:31 +00:00
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
2020-03-20 20:28:31 +00:00
};
dialog.showMessageBox(mainWindow!, options).then(({ response }) => {
2020-03-20 20:28:31 +00:00
done(response === 0);
});
});
2020-03-20 20:28:31 +00:00
return mainWindow;
}
export const loadURL = async (appUrl: string) => {
2020-03-20 20:28:31 +00:00
mainWindow = await createWindow();
mainWindow.loadURL(appUrl);
mainWindow.focus();
};
export const loadFile = async (appPath: string) => {
2020-03-20 20:28:31 +00:00
mainWindow = await createWindow();
mainWindow.loadFile(appPath);
mainWindow.focus();
};