-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathuser-agent-parser.html
More file actions
544 lines (485 loc) · 17.6 KB
/
Copy pathuser-agent-parser.html
File metadata and controls
544 lines (485 loc) · 17.6 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!--
User Agent Parser
Single-file client-side tool that parses a User-Agent string into a
human-readable breakdown: browser, engine, OS, device type, and CPU arch.
Built for One File Tools.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>User Agent Parser - One File Tools</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet" />
<style>
:root {
--bg: #030712;
--grid-line: #111827;
--panel: rgba(17, 24, 39, 0.7);
--panel-border: #1f2937;
--accent: #22d3ee;
--accent-hover: #06b6d4;
--accent-glow: rgba(34, 211, 238, 0.15);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #4b5563;
--radius-sm: 12px;
--radius-md: 20px;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--font-display: "Space Grotesk", sans-serif;
--color-weak: #ef4444;
--color-strong: #10b981;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
background-size: 32px 32px;
color: var(--text);
padding: 48px 20px 80px;
min-height: 100vh;
display: flex;
justify-content: center;
}
.app {
width: 100%;
max-width: 820px;
}
.hero {
text-align: center;
margin-bottom: 32px;
}
.eyebrow {
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 0.2em;
color: var(--accent);
text-transform: uppercase;
margin-bottom: 12px;
font-weight: 500;
}
.wordmark {
font-family: var(--font-display);
font-weight: 700;
font-size: clamp(26px, 5vw, 38px);
letter-spacing: -0.02em;
margin-bottom: 12px;
}
.tagline {
color: var(--text-muted);
font-size: 15px;
line-height: 1.5;
max-width: 560px;
margin: 0 auto;
}
.studio {
background: var(--panel);
backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 28px;
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.5);
margin-bottom: 24px;
}
.section-label {
font-family: var(--font-mono);
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.1em;
display: block;
margin-bottom: 14px;
}
textarea {
width: 100%;
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--panel-border);
border-radius: 8px;
color: var(--text);
font-family: var(--font-mono);
font-size: 14px;
padding: 12px 14px;
resize: vertical;
min-height: 90px;
line-height: 1.5;
}
textarea:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.actions {
display: flex;
gap: 10px;
margin-top: 14px;
flex-wrap: wrap;
}
.btn {
background: var(--accent);
color: #030712;
border: none;
border-radius: 8px;
padding: 11px 18px;
font-family: var(--font-sans);
font-size: 14px;
font-weight: 700;
cursor: pointer;
transition: background 0.2s;
}
.btn:hover {
background: var(--accent-hover);
}
.btn:focus-visible,
.btn-ghost:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.btn-ghost {
background: rgba(31, 41, 55, 0.6);
border: 1px solid var(--panel-border);
color: var(--text);
border-radius: 8px;
padding: 11px 16px;
font-family: var(--font-sans);
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.btn-ghost:hover {
background: rgba(55, 65, 81, 0.8);
}
.result-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 12px;
}
.result-card {
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 14px 16px;
}
.result-card .label {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 6px;
}
.result-card .value {
font-size: 16px;
font-weight: 600;
color: var(--text);
word-break: break-word;
}
.result-card .value.muted {
color: var(--text-dim);
font-weight: 400;
}
.raw-ua {
font-family: var(--font-mono);
font-size: 13px;
line-height: 1.7;
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 14px 16px;
word-break: break-all;
color: var(--text-muted);
}
.actions-secondary {
display: flex;
gap: 10px;
margin-top: 16px;
flex-wrap: wrap;
}
.empty-state {
color: var(--text-dim);
font-size: 14px;
text-align: center;
padding: 20px 0;
}
.footer-note {
text-align: center;
color: var(--text-dim);
font-size: 13px;
margin-top: 8px;
}
.footer-note a {
color: var(--text-muted);
}
.toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: var(--text);
color: var(--bg);
padding: 12px 22px;
border-radius: 99px;
font-family: var(--font-sans);
font-weight: 600;
font-size: 14px;
opacity: 0;
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
z-index: 100;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
@media (max-width: 640px) {
.studio {
padding: 20px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="hero">
<div class="eyebrow">Browser & Network</div>
<h1 class="wordmark">User Agent Parser</h1>
<p class="tagline">Paste any User-Agent string — or parse your current browser — to see the browser, engine, operating system, device type, and CPU architecture in plain, readable form.</p>
</header>
<main class="studio">
<label class="section-label" for="uaInput">User-Agent string</label>
<textarea id="uaInput" spellcheck="false" placeholder='Paste a User-Agent string here, or click "Use current browser".'></textarea>
<div class="actions">
<button class="btn" id="btnParse" type="button">Parse</button>
<button class="btn-ghost" id="btnCurrent" type="button">Use current browser</button>
<button class="btn-ghost" id="btnClear" type="button">Clear</button>
</div>
</main>
<main class="studio">
<span class="section-label">Parsed details</span>
<div class="result-grid" id="resultGrid" hidden></div>
<p class="empty-state" id="emptyState">Parse a User-Agent string to see the breakdown here.</p>
<div class="actions-secondary" id="copyActions" hidden>
<button class="btn-ghost" id="btnCopyText" type="button">Copy as text</button>
<button class="btn-ghost" id="btnCopyJson" type="button">Copy as JSON</button>
</div>
</main>
<main class="studio">
<span class="section-label">Raw User-Agent</span>
<div class="raw-ua" id="rawUa">—</div>
</main>
<p class="footer-note">Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a>. Parsing happens entirely in your browser — nothing is sent anywhere.</p>
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script>
/* ==========================================================
User Agent Parser
Vanilla JS, no dependencies. Pattern-based detection covering
the common browsers, engines, and platforms. UA strings are
inherently ambiguous, so results are best-effort.
========================================================== */
const uaInput = document.getElementById("uaInput");
const btnParse = document.getElementById("btnParse");
const btnCurrent = document.getElementById("btnCurrent");
const btnClear = document.getElementById("btnClear");
const resultGrid = document.getElementById("resultGrid");
const emptyState = document.getElementById("emptyState");
const copyActions = document.getElementById("copyActions");
const btnCopyText = document.getElementById("btnCopyText");
const btnCopyJson = document.getElementById("btnCopyJson");
const rawUa = document.getElementById("rawUa");
const toast = document.getElementById("toast");
// Holds the most recent parse result for copy actions.
let lastParsed = null;
/** Detects the browser name and version. Order matters — more specific first. */
function detectBrowser(ua) {
const checks = [
{ name: "Edge", re: /Edg(?:e|A|iOS)?\/([\d.]+)/ },
{ name: "Opera", re: /(?:OPR|Opera)\/([\d.]+)/ },
{ name: "Samsung Internet", re: /SamsungBrowser\/([\d.]+)/ },
{ name: "Vivaldi", re: /Vivaldi\/([\d.]+)/ },
{ name: "Brave", re: /Brave\/([\d.]+)/ },
{ name: "Chrome", re: /(?:Chrome|CriOS)\/([\d.]+)/ },
{ name: "Firefox", re: /(?:Firefox|FxiOS)\/([\d.]+)/ },
{ name: "Safari", re: /Version\/([\d.]+).*Safari/ },
{ name: "Internet Explorer", re: /(?:MSIE\s|rv:)([\d.]+).*Trident/ }
];
for (const check of checks) {
const match = ua.match(check.re);
if (match) return { name: check.name, version: match[1] };
}
return { name: "Unknown", version: "" };
}
/** Detects the rendering engine. */
function detectEngine(ua) {
if (/Trident/.test(ua)) return "Trident";
if (/Gecko\/|rv:.*Gecko/.test(ua) && /Firefox/.test(ua)) return "Gecko";
// EdgeHTML historically, but modern Edge is Blink.
if (/Edge\//.test(ua)) return "EdgeHTML";
if (/AppleWebKit/.test(ua)) {
// Blink-based browsers still report AppleWebKit but also include Chrome.
if (/Chrome|Chromium|CriOS|Edg\//.test(ua)) return "Blink";
return "WebKit";
}
if (/Presto/.test(ua)) return "Presto";
return "Unknown";
}
/** Detects OS name and version where possible. */
function detectOS(ua) {
let match;
if ((match = ua.match(/Windows NT ([\d.]+)/))) {
const map = {
"10.0": "10 / 11",
6.3: "8.1",
6.2: "8",
6.1: "7",
"6.0": "Vista",
5.1: "XP"
};
return { name: "Windows", version: map[match[1]] || match[1] };
}
if ((match = ua.match(/Mac OS X ([\d_]+)/))) {
return { name: "macOS", version: match[1].replace(/_/g, ".") };
}
if (/iPhone|iPad|iPod/.test(ua)) {
match = ua.match(/OS ([\d_]+)/);
return { name: "iOS", version: match ? match[1].replace(/_/g, ".") : "" };
}
if ((match = ua.match(/Android ([\d.]+)/))) {
return { name: "Android", version: match[1] };
}
if (/CrOS/.test(ua)) return { name: "ChromeOS", version: "" };
if (/Linux/.test(ua)) return { name: "Linux", version: "" };
return { name: "Unknown", version: "" };
}
/** Classifies device type from common signals. */
function detectDevice(ua) {
if (/bot|crawler|spider|crawling|Googlebot|bingbot|Slurp|DuckDuckBot/i.test(ua)) return "Bot / Crawler";
if (/SmartTV|SMART-TV|GoogleTV|AppleTV|CrKey|HbbTV/i.test(ua)) return "Smart TV";
if (/iPad|Tablet|Nexus 7|Nexus 10|SM-T/i.test(ua)) return "Tablet";
if (/Mobi|iPhone|iPod|Android.*Mobile|Windows Phone|BlackBerry/i.test(ua)) return "Mobile";
return "Desktop";
}
/** Detects CPU architecture when the UA advertises it. */
function detectArchitecture(ua) {
if (/x64|Win64|WOW64|x86_64|amd64/i.test(ua)) return "x86-64 (64-bit)";
if (/arm64|aarch64/i.test(ua)) return "ARM64";
if (/\barm\b/i.test(ua)) return "ARM";
if (/i686|i386|x86/i.test(ua)) return "x86 (32-bit)";
return "Not specified";
}
/** Runs all detectors and returns a normalized result object. */
function parseUserAgent(ua) {
const browser = detectBrowser(ua);
const os = detectOS(ua);
return {
browser: browser.name + (browser.version ? " " + browser.version : ""),
browserName: browser.name,
browserVersion: browser.version || "",
engine: detectEngine(ua),
os: os.name + (os.version ? " " + os.version : ""),
osName: os.name,
osVersion: os.version || "",
device: detectDevice(ua),
architecture: detectArchitecture(ua),
raw: ua
};
}
/** Renders the parsed result into the card grid. */
function renderResult(parsed) {
const cards = [
{ label: "Browser", value: parsed.browser },
{ label: "Rendering Engine", value: parsed.engine },
{ label: "Operating System", value: parsed.os },
{ label: "Device Type", value: parsed.device },
{ label: "CPU Architecture", value: parsed.architecture }
];
resultGrid.innerHTML = "";
cards.forEach((card) => {
const div = document.createElement("div");
div.className = "result-card";
const label = document.createElement("div");
label.className = "label";
label.textContent = card.label;
const value = document.createElement("div");
const isUnknown = /^Unknown|^Not specified/.test(card.value);
value.className = "value" + (isUnknown ? " muted" : "");
value.textContent = card.value;
div.appendChild(label);
div.appendChild(value);
resultGrid.appendChild(div);
});
resultGrid.hidden = false;
emptyState.hidden = true;
copyActions.hidden = false;
rawUa.textContent = parsed.raw;
}
/** Parses whatever is in the textarea and renders it. */
function parseCurrentInput() {
const ua = uaInput.value.trim();
if (!ua) {
uaInput.focus();
return;
}
lastParsed = parseUserAgent(ua);
renderResult(lastParsed);
}
/** Shows a brief toast message (also announced via aria-live). */
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => toast.classList.remove("show"), 2000);
}
/** Copies text to the clipboard with graceful failure. */
function copyText(text, successMessage) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
.writeText(text)
.then(() => showToast(successMessage))
.catch(() => showToast("Copy failed — select the text manually"));
} else {
showToast("Copy failed — clipboard unavailable");
}
}
btnParse.addEventListener("click", parseCurrentInput);
btnCurrent.addEventListener("click", () => {
uaInput.value = navigator.userAgent;
parseCurrentInput();
});
btnClear.addEventListener("click", () => {
uaInput.value = "";
lastParsed = null;
resultGrid.hidden = true;
resultGrid.innerHTML = "";
copyActions.hidden = true;
emptyState.hidden = false;
rawUa.textContent = "—";
uaInput.focus();
});
btnCopyText.addEventListener("click", () => {
if (!lastParsed) return;
const text = ["Browser: " + lastParsed.browser, "Rendering Engine: " + lastParsed.engine, "Operating System: " + lastParsed.os, "Device Type: " + lastParsed.device, "CPU Architecture: " + lastParsed.architecture, "", "Raw: " + lastParsed.raw].join("\n");
copyText(text, "Copied as text!");
});
btnCopyJson.addEventListener("click", () => {
if (!lastParsed) return;
copyText(JSON.stringify(lastParsed, null, 2), "Copied as JSON!");
});
// Support Enter (with modifier) to parse quickly from the textarea.
uaInput.addEventListener("keydown", (event) => {
if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
event.preventDefault();
parseCurrentInput();
}
});
</script>
</body>
</html>