UNPKG

3.09 kBJavaScriptView Raw
1/*
2 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13import { ConsoleLogger as Logger } from '../Logger';
14var logger = new Logger('ClientDevice_Browser');
15export function clientInfo() {
16 if (typeof window === 'undefined') {
17 return {};
18 }
19 return browserClientInfo();
20}
21function browserClientInfo() {
22 if (typeof window === 'undefined') {
23 logger.warn('No window object available to get browser client info');
24 return {};
25 }
26 var nav = window.navigator;
27 if (!nav) {
28 logger.warn('No navigator object available to get browser client info');
29 return {};
30 }
31 var platform = nav.platform, product = nav.product, vendor = nav.vendor, userAgent = nav.userAgent, language = nav.language;
32 var type = browserType(userAgent);
33 var timezone = browserTimezone();
34 return {
35 platform: platform,
36 make: product || vendor,
37 model: type.type,
38 version: type.version,
39 appVersion: [type.type, type.version].join('/'),
40 language: language,
41 timezone: timezone,
42 };
43}
44export function dimension() {
45 if (typeof window === 'undefined') {
46 logger.warn('No window object available to get browser client info');
47 return { width: 320, height: 320 };
48 }
49 return {
50 width: window.innerWidth,
51 height: window.innerHeight,
52 };
53}
54function browserTimezone() {
55 var tzMatch = /\(([A-Za-z\s].*)\)/.exec(new Date().toString());
56 return tzMatch ? tzMatch[1] || '' : '';
57}
58export function browserType(userAgent) {
59 var operaMatch = /.+(Opera[\s[A-Z]*|OPR[\sA-Z]*)\/([0-9\.]+).*/i.exec(userAgent);
60 if (operaMatch) {
61 return { type: operaMatch[1], version: operaMatch[2] };
62 }
63 var ieMatch = /.+(Trident|Edge)\/([0-9\.]+).*/i.exec(userAgent);
64 if (ieMatch) {
65 return { type: ieMatch[1], version: ieMatch[2] };
66 }
67 var cfMatch = /.+(Chrome|Firefox|FxiOS)\/([0-9\.]+).*/i.exec(userAgent);
68 if (cfMatch) {
69 return { type: cfMatch[1], version: cfMatch[2] };
70 }
71 var sMatch = /.+(Safari)\/([0-9\.]+).*/i.exec(userAgent);
72 if (sMatch) {
73 return { type: sMatch[1], version: sMatch[2] };
74 }
75 var awkMatch = /.+(AppleWebKit)\/([0-9\.]+).*/i.exec(userAgent);
76 if (awkMatch) {
77 return { type: awkMatch[1], version: awkMatch[2] };
78 }
79 var anyMatch = /.*([A-Z]+)\/([0-9\.]+).*/i.exec(userAgent);
80 if (anyMatch) {
81 return { type: anyMatch[1], version: anyMatch[2] };
82 }
83 return { type: '', version: '' };
84}
85//# sourceMappingURL=browser.js.map
\No newline at end of file