From 4839f2901a323b080c888a3ff3194ffab932b393 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 25 Feb 2024 03:52:06 +0530 Subject: [PATCH] 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 --- src/interface/desktop/main.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/interface/desktop/main.js b/src/interface/desktop/main.js index 32c58a23..587b8ef9 100644 --- a/src/interface/desktop/main.js +++ b/src/interface/desktop/main.js @@ -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)