From 497bb0e2cbefad3e9a1188ee5df49cf61f6bd6e4 Mon Sep 17 00:00:00 2001 From: Akis Kesoglou Date: Thu, 6 Jul 2023 21:39:53 +0300 Subject: [PATCH] Fix opening links from within SafeWebView Change-type: patch --- .../components/safe-webview/safe-webview.tsx | 33 +++++++++---------- lib/gui/etcher.ts | 18 ++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/gui/app/components/safe-webview/safe-webview.tsx b/lib/gui/app/components/safe-webview/safe-webview.tsx index c47ae170..62b08d78 100644 --- a/lib/gui/app/components/safe-webview/safe-webview.tsx +++ b/lib/gui/app/components/safe-webview/safe-webview.tsx @@ -95,6 +95,7 @@ export class SafeWebview extends React.PureComponent< ); this.entryHref = url.href; // Events steal 'this' + this.handleDomReady = _.bind(this.handleDomReady, this); this.didFailLoad = _.bind(this.didFailLoad, this); this.didGetResponseDetails = _.bind(this.didGetResponseDetails, this); // Make a persistent electron session for the webview @@ -121,6 +122,8 @@ export class SafeWebview extends React.PureComponent< ref={this.webviewRef} partition={ELECTRON_SESSION} style={style} + // @ts-ignore + allowpopups="true" /> ); } @@ -134,8 +137,8 @@ export class SafeWebview extends React.PureComponent< this.didFailLoad, ); this.webviewRef.current.addEventListener( - 'new-window', - SafeWebview.newWindow, + 'dom-ready', + this.handleDomReady, ); this.webviewRef.current.addEventListener( 'console-message', @@ -157,8 +160,8 @@ export class SafeWebview extends React.PureComponent< this.didFailLoad, ); this.webviewRef.current.removeEventListener( - 'new-window', - SafeWebview.newWindow, + 'dom-ready', + this.handleDomReady, ); this.webviewRef.current.removeEventListener( 'console-message', @@ -168,6 +171,15 @@ export class SafeWebview extends React.PureComponent< this.session.webRequest.onCompleted(null); } + handleDomReady() { + const webview = this.webviewRef.current; + if (webview == null) { + return; + } + const id = webview.getWebContentsId(); + electron.ipcRenderer.send('webview-dom-ready', id); + } + // Set the element state to hidden public didFailLoad() { this.setState({ @@ -196,17 +208,4 @@ export class SafeWebview extends React.PureComponent< } } } - - // Open link in browser if it's opened as a 'foreground-tab' - public static async newWindow(event: electron.NewWindowEvent) { - const url = new window.URL(event.url); - if ( - (url.protocol === 'http:' || url.protocol === 'https:') && - event.disposition === 'foreground-tab' && - // Don't open links if they're disabled by the env var - !(await settings.get('disableExternalLinks')) - ) { - electron.shell.openExternal(url.href); - } - } } diff --git a/lib/gui/etcher.ts b/lib/gui/etcher.ts index 13ed23d2..104f6858 100644 --- a/lib/gui/etcher.ts +++ b/lib/gui/etcher.ts @@ -267,6 +267,24 @@ async function main(): Promise { console.log('Build menu failed. '); } }); + + electron.ipcMain.on('webview-dom-ready', (_, id) => { + const webview = electron.webContents.fromId(id); + + // Open link in browser if it's opened as a 'foreground-tab' + webview.setWindowOpenHandler((event) => { + const url = new URL(event.url); + if ( + (url.protocol === 'http:' || url.protocol === 'https:') && + event.disposition === 'foreground-tab' && + // Don't open links if they're disabled by the env var + !(settings.getSync('disableExternalLinks')) + ) { + electron.shell.openExternal(url.href); + } + return { action: 'deny' }; + }); + }); } } main();