From 142859f0ed3fe045a93336a05f5a3265d14a647a Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Tue, 29 Jun 2021 13:00:18 -0700 Subject: [PATCH] Intercept click to links in the embedded documentation and redirect them to the default browser Summary: Intercept click to links in the embedded documentation and redirect them to the default browser. Reviewed By: passy Differential Revision: D29426270 fbshipit-source-id: 1d3d9613ff827f80287883768452b93701e95d96 --- desktop/static/main.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/desktop/static/main.ts b/desktop/static/main.ts index 69af30d25..0e23f02fd 100644 --- a/desktop/static/main.ts +++ b/desktop/static/main.ts @@ -18,6 +18,7 @@ import { globalShortcut, session, nativeTheme, + shell, } from 'electron'; import os from 'os'; import path from 'path'; @@ -232,6 +233,20 @@ app.on('ready', () => { }); }); +app.on('web-contents-created', (_event, contents) => { + if (contents.getType() === 'webview') { + contents.on('new-window', async (event, url) => { + // Disable creating of native Electron windows when requested from web views. + // This can happen e.g. when user clicks to a link with target="__blank" on a page loaded in a web view, + // or if some javascript code in a web view executes window.open. + // Instead of the default implementation, we redirect such URLs to the operating system which handles them automatically: + // using default browser for http/https links, using default mail client for "mailto" links etc. + event.preventDefault(); + await shell.openExternal(url); + }); + } +}); + function configureSession() { session.defaultSession.webRequest.onBeforeSendHeaders( {