mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-21 10:16:32 +00:00
Fix opening links from within SafeWebView
Change-type: patch
This commit is contained in:
parent
a42be8ee74
commit
497bb0e2cb
@ -95,6 +95,7 @@ export class SafeWebview extends React.PureComponent<
|
|||||||
);
|
);
|
||||||
this.entryHref = url.href;
|
this.entryHref = url.href;
|
||||||
// Events steal 'this'
|
// Events steal 'this'
|
||||||
|
this.handleDomReady = _.bind(this.handleDomReady, this);
|
||||||
this.didFailLoad = _.bind(this.didFailLoad, this);
|
this.didFailLoad = _.bind(this.didFailLoad, this);
|
||||||
this.didGetResponseDetails = _.bind(this.didGetResponseDetails, this);
|
this.didGetResponseDetails = _.bind(this.didGetResponseDetails, this);
|
||||||
// Make a persistent electron session for the webview
|
// Make a persistent electron session for the webview
|
||||||
@ -121,6 +122,8 @@ export class SafeWebview extends React.PureComponent<
|
|||||||
ref={this.webviewRef}
|
ref={this.webviewRef}
|
||||||
partition={ELECTRON_SESSION}
|
partition={ELECTRON_SESSION}
|
||||||
style={style}
|
style={style}
|
||||||
|
// @ts-ignore
|
||||||
|
allowpopups="true"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -134,8 +137,8 @@ export class SafeWebview extends React.PureComponent<
|
|||||||
this.didFailLoad,
|
this.didFailLoad,
|
||||||
);
|
);
|
||||||
this.webviewRef.current.addEventListener(
|
this.webviewRef.current.addEventListener(
|
||||||
'new-window',
|
'dom-ready',
|
||||||
SafeWebview.newWindow,
|
this.handleDomReady,
|
||||||
);
|
);
|
||||||
this.webviewRef.current.addEventListener(
|
this.webviewRef.current.addEventListener(
|
||||||
'console-message',
|
'console-message',
|
||||||
@ -157,8 +160,8 @@ export class SafeWebview extends React.PureComponent<
|
|||||||
this.didFailLoad,
|
this.didFailLoad,
|
||||||
);
|
);
|
||||||
this.webviewRef.current.removeEventListener(
|
this.webviewRef.current.removeEventListener(
|
||||||
'new-window',
|
'dom-ready',
|
||||||
SafeWebview.newWindow,
|
this.handleDomReady,
|
||||||
);
|
);
|
||||||
this.webviewRef.current.removeEventListener(
|
this.webviewRef.current.removeEventListener(
|
||||||
'console-message',
|
'console-message',
|
||||||
@ -168,6 +171,15 @@ export class SafeWebview extends React.PureComponent<
|
|||||||
this.session.webRequest.onCompleted(null);
|
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
|
// Set the element state to hidden
|
||||||
public didFailLoad() {
|
public didFailLoad() {
|
||||||
this.setState({
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -267,6 +267,24 @@ async function main(): Promise<void> {
|
|||||||
console.log('Build menu failed. ');
|
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();
|
main();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user