rvv-desktop/main.js

109 lines
2.4 KiB
JavaScript

const { app, BrowserWindow, Tray, Menu, shell } = require('electron');
const path = require('path');
const TARGET_URL = 'https://www.rvv.de';
let mainWindow = null;
let tray = null;
function createWindow() {
mainWindow = new BrowserWindow({
width: 414,
height: 896,
title: 'RVV',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
mainWindow.loadURL(TARGET_URL);
// Minimize to tray instead of closing
mainWindow.on('close', (event) => {
if (!app.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
});
// Open external links in the system browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
const targetHost = new URL(TARGET_URL).host;
const linkHost = new URL(url).host;
if (!linkHost.endsWith(targetHost)) {
shell.openExternal(url);
return { action: 'deny' };
}
return { action: 'allow' };
});
// Handle navigation to external sites
mainWindow.webContents.on('will-navigate', (event, url) => {
const targetHost = new URL(TARGET_URL).host;
try {
const linkHost = new URL(url).host;
if (!linkHost.endsWith(targetHost)) {
event.preventDefault();
shell.openExternal(url);
}
} catch {
// allow navigation if URL parsing fails
}
});
// Show fallback page if the site fails to load
mainWindow.webContents.on('did-fail-load', (_event, _code, _desc, url) => {
if (url.startsWith(TARGET_URL)) {
mainWindow.loadFile('fallback.html');
}
});
}
function createTray() {
tray = new Tray(path.join(__dirname, 'rvv-icon.png'));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show RVV',
click: () => {
mainWindow.show();
},
},
{ type: 'separator' },
{
label: 'Quit',
click: () => {
app.isQuitting = true;
app.quit();
},
},
]);
tray.setToolTip('RVV');
tray.setContextMenu(contextMenu);
// Click on tray icon shows the window
tray.on('click', () => {
mainWindow.show();
});
}
app.whenReady().then(() => {
createWindow();
createTray();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
} else {
mainWindow.show();
}
});
});
app.on('window-all-closed', () => {
// Don't quit on window close — we live in the tray
});