-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
293 lines (244 loc) · 8.45 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>test</title>
<script src="detectbrowser.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style>
#browser {
font-size: 16px;
font-family: monospace;
}
table {
border-collapse: collapse;
}
table,
tr,
td,
th {
border: 1px solid #333;
}
tbody {
border-bottom: 4px solid #333;
}
th,
td {
padding: 2px;
vertical-align: top;
}
.legend {
display: inline-block;
width: 100px;
border: 1px solid #ccc;
text-align: center;
padding: 3px;
margin-bottom: 20px;
}
.pass {
background: green;
}
.fail {
background: red;
}
.no-test {}
#testUA {
padding: 20px;
background: #efefef;
margin: 10px 10px 20px 10px;
}
input {
width: 50%;
font-size: 14px;
}
</style>
</head>
<body>
<div id="browser"></div>
<div id="testUA">
<h3>Test a user agent</h3>
<form>
<input type="text" />
<br>
<button>Test</button>
</form>
<div id="test-results"></div>
</div>
<div class="legend pass">Pass</div>
<div class="legend fail">Fail</div>
<div class="legend no-test">Not tested</div>
<p>There are many properties are not being tested (i.e.: OS), because I want to reduce defining test objects in userAgents
object. As long the detection returns a browser name, it satisfies my requirements. </p>
<table id="results">
<thead>
<tr>
<th colspan="5"> detected results</th>
<th></th>
</tr>
<tr>
<th>browser</th>
<th>version</th>
<th>long version</th>
<th>os</th>
<th>mobile?</th>
<th>userAgent</th>
</tr>
</thead>
</table>
<script>
// samples http://www.useragentstring.com/pages/Internet%20Explorer/
var userAgents = [
{
browser: {
name: 'msie',
os: 'windows',
mobile: '' // test
},
tests: [
{ version: 11, ua: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko' }
, { version: 11, ua: 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko' }
, { version: 10, ua: 'Mozilla/4.0 (Compatible; MSIE 8.0; Windows NT 5.2; Trident/6.0)' }
, { version: 9, ua: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)' }
, { version: 8, ua: 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)' }
, { version: 7, ua: 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)' }
]
},
{
browser: {
name: 'edge',
os: 'windows',
mobile: '' // test
},
tests: [
{ version: 13, ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586' }
]
},
{
browser: {
name: 'safari',
mobile: '' // still test for empty string, unless it's undefined
},
tests: [
{ version: 7, ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A' }
, { version: 4, ua: 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16' }
, { version: 3, ua: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; de-de) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.28.3' }
]
},
{
browser: {
name: 'safari',
mobile: 'ipad'
},
tests: [
{ version: 5, ua: 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 Safari/7534.48.3' }
]
},
{
browser: {
name: 'safari',
mobile: 'iphone'
},
tests: [
{ version: 5, ua: 'Mozilla/5.0 (iPhone; U; fr; CPU iPhone OS 4_2_1 like Mac OS X; fr) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5' }
]
}
, {
browser: {
name: 'safari',
mobile: 'ipod'
},
tests: [
{ version: 5, ua: 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5' }
]
}
, {
browser: {
name: 'chrome',
mobile: ''
},
tests: [
{ version: 41, ua: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' }
, { version: 41, ua: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36' }
, { version: 37, ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36' }
]
}
, {
browser: {
name: 'android',
mobile: 'android'
},
tests: [
{ version: 2, ua: 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' }
]
},
{
browser: {
name: 'opera',
mobile: '' // test
},
tests: [
{ version: 12, ua: 'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16' }
, { version: 12, ua: 'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16' }
, { version: 12, ua: 'Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14' }
]
},
{
browser: {
name: 'opera',
mobile: 'opera mobi' // test
},
tests: [
{ version: 12, ua: 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02' }
, { version: 11, ua: 'Opera/9.80 (S60; SymbOS; Opera Mobi/SYB-1107071606; U; en) Presto/2.8.149 Version/11.10' }
, { version: 10, ua: 'Mozilla/4.0 (compatible; Windows Mobile; WCE; Opera Mobi/WMD-50433; U; de) Presto/2.4.13 Version/10.00' }
, { version: 10, ua: 'Opera/9.80 (Macintosh; Intel Mac OS X; Opera Mobi/27; U; en) Presto/2.4.18 Version/10.00' }
]
},
{
browser: {
name: 'firefox',
mobile: '' // test
},
tests: [
{ version: 36, ua: 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0' }
]
}
];
function hiliteClass(str, compareTo) {
if (typeof compareTo == 'undefined') {
return '';
}
return str == compareTo ? 'pass' : 'fail';
}
$(document).ready(function () {
var br = detectBrowser(navigator.userAgent);
$('#browser').append('Your browser is: ' + br.name + ',<br> version: ' + br.version + '<br> os: ' + br.os + '<br> mobile: ' + br.mobile + '<br> user-agent: ' + navigator.userAgent);
var table = $('#results');
userAgents.forEach(function (obj) {
var str = '';
obj.tests.forEach(function (test) {
var br = detectBrowser(test.ua);
str += '<tr>' +
'<td class="' + hiliteClass(obj.browser.name, obj.browser.name) + '">' + br.name + '</td>'
+ '<td class="' + hiliteClass(test.version, test.version) + '">' + test.version + '</td>'
+ '<td class="">' + br.longVersion + '</td>'
+ '<td class="' + hiliteClass(br.os, obj.browser.os) + '">' + br.os + '</td>'
+ '<td class="' + hiliteClass(br.mobile, obj.browser.mobile) + '">' + br.mobile + '</td>'
+ '<td>' + test.ua + '</td>'
+ '</tr>'
;
});
str = '<tbody>' + str + '</tbody>';
table.append(str);
});
}); // ready
$('form button').on('click', function (ev) {
ev.preventDefault();
var br = detectBrowser($('input').val());
$('#test-results').html(
'detected results:<br> browser: ' + br.name + ',<br> version: ' + br.version + ',<br> longVersion: ' + br.longVersion + '<br> os: ' + br.os + '<br> mobile: ' + br.mobile
);
})
</script>
</body>
</html>