@@ -1182,7 +1182,7 @@ export function computeScreenAwareSize(cssPx: number): number {
1182
1182
/**
1183
1183
* Open safely a new window. This is the best way to do so, but you cannot tell
1184
1184
* if the window was opened or if it was blocked by the browser's popup blocker.
1185
- * If you want to tell if the browser blocked the new window, use `windowOpenNoOpenerWithSuccess` .
1185
+ * If you want to tell if the browser blocked the new window, use { @link windowOpenWithSuccess} .
1186
1186
*
1187
1187
* See https://github.com/microsoft/monaco-editor/issues/601
1188
1188
* To protect against malicious code in the linked site, particularly phishing attempts,
@@ -1201,19 +1201,49 @@ export function windowOpenNoOpener(url: string): void {
1201
1201
}
1202
1202
1203
1203
/**
1204
- * Open safely a new window. This technique is not appropriate in certain contexts,
1205
- * like for example when the JS context is executing inside a sandboxed iframe.
1206
- * If it is not necessary to know if the browser blocked the new window, use
1207
- * `windowOpenNoOpener`.
1204
+ * Open a new window in a popup. This is the best way to do so, but you cannot tell
1205
+ * if the window was opened or if it was blocked by the browser's popup blocker.
1206
+ * If you want to tell if the browser blocked the new window, use {@link windowOpenWithSuccess}.
1207
+ *
1208
+ * Note: this does not set {@link window.opener} to null. This is to allow the opened popup to
1209
+ * be able to use {@link window.close} to close itself. Because of this, you should only use
1210
+ * this function on urls that you trust.
1211
+ *
1212
+ * In otherwords, you should almost always use {@link windowOpenNoOpener} instead of this function.
1213
+ */
1214
+ const popupWidth = 780 , popupHeight = 640 ;
1215
+ export function windowOpenPopup ( url : string ) : void {
1216
+ const left = Math . floor ( window . screenLeft + window . innerWidth / 2 - popupWidth / 2 ) ;
1217
+ const top = Math . floor ( window . screenTop + window . innerHeight / 2 - popupHeight / 2 ) ;
1218
+ window . open (
1219
+ url ,
1220
+ '_blank' ,
1221
+ `width=${ popupWidth } ,height=${ popupHeight } ,top=${ top } ,left=${ left } `
1222
+ ) ;
1223
+ }
1224
+
1225
+ /**
1226
+ * Attempts to open a window and returns whether it succeeded. This technique is
1227
+ * not appropriate in certain contexts, like for example when the JS context is
1228
+ * executing inside a sandboxed iframe. If it is not necessary to know if the
1229
+ * browser blocked the new window, use {@link windowOpenNoOpener}.
1208
1230
*
1209
1231
* See https://github.com/microsoft/monaco-editor/issues/601
1210
1232
* See https://github.com/microsoft/monaco-editor/issues/2474
1211
1233
* See https://mathiasbynens.github.io/rel-noopener/
1234
+ *
1235
+ * @param url the url to open
1236
+ * @param noOpener whether or not to set the {@link window.opener} to null. You should leave the default
1237
+ * (true) unless you trust the url that is being opened.
1238
+ * @returns boolean indicating if the {@link window.open} call succeeded
1212
1239
*/
1213
- export function windowOpenNoOpenerWithSuccess ( url : string ) : boolean {
1240
+ export function windowOpenWithSuccess ( url : string , noOpener = true ) : boolean {
1214
1241
const newTab = window . open ( ) ;
1215
1242
if ( newTab ) {
1216
- ( newTab as any ) . opener = null ;
1243
+ if ( noOpener ) {
1244
+ // see `windowOpenNoOpener` for details on why this is important
1245
+ ( newTab as any ) . opener = null ;
1246
+ }
1217
1247
newTab . location . href = url ;
1218
1248
return true ;
1219
1249
}
0 commit comments