Skip to content

Commit 1ede9f2

Browse files
author
Tim Sinaeve
committed
feat: open external web links in default browser instead of main electron window
1 parent 652183e commit 1ede9f2

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

electron/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,24 @@ function createWindow() {
370370
mainWindow?.show();
371371
});
372372

373+
// Open external links in the default browser
374+
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
375+
if (url.startsWith('http:') || url.startsWith('https:')) {
376+
shell.openExternal(url);
377+
}
378+
return { action: 'deny' };
379+
});
380+
381+
mainWindow.webContents.on('will-navigate', (event, url) => {
382+
if (url !== mainWindow?.webContents.getURL() && (url.startsWith('http:') || url.startsWith('https:'))) {
383+
const isLocalhost = url.includes('localhost:') || url.includes('127.0.0.1:');
384+
if (!isLocalhost) {
385+
event.preventDefault();
386+
shell.openExternal(url);
387+
}
388+
}
389+
});
390+
373391
mainWindow.on('closed', () => {
374392
mainWindow = null;
375393
});

0 commit comments

Comments
 (0)