Open external links in Desktop app with default app for url on OS

- Open external links using the default link handler registered on OS
  for the link type, e.g http:// -> firefox, mailto: thunderbird etc
- Confirm before opening non-http URL using an external app
This commit is contained in:
Debanjum Singh Solanky 2024-02-25 03:52:06 +05:30
parent 170bce2c02
commit 4839f2901a

View file

@ -380,6 +380,29 @@ const createWindow = (tab = 'chat.html') => {
win.setBackgroundColor('#f5f4f3');
win.setHasShadow(true);
// Open external links in link handler registered on OS (e.g. browser)
win.webContents.setWindowOpenHandler(async ({ url }) => {
const shouldOpen = { response: 0 };
if (!url.startsWith('http://')) {
// Confirm before opening non-HTTP links
const confirmNotice = `Do you want to open this link? It will be handled by an external application.\n\n${url}`;
shouldOpen = await dialog.showMessageBox({
type: 'question',
buttons: ['Yes', 'No'],
defaultId: 1,
title: 'Confirm',
message: confirmNotice,
});
}
// If user confirms, let OS link handler open the link in appropriate app
if (shouldOpen.response === 0) shell.openExternal(url);
// Do not open external links within the app
return { action: 'deny' };
});
job.start();
win.loadFile(tab)