-
Notifications
You must be signed in to change notification settings - Fork 899
/
popup.ts
137 lines (118 loc) · 3.38 KB
/
popup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @license
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getUA } from '@firebase/util';
import { AuthErrorCode } from '../../core/errors';
import { _assert } from '../../core/util/assert';
import {
_isChromeIOS,
_isFirefox,
_isIOSStandalone
} from '../../core/util/browser';
import { AuthInternal } from '../../model/auth';
const BASE_POPUP_OPTIONS = {
location: 'yes',
resizable: 'yes',
statusbar: 'yes',
toolbar: 'no'
};
const DEFAULT_WIDTH = 500;
const DEFAULT_HEIGHT = 600;
const TARGET_BLANK = '_blank';
const FIREFOX_EMPTY_URL = 'http://localhost';
export class AuthPopup {
associatedEvent: string | null = null;
constructor(readonly window: Window | null) {}
close(): void {
if (this.window) {
try {
this.window.close();
} catch (e) {}
}
}
}
export function _open(
auth: AuthInternal,
url?: string,
name?: string,
width = DEFAULT_WIDTH,
height = DEFAULT_HEIGHT
): AuthPopup {
const top = Math.max((window.screen.availHeight - height) / 2, 0).toString();
const left = Math.max((window.screen.availWidth - width) / 2, 0).toString();
let target = '';
const options: { [key: string]: string } = {
...BASE_POPUP_OPTIONS,
width: width.toString(),
height: height.toString(),
top,
left
};
// Chrome iOS 7 and 8 is returning an undefined popup win when target is
// specified, even though the popup is not necessarily blocked.
const ua = getUA().toLowerCase();
if (name) {
target = _isChromeIOS(ua) ? TARGET_BLANK : name;
}
if (_isFirefox(ua)) {
// Firefox complains when invalid URLs are popped out. Hacky way to bypass.
url = url || FIREFOX_EMPTY_URL;
// Firefox disables by default scrolling on popup windows, which can create
// issues when the user has many Google accounts, for instance.
options.scrollbars = 'yes';
}
const optionsString = Object.entries(options).reduce(
(accum, [key, value]) => `${accum}${key}=${value},`,
''
);
if (_isIOSStandalone(ua) && target !== '_self') {
openAsNewWindowIOS(url || '', target);
return new AuthPopup(null);
}
// about:blank getting sanitized causing browsers like IE/Edge to display
// brief error message before redirecting to handler.
const newWin = window.open(url || '', target, optionsString);
_assert(newWin, auth, AuthErrorCode.POPUP_BLOCKED);
// Flaky on IE edge, encapsulate with a try and catch.
try {
newWin.focus();
} catch (e) {}
return new AuthPopup(newWin);
}
function openAsNewWindowIOS(url: string, target: string): void {
const el = document.createElement('a');
el.href = url;
el.target = target;
const click = document.createEvent('MouseEvent');
click.initMouseEvent(
'click',
true,
true,
window,
1,
0,
0,
0,
0,
false,
false,
false,
false,
1,
null
);
el.dispatchEvent(click);
}