Skip to content

Commit db40ce3

Browse files
committed
feat(Platform): add $q.platform.userAgent
1 parent d18d982 commit db40ce3

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

ui/dev/components/other/platform-detection.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
Based on the device you are using to view this, Quasar detects the following:
66
</p>
77

8+
<p>
9+
User agent: "<strong>{{ $q.platform.userAgent }}</strong>"
10+
</p>
11+
812
<table class="q-table striped">
913
<thead>
1014
<tr>

ui/src/plugins/Platform.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ function getMatch (userAgent, platformMatch) {
3131
}
3232
}
3333

34+
function getClientUserAgent () {
35+
return (navigator.userAgent || navigator.vendor || window.opera).toLowerCase()
36+
}
37+
3438
function getPlatformMatch (userAgent) {
3539
return /(ipad)/.exec(userAgent) ||
3640
/(ipod)/.exec(userAgent) ||
@@ -50,12 +54,10 @@ function getPlatformMatch (userAgent) {
5054
}
5155

5256
function getPlatform (userAgent) {
53-
userAgent = (userAgent || navigator.userAgent || navigator.vendor || window.opera).toLowerCase()
54-
5557
const
5658
platformMatch = getPlatformMatch(userAgent),
5759
matched = getMatch(userAgent, platformMatch),
58-
browser = { userAgent }
60+
browser = {}
5961

6062
if (matched.browser) {
6163
browser[matched.browser] = true
@@ -239,12 +241,20 @@ export default {
239241
within: { iframe: false },
240242

241243
parseSSR (/* ssrContext */ ssr) {
242-
return ssr ? {
243-
is: getPlatform(ssr.req.headers['user-agent'] || ssr.req.headers['User-Agent']),
244-
has: this.has,
245-
within: this.within
246-
} : {
247-
is: getPlatform(),
244+
if (ssr) {
245+
const userAgent = (ssr.req.headers['user-agent'] || ssr.req.headers['User-Agent'] || '').toLowerCase()
246+
return {
247+
userAgent,
248+
is: getPlatform(userAgent),
249+
has: this.has,
250+
within: this.within
251+
}
252+
}
253+
254+
const userAgent = getClientUserAgent()
255+
return {
256+
userAgent,
257+
is: getPlatform(userAgent),
248258
...getClientProperties()
249259
}
250260
},
@@ -257,7 +267,8 @@ export default {
257267
return
258268
}
259269

260-
this.is = getPlatform()
270+
this.userAgent = getClientUserAgent()
271+
this.is = getPlatform(this.userAgent)
261272

262273
if (fromSSR === true) {
263274
queues.takeover.push(q => {

ui/src/plugins/Platform.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
"injection": "$q.platform",
33

44
"props": {
5+
"userAgent": {
6+
"type": "String",
7+
"desc": "Client browser User Agent",
8+
"examples": [
9+
"mozilla/5.0 (macintosh; intel mac os x 10_14_5) applewebkit/537.36 (khtml, like gecko) chrome/75.0.3770.100 safari/537.36"
10+
]
11+
},
12+
513
"is": {
614
"type": "Object",
715
"desc": "Client browser details (property names depend on browser)",

0 commit comments

Comments
 (0)