Restore behavior of BrowserWindow.setupOpenHandlers (see microsoft/monaco-editor#2474)

This commit is contained in:
Alex Dima
2021-05-12 14:46:16 +02:00
parent 579cf91f0a
commit 0a2f87623d
2 changed files with 28 additions and 6 deletions

View File

@@ -1179,19 +1179,41 @@ export function computeScreenAwareSize(cssPx: number): number {
}
/**
* Open safely a new window. This is the best way to do so, but you cannot tell
* if the window was opened or if it was blocked by the brower's popup blocker.
* If you want to tell if the browser blocked the new window, use `windowOpenNoOpenerWithSuccess`.
*
* See https://github.com/microsoft/monaco-editor/issues/601
* To protect against malicious code in the linked site, particularly phishing attempts,
* the window.opener should be set to null to prevent the linked site from having access
* to change the location of the current page.
* See https://mathiasbynens.github.io/rel-noopener/
*/
export function windowOpenNoOpener(url: string): boolean {
// By using 'noopener' in the windowFeatures argument, the newly created window will
// not be able to use window.opener to reach back to the current page.
export function windowOpenNoOpener(url: string): void {
// By using 'noopener' in the `windowFeatures` argument, the newly created window will
// not be able to use `window.opener` to reach back to the current page.
// See https://stackoverflow.com/a/46958731
// See https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener
const newTab = window.open(url, '_blank', 'noopener');
// However, this also doesn't allow us to realize if the browser blocked
// the creation of the window.
window.open(url, '_blank', 'noopener');
}
/**
* Open safely a new window. This technique is not appropiate in certain contexts,
* like for example when the JS context is executing inside a sandboxed iframe.
* If it is not necessary to know if the browser blocked the new window, use
* `windowOpenNoOpener`.
*
* See https://github.com/microsoft/monaco-editor/issues/601
* See https://github.com/microsoft/monaco-editor/issues/2474
* See https://mathiasbynens.github.io/rel-noopener/
*/
export function windowOpenNoOpenerWithSuccess(url: string): boolean {
const newTab = window.open();
if (newTab) {
(newTab as any).opener = null;
newTab.location.href = url;
return true;
}
return false;