forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Platform.js
355 lines (313 loc) · 9.25 KB
/
Platform.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* eslint-disable no-useless-escape */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-mixed-operators */
import Vue from 'vue'
export const isSSR = typeof window === 'undefined'
export let fromSSR = false
export let onSSR = isSSR
export let iosEmulated = false
export let iosCorrection
function getMatch (userAgent, platformMatch) {
const match = /(edge|edga|edgios)\/([\w.]+)/.exec(userAgent) ||
/(opr)[\/]([\w.]+)/.exec(userAgent) ||
/(vivaldi)[\/]([\w.]+)/.exec(userAgent) ||
/(chrome|crios)[\/]([\w.]+)/.exec(userAgent) ||
/(iemobile)[\/]([\w.]+)/.exec(userAgent) ||
/(version)(applewebkit)[\/]([\w.]+).*(safari)[\/]([\w.]+)/.exec(userAgent) ||
/(webkit)[\/]([\w.]+).*(version)[\/]([\w.]+).*(safari)[\/]([\w.]+)/.exec(userAgent) ||
/(firefox|fxios)[\/]([\w.]+)/.exec(userAgent) ||
/(webkit)[\/]([\w.]+)/.exec(userAgent) ||
/(opera)(?:.*version|)[\/]([\w.]+)/.exec(userAgent) ||
/(msie) ([\w.]+)/.exec(userAgent) ||
(userAgent.indexOf('trident') >= 0 && /(rv)(?::| )([\w.]+)/.exec(userAgent)) ||
(userAgent.indexOf('compatible') < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(userAgent)) ||
[]
return {
browser: match[5] || match[3] || match[1] || '',
version: match[2] || match[4] || '0',
versionNumber: match[4] || match[2] || '0',
platform: platformMatch[0] || ''
}
}
function getPlatformMatch (userAgent) {
return /(ipad)/.exec(userAgent) ||
/(ipod)/.exec(userAgent) ||
/(windows phone)/.exec(userAgent) ||
/(iphone)/.exec(userAgent) ||
/(kindle)/.exec(userAgent) ||
/(silk)/.exec(userAgent) ||
/(android)/.exec(userAgent) ||
/(win)/.exec(userAgent) ||
/(mac)/.exec(userAgent) ||
/(linux)/.exec(userAgent) ||
/(cros)/.exec(userAgent) ||
/(playbook)/.exec(userAgent) ||
/(bb)/.exec(userAgent) ||
/(blackberry)/.exec(userAgent) ||
[]
}
const hasTouch = isSSR === false
? 'ontouchstart' in window || window.navigator.maxTouchPoints > 0
: false
function applyIosCorrection (is) {
iosCorrection = { is: { ...is } }
delete is.mac
delete is.desktop
const platform = Math.min(window.innerHeight, window.innerWidth) > 414
? 'ipad'
: 'iphone'
Object.assign(is, {
mobile: true,
ios: true,
platform,
[ platform ]: true
})
}
function getPlatform (UA) {
const
userAgent = UA.toLowerCase(),
platformMatch = getPlatformMatch(userAgent),
matched = getMatch(userAgent, platformMatch),
browser = {}
if (matched.browser) {
browser[matched.browser] = true
browser.version = matched.version
browser.versionNumber = parseInt(matched.versionNumber, 10)
}
if (matched.platform) {
browser[matched.platform] = true
}
const knownMobiles = browser.android ||
browser.ios ||
browser.bb ||
browser.blackberry ||
browser.ipad ||
browser.iphone ||
browser.ipod ||
browser.kindle ||
browser.playbook ||
browser.silk ||
browser['windows phone']
// These are all considered mobile platforms, meaning they run a mobile browser
if (knownMobiles === true || userAgent.indexOf('mobile') > -1) {
browser.mobile = true
if (browser.edga || browser.edgios) {
browser.edge = true
matched.browser = 'edge'
}
else if (browser.crios) {
browser.chrome = true
matched.browser = 'chrome'
}
else if (browser.fxios) {
browser.firefox = true
matched.browser = 'firefox'
}
}
// If it's not mobile we should consider it's desktop platform, meaning it runs a desktop browser
// It's a workaround for anonymized user agents
// (browser.cros || browser.mac || browser.linux || browser.win)
else {
browser.desktop = true
}
// Set iOS if on iPod, iPad or iPhone
if (browser.ipod || browser.ipad || browser.iphone) {
browser.ios = true
}
if (browser['windows phone']) {
browser.winphone = true
delete browser['windows phone']
}
// Chrome, Opera 15+, Vivaldi and Safari are webkit based browsers
if (
browser.chrome ||
browser.opr ||
browser.safari ||
browser.vivaldi ||
// we expect unknown, non iOS mobile browsers to be webkit based
(
browser.mobile === true &&
browser.ios !== true &&
knownMobiles !== true
)
) {
browser.webkit = true
}
// IE11 has a new token so we will assign it msie to avoid breaking changes
if (browser.rv || browser.iemobile) {
matched.browser = 'ie'
browser.ie = true
}
// Blackberry browsers are marked as Safari on BlackBerry
if (browser.safari && browser.blackberry || browser.bb) {
matched.browser = 'blackberry'
browser.blackberry = true
}
// Playbook browsers are marked as Safari on Playbook
if (browser.safari && browser.playbook) {
matched.browser = 'playbook'
browser.playbook = true
}
// Opera 15+ are identified as opr
if (browser.opr) {
matched.browser = 'opera'
browser.opera = true
}
// Stock Android browsers are marked as Safari on Android.
if (browser.safari && browser.android) {
matched.browser = 'android'
browser.android = true
}
// Kindle browsers are marked as Safari on Kindle
if (browser.safari && browser.kindle) {
matched.browser = 'kindle'
browser.kindle = true
}
// Kindle Silk browsers are marked as Safari on Kindle
if (browser.safari && browser.silk) {
matched.browser = 'silk'
browser.silk = true
}
if (browser.vivaldi) {
matched.browser = 'vivaldi'
browser.vivaldi = true
}
// Assign the name and platform variable
browser.name = matched.browser
browser.platform = matched.platform
if (isSSR === false) {
if (userAgent.indexOf('electron') > -1) {
browser.electron = true
}
else if (document.location.href.indexOf('-extension://') > -1) {
browser.bex = true
}
else {
if (window.Capacitor !== void 0) {
browser.capacitor = true
browser.nativeMobile = true
browser.nativeMobileWrapper = 'capacitor'
}
else if (window._cordovaNative !== void 0 || window.cordova !== void 0) {
browser.cordova = true
browser.nativeMobile = true
browser.nativeMobileWrapper = 'cordova'
}
if (
hasTouch === true &&
browser.mac === true &&
(
(browser.desktop === true && browser.safari === true) ||
(
browser.nativeMobile === true &&
browser.android !== true &&
browser.ios !== true &&
browser.ipad !== true
)
)
) {
/*
* Correction needed for iOS since the default
* setting on iPad is to request desktop view; if we have
* touch support and the user agent says it's a
* desktop, we infer that it's an iPhone/iPad with desktop view
* so we must fix the false positives
*/
applyIosCorrection(browser)
}
}
fromSSR = browser.nativeMobile === void 0 &&
browser.electron === void 0 &&
document.querySelector('[data-server-rendered]') !== null
if (fromSSR === true) {
onSSR = true
}
}
return browser
}
const userAgent = isSSR !== true
? navigator.userAgent || navigator.vendor || window.opera
: ''
const ssrClient = {
has: {
touch: false,
webStorage: false
},
within: { iframe: false }
}
// We export "client" for hydration error-free parts,
// like touch directives who do not (and must NOT) wait
// for the client takeover;
// Do NOT import this directly in your app, unless you really know
// what you are doing.
export const client = isSSR === false
? {
userAgent,
is: getPlatform(userAgent),
has: {
touch: hasTouch,
webStorage: (() => {
try {
if (window.localStorage) {
return true
}
}
catch (e) {}
return false
})()
},
within: {
iframe: window.self !== window.top
}
}
: ssrClient
const Platform = {
install ($q, queues) {
if (isSSR === true) {
// we're on server-side, so we push
// to the server queue instead of
// applying directly
queues.server.push((q, ctx) => {
q.platform = this.parseSSR(ctx.ssr)
})
}
else if (fromSSR === true) {
// must match with server-side before
// client taking over in order to prevent
// hydration errors
Object.assign(this, client, iosCorrection, ssrClient)
// takeover should increase accuracy for
// the rest of the props; we also avoid
// hydration errors
queues.takeover.push(q => {
onSSR = fromSSR = false
Object.assign(q.platform, client)
iosCorrection = void 0
})
// we need to make platform reactive
// for the takeover phase
Vue.util.defineReactive($q, 'platform', this)
}
else {
// we don't have any business with SSR, so
// directly applying...
Object.assign(this, client)
$q.platform = this
}
}
}
if (isSSR === true) {
Platform.parseSSR = (/* ssrContext */ ssr) => {
const userAgent = ssr.req.headers['user-agent'] || ssr.req.headers['User-Agent'] || ''
return {
...client,
userAgent,
is: getPlatform(userAgent)
}
}
}
else {
iosEmulated = client.is.ios === true &&
window.navigator.vendor.toLowerCase().indexOf('apple') === -1
}
export default Platform