-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
170 lines (149 loc) · 4.39 KB
/
agent.js
File metadata and controls
170 lines (149 loc) · 4.39 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
/* global UAParser */
class Agent {
#prefs = {}; // userAgentData, parser
deriveAppVersion(userAgent) {
// Keep appVersion structurally consistent with userAgent.
const mozillaMatch = userAgent.match(/^Mozilla\/([^\s]+)\s?(.*)$/);
if (mozillaMatch) {
const [, version, rest] = mozillaMatch;
return `${version}${rest ? ` ${rest}` : ''}`.trim();
}
const operaMatch = userAgent.match(/^Opera\/([^\s]+)\s?(.*)$/);
if (operaMatch) {
const [, version, rest] = operaMatch;
return `${version}${rest ? ` ${rest}` : ''}`.trim();
}
return userAgent;
}
prefs(prefs) {
this.#prefs = prefs;
}
linuxNavigatorPlatform(uaString, parsed) {
const fromUa = uaString.match(/\((?:[^)]*;\s*)?Linux\s+([^;)]+)\)/i);
if (fromUa) {
const tail = fromUa[1].trim();
if (tail) {
return tail.toLowerCase().startsWith('linux ') ? tail : `Linux ${tail}`;
}
}
const arch = (parsed.cpu && parsed.cpu.architecture) || '';
const map = {
amd64: 'x86_64',
x64: 'x86_64',
ia32: 'i686',
i686: 'i686',
arm: 'armv7l',
armhf: 'armv7l',
aarch64: 'aarch64',
arm64: 'aarch64'
};
const normalized = map[String(arch).toLowerCase()] || arch;
if (normalized) {
return `Linux ${normalized}`;
}
return 'Linux x86_64';
}
parse(s = '') {
if (this.#prefs.parser && this.#prefs.parser[s]) {
return Object.assign({
userAgent: s
}, this.#prefs.parser[s]);
}
// build ua string from the navigator object or from a custom UAParser;
// examples: ${platform}, ${browser.version|ua-parser}
s = s.replace(/\${([^}]+)}/g, (a, b) => {
const key = (parent, keys) => {
for (const key of keys) {
parent = parent[key] || {};
}
return parent;
};
let [childs, object] = b.split('|');
object = object || 'navigator';
let v;
if (object.startsWith('ua-parser')) {
const [a, b] = object.split('@');
object = a;
v = key((new UAParser(b || navigator.userAgent)).getResult(), childs.split('.'));
}
v = v || key(navigator, childs.split('.'));
return typeof v === 'string' ? v : 'cannot parse your ${...} replacements.';
});
const o = {};
o.userAgent = s;
o.appVersion = this.deriveAppVersion(s);
const isFF = /Firefox/.test(s);
const isCH = /Chrome/.test(s);
const isSF = /Safari/.test(s) && isCH === false;
const p = (new UAParser(s)).getResult();
// platform
if (p.os.name === 'Mac OS' || p.os.name === 'macOS') {
o.platform = 'MacIntel';
}
else if (p.os.name === 'Windows') {
o.platform = 'Win32';
}
else if (p.os.name === 'Linux') {
o.platform = this.linuxNavigatorPlatform(s, p);
}
else if (p.os.name === 'Android') {
if (p.cpu.architecture) {
o.platform = 'Linux ' + p.cpu.architecture;
}
else {
o.platform = 'Linux armv81';
}
}
else if (p.os.name === 'iOS') {
o.platform = p.device.model;
}
// backup plan
if (!o.platform) {
if (p.os.name === 'Linux') {
o.platform = this.linuxNavigatorPlatform(s, p);
}
else {
o.platform = (p.cpu.architecture ? ('Linux ' + p.cpu.architecture) : (p.os.name || ''));
}
}
o.vendor = p.device.vendor || '';
if (isSF) {
o.vendor = 'Apple Computer, Inc.';
}
else if (isFF === false) {
o.vendor = 'Google Inc.';
}
o.product = p.engine.name || '';
if (s.indexOf('Gecko') !== -1) {
o.product = 'Gecko';
}
// Set appName and appCodeName
o.appName = 'Netscape';
o.appCodeName = 'Mozilla';
o.userAgentData = '[delete]';
if (isFF) {
o.oscpu = ((p.os.name || '') + ' ' + (p.os.version || '')).trim();
o.productSub = '20100101';
o.buildID = '20181001000000';
}
else {
o.oscpu = '[delete]';
o.buildID = '[delete]';
o.productSub = '20030107';
if (this.#prefs.userAgentData && p.browser && p.browser.major) {
if (['Opera', 'Chrome', 'Edge'].includes(p.browser.name)) {
o.userAgentDataBuilder = {p, ua: s};
delete o.userAgentData;
}
}
}
if (o.userAgent === 'empty') {
Object.keys(o).forEach(key => {
if (key !== 'userAgent') {
o[key] = '';
}
});
}
return o;
}
}