angular browser detector

Solutions on MaxInterview for angular browser detector by the best coders in the world

showing results for - "angular browser detector"
Maximilian
25 Sep 2019
1import {Injectable} from '@angular/core';
2
3@Injectable({
4  providedIn: 'root'
5})
6export class BrowserDetectorService {
7
8  private browser: string;
9  private version: number | string;
10  private os: any;
11  private versionSearchString: any;
12
13
14  private dataBrowser = [
15    {
16      string: navigator.userAgent,
17      subString: 'Edg',
18      versionSearch: 'Edg',
19      identity: 'Microsoft Edge'
20    },
21    {
22      string: navigator.userAgent,
23      subString: 'OPR',
24      versionSearch: 'OPR',
25      identity: 'Opera'
26    },
27    {
28      string: navigator.userAgent,
29      subString: 'Chrome',
30      identity: 'Chrome'
31    },
32    {
33      string: navigator.userAgent,
34      subString: 'OmniWeb',
35      versionSearch: 'OmniWeb/',
36      identity: 'OmniWeb'
37    },
38    {
39      string: navigator.vendor,
40      subString: 'Apple',
41      identity: 'Safari',
42      versionSearch: 'Version'
43    },
44    {
45      prop: (window as any).opera,
46      identity: 'Opera'
47    },
48    {
49      string: navigator.vendor,
50      subString: 'iCab',
51      identity: 'iCab'
52    },
53    {
54      string: navigator.vendor,
55      subString: 'KDE',
56      identity: 'Konqueror'
57    },
58    {
59      string: navigator.userAgent,
60      subString: 'Firefox',
61      identity: 'Firefox'
62    },
63    {
64      string: navigator.vendor,
65      subString: 'Camino',
66      identity: 'Camino'
67    },
68    {
69      // for newer Netscapes (6+)
70      string: navigator.userAgent,
71      subString: 'Netscape',
72      identity: 'Netscape'
73    },
74    {
75      string: navigator.userAgent,
76      subString: 'MSIE',
77      identity: 'Explorer',
78      versionSearch: 'MSIE'
79    },
80    {
81      string: navigator.userAgent,
82      subString: 'Gecko',
83      identity: 'Mozilla',
84      versionSearch: 'rv'
85    },
86    {
87      // for older Netscapes (4-)
88      string: navigator.userAgent,
89      subString: 'Mozilla',
90      identity: 'Netscape',
91      versionSearch: 'Mozilla'
92    }
93  ];
94
95  private dataOS = [
96    {
97      string: navigator.platform,
98      subString: 'Win',
99      identity: 'Windows'
100    },
101    {
102      string: navigator.platform,
103      subString: 'Mac',
104      identity: 'Mac'
105    },
106    {
107      string: navigator.userAgent,
108      subString: 'iPhone',
109      identity: 'iPhone/iPod'
110    },
111    {
112      string: navigator.userAgent,
113      subString: 'iPad',
114      identity: 'iPad'
115    },
116
117    {
118      string: navigator.platform,
119      subString: 'Linux',
120      identity: 'Linux'
121    }
122  ];
123
124  constructor() {
125  }
126
127  onGetBrowserInfo(): void {
128    this.browser = this.searchString(this.dataBrowser) || 'An unknown browser';
129
130    this.version =
131      this.searchVersion(navigator.userAgent) ||
132      this.searchVersion(navigator.appVersion) ||
133      'an unknown version';
134
135    this.os = this.searchString(this.dataOS) || 'an unknown OS';
136
137    const lBrowserInfo = {
138      BROWSER: this.browser,
139      VERSION: this.version,
140      OS: this.os
141    };
142
143    console.table(lBrowserInfo);
144  }
145
146
147  private searchString(iData: any): string {
148
149    for (const data in iData) {
150
151      if (iData) {
152        const lDataString = iData[data].string;
153        const lDataProp = iData[data].prop;
154
155        this.versionSearchString = iData[data].versionSearch || iData[data].identity;
156
157        if (lDataString) {
158          if (lDataString.indexOf(iData[data].subString) !== -1) {
159            return iData[data].identity;
160          }
161        } else if (lDataProp) {
162          return iData[data].identity;
163        }
164      }
165
166    }
167  }
168
169
170  private searchVersion(dataString: string): number {
171    const lIndex = dataString.indexOf(this.versionSearchString);
172
173    if (lIndex === -1) {
174      return;
175    }
176    return parseFloat(
177      dataString.substring(lIndex + this.versionSearchString.length + 1)
178    );
179  }
180}
181