2021-12-06 05:44:41 +03:00
|
|
|
import { shell } from 'electron/common';
|
|
|
|
import { app, dialog, BrowserWindow, ipcMain } from 'electron/main';
|
2019-02-06 21:27:20 +03:00
|
|
|
import * as path from 'path';
|
2020-06-22 15:14:20 +03:00
|
|
|
import * as url from 'url';
|
2019-01-10 22:54:34 +03:00
|
|
|
|
2019-02-06 21:27:20 +03:00
|
|
|
let mainWindow: BrowserWindow | null = null;
|
2013-07-17 12:21:33 +04:00
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
2016-05-07 02:53:50 +03:00
|
|
|
app.on('window-all-closed', () => {
|
2016-03-24 23:15:04 +03:00
|
|
|
app.quit();
|
|
|
|
});
|
2013-07-17 12:21:33 +04:00
|
|
|
|
2019-03-12 02:13:46 +03: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;
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:14:20 +03:00
|
|
|
try {
|
|
|
|
return url.fileURLToPath(webContents.getURL()) === indexPath;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-12 02:13:46 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 19:13:35 +03:00
|
|
|
ipcMain.handle('bootstrap', (event) => {
|
|
|
|
return isTrustedSender(event.sender) ? electronPath : null;
|
2019-03-12 02:13:46 +03:00
|
|
|
});
|
|
|
|
|
2021-04-26 10:29:14 +03:00
|
|
|
async function createWindow (backgroundColor?: string) {
|
2018-09-21 08:24:42 +03:00
|
|
|
await app.whenReady();
|
|
|
|
|
2019-03-12 02:13:46 +03:00
|
|
|
const options: Electron.BrowserWindowConstructorOptions = {
|
2020-03-16 09:28:34 +03:00
|
|
|
width: 960,
|
|
|
|
height: 620,
|
2018-09-21 08:24:42 +03:00
|
|
|
autoHideMenuBar: true,
|
2021-04-26 10:29:14 +03:00
|
|
|
backgroundColor,
|
2018-09-21 08:24:42 +03:00
|
|
|
webPreferences: {
|
2019-03-12 02:13:46 +03:00
|
|
|
preload: path.resolve(__dirname, 'preload.js'),
|
2018-09-21 08:24:42 +03:00
|
|
|
contextIsolation: true,
|
2021-03-10 04:12:40 +03:00
|
|
|
sandbox: true
|
2018-09-21 08:24:42 +03:00
|
|
|
},
|
|
|
|
useContentSize: true,
|
|
|
|
show: false
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
options.icon = path.join(__dirname, 'icon.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow = new BrowserWindow(options);
|
2019-02-06 21:27:20 +03:00
|
|
|
mainWindow.on('ready-to-show', () => mainWindow!.show());
|
2018-09-21 08:24:42 +03:00
|
|
|
|
2022-05-24 11:23:56 +03:00
|
|
|
mainWindow.webContents.setWindowOpenHandler(details => {
|
|
|
|
shell.openExternal(decorateURL(details.url));
|
|
|
|
return { action: 'deny' };
|
2019-03-12 02:13:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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 19:54:53 +03:00
|
|
|
dialog.showMessageBox(mainWindow!, options).then(({ response }) => {
|
2019-03-12 02:13:46 +03:00
|
|
|
done(response === 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return mainWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadURL = async (appUrl: string) => {
|
|
|
|
mainWindow = await createWindow();
|
2018-09-21 08:24:42 +03:00
|
|
|
mainWindow.loadURL(appUrl);
|
|
|
|
mainWindow.focus();
|
2016-03-24 23:15:04 +03:00
|
|
|
};
|
2019-03-12 02:13:46 +03:00
|
|
|
|
|
|
|
export const loadFile = async (appPath: string) => {
|
2021-04-26 10:29:14 +03:00
|
|
|
mainWindow = await createWindow(appPath === 'index.html' ? '#2f3241' : undefined);
|
2019-03-12 02:13:46 +03:00
|
|
|
mainWindow.loadFile(appPath);
|
|
|
|
mainWindow.focus();
|
|
|
|
};
|