diff --git a/src/main.js b/src/main.js index 070a946..d274e22 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,4 @@ -import { app, BrowserWindow } from 'electron'; +import { app, BrowserWindow, shell } from 'electron'; import path from 'node:path'; import started from 'electron-squirrel-startup'; @@ -7,6 +7,38 @@ if (started) { app.quit(); } +function isAppNavigation(url) { + if (MAIN_WINDOW_VITE_DEV_SERVER_URL && url.startsWith(MAIN_WINDOW_VITE_DEV_SERVER_URL)) { + return true; + } + + return url.startsWith('file://'); +} + +function openExternalLink(url) { + shell.openExternal(url).catch((error) => { + console.error('Failed to open external link:', url, error); + }); +} + +function configureExternalLinks(mainWindow) { + mainWindow.webContents.setWindowOpenHandler(({ url }) => { + openExternalLink(url); + return { action: 'deny' }; + }); + + mainWindow.webContents.on('will-navigate', (event, url) => { + if (isAppNavigation(url)) { + return; + } + + if (url.startsWith('http://') || url.startsWith('https://')) { + event.preventDefault(); + openExternalLink(url); + } + }); +} + const createWindow = () => { const { screen } = require('electron'); const primaryDisplay = screen.getPrimaryDisplay(); @@ -22,6 +54,7 @@ const createWindow = () => { }); mainWindow.setMenuBarVisibility(false); mainWindow.maximize(); + configureExternalLinks(mainWindow); // and load the index.html of the app. if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {