Skip to content

Commit 30f2316

Browse files
authored
fixing browser links (#233)
1 parent 2d50501 commit 30f2316

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

node-ui/main.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* 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")
44
const path = require("path")
55
const fs = require("fs")
66
const https = require("https")
@@ -407,6 +407,38 @@ function createWindow() {
407407
}
408408
)
409409

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+
410442
// Open DevTools in development (uncomment for debugging)
411443
// mainWindow.webContents.openDevTools();
412444
}

0 commit comments

Comments
 (0)