|
1 | 1 | /* eslint-env node, es6 */ |
2 | | -/* global require, __dirname, process, console, setTimeout, clearTimeout, setInterval, clearInterval */ |
3 | | -const { app, BrowserWindow, ipcMain, dialog } = require("electron") |
| 2 | +/* global require, __dirname, process, console, setTimeout, clearTimeout, setInterval, clearInterval, URL */ |
| 3 | +const { app, BrowserWindow, ipcMain, dialog, shell } = require("electron") |
4 | 4 | const path = require("path") |
5 | 5 | const fs = require("fs") |
6 | 6 | const https = require("https") |
@@ -407,6 +407,38 @@ function createWindow() { |
407 | 407 | } |
408 | 408 | ) |
409 | 409 |
|
| 410 | + // Open external links in the system's default browser instead of Electron window |
| 411 | + mainWindow.webContents.setWindowOpenHandler(({ url }) => { |
| 412 | + shell.openExternal(url) |
| 413 | + return { action: "deny" } |
| 414 | + }) |
| 415 | + |
| 416 | + // Prevent navigation within the Electron window - open external URLs in system browser |
| 417 | + mainWindow.webContents.on("will-navigate", (event, navigationUrl) => { |
| 418 | + try { |
| 419 | + const parsedUrl = new URL(navigationUrl) |
| 420 | + const currentUrlString = mainWindow.webContents.getURL() |
| 421 | + |
| 422 | + // If current URL is file://, any http/https URL should open externally |
| 423 | + if (currentUrlString.startsWith("file://")) { |
| 424 | + if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") { |
| 425 | + event.preventDefault() |
| 426 | + shell.openExternal(navigationUrl) |
| 427 | + } |
| 428 | + } else { |
| 429 | + // If navigating to a different origin, open in system browser |
| 430 | + const currentUrl = new URL(currentUrlString) |
| 431 | + if (parsedUrl.origin !== currentUrl.origin) { |
| 432 | + event.preventDefault() |
| 433 | + shell.openExternal(navigationUrl) |
| 434 | + } |
| 435 | + } |
| 436 | + } catch (err) { |
| 437 | + // If URL parsing fails, allow navigation (fallback) |
| 438 | + console.error("Error parsing URL in will-navigate:", err) |
| 439 | + } |
| 440 | + }) |
| 441 | + |
410 | 442 | // Open DevTools in development (uncomment for debugging) |
411 | 443 | // mainWindow.webContents.openDevTools(); |
412 | 444 | } |
|
0 commit comments