-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathagent.js
More file actions
141 lines (126 loc) · 3.58 KB
/
Copy pathagent.js
File metadata and controls
141 lines (126 loc) · 3.58 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
/* global UAParser */
class Agent {
#prefs = {}; // userAgentData, parser
async prefs(ps) {
const dps = await chrome.storage.local.get(ps || {
'mode': 'blacklist',
'ua': '',
'blacklist': [],
'whitelist': [],
'custom': {},
'parser': {},
'protected': [
'google.com/recaptcha',
'gstatic.com/recaptcha',
'accounts.google.com',
'accounts.youtube.com',
'gitlab.com/users/sign_in',
'challenges.cloudflare.com'
],
'userAgentData': true
});
this.#prefs = dps;
return dps;
}
parse(s = '') {
// log('ua.parse is called', s);
if (this.#prefs.parser[s]) {
// log('ua.parse is resolved using parser');
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 = s
.replace(/^Mozilla\//, '')
.replace(/^Opera\//, '');
const isFF = /Firefox/.test(s);
const isCH = /Chrome/.test(s);
const isSF = /Safari/.test(s) && isCH === false;
if (isFF) {
o.appVersion = '5.0 ' + o.appVersion.replace('5.0 ', '').split(/[\s;]/)[0] + ')';
}
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 = o.oscpu;
}
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
o.platform = 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';
}
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;
}
}