Skip to content

Commit 4611415

Browse files
authored
Merge pull request #12 from github/ua-parsing
Improve User Agent parsing
2 parents 35e2390 + 5d229e5 commit 4611415

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

example/index.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@
4949
<script type="module" defer>
5050
import {isSupported, isPolyfilled, baseSupport, apply, polyfills} from '../lib/index.js'
5151
// import {isSupported, isPolyfilled, apply, poyfills} from 'https://unpkg.com/@github/browser-support@latest/lib/index.js'
52-
let browser
53-
let version
54-
for(const term of navigator.userAgent.split(' ')) {
55-
if (term.indexOf('/') !== -1) {
56-
browser = term.split('/')[0]
57-
version = term.split('/')[1]
58-
if (browser === 'Chrome') break
52+
function getBrowser(useragent) {
53+
if ('userAgentData' in navigator) {
54+
const {brand, version} = Array.from(navigator.userAgentData.brands).pop()
55+
return [brand, version]
5956
}
57+
const parts = useragent.split(' ')
58+
let [name, version] = parts.pop().split('/')
59+
if (name === 'Safari') {
60+
version = parts.pop().split('/').pop()
61+
}
62+
return [name, version]
6063
}
64+
const [browser, version] = getBrowser(navigator.userAgent)
6165
document.getElementById('your-browser').innerText = browser + " " + version
6266
for(const el of document.querySelectorAll('[data-code]')) {
6367
let supported = false

0 commit comments

Comments
 (0)