electron/default_app/default_app.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

import { shell } from 'electron/common';
import { app, dialog, BrowserWindow, ipcMain } from 'electron/main';
import * as path from 'node:path';
import * as url from 'node:url';
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;
}
try {
return url.fileURLToPath(webContents.getURL()) === indexPath;
} catch {
return false;
}
}
ipcMain.handle('bootstrap', (event) => {
2020-03-20 20:28:31 +00:00
return isTrustedSender(event.sender) ? electronPath : null;
});
async function createWindow (backgroundColor?: string) {
2020-03-20 20:28:31 +00:00
await app.whenReady();
const options: Electron.BrowserWindowConstructorOptions = {
width: 960,
height: 620,
autoHideMenuBar: true,
backgroundColor,
webPreferences: {
preload: path.resolve(__dirname, 'preload.js'),
contextIsolation: true,
sandbox: true
},
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.setWindowOpenHandler(details => {
shell.openExternal(decorateURL(details.url));
return { action: 'deny' };
2020-03-20 20:28:31 +00:00
});
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) => {
mainWindow = await createWindow(appPath === 'index.html' ? '#2f3241' : undefined);
2020-03-20 20:28:31 +00:00
mainWindow.loadFile(appPath);
mainWindow.focus();
};