diff options
Diffstat (limited to 'packages/service-inject/src/shims/userAgentData.ts')
-rw-r--r-- | packages/service-inject/src/shims/userAgentData.ts | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/packages/service-inject/src/shims/userAgentData.ts b/packages/service-inject/src/shims/userAgentData.ts deleted file mode 100644 index 7e2c825..0000000 --- a/packages/service-inject/src/shims/userAgentData.ts +++ /dev/null | |||
@@ -1,103 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import { | ||
22 | defineProtoProperty, | ||
23 | deleteProtoProperty, | ||
24 | simulateNativeClass, | ||
25 | simulateNativeFunction, | ||
26 | } from '../utils'; | ||
27 | |||
28 | export function shimUserAgentData(chromeVersion: string | null, platform: string): void { | ||
29 | const brands = [ | ||
30 | { | ||
31 | brand: ' Not A; Brand', | ||
32 | version: '99', | ||
33 | }, | ||
34 | ]; | ||
35 | if (chromeVersion !== null) { | ||
36 | brands.push({ | ||
37 | brand: 'Chromium', | ||
38 | version: '96', | ||
39 | }); | ||
40 | } | ||
41 | const mobile = false; | ||
42 | |||
43 | const simulatedNavigatorUa = simulateNativeClass('NavigatorUAData', function NavigatorUAData() { | ||
44 | // Nothing to initiailize. | ||
45 | }, { | ||
46 | brands: { | ||
47 | configurable: true, | ||
48 | enumerable: true, | ||
49 | get: simulateNativeFunction('brands', () => brands), | ||
50 | }, | ||
51 | mobile: { | ||
52 | configurable: true, | ||
53 | enumerable: true, | ||
54 | get: simulateNativeFunction('mobile', () => mobile), | ||
55 | }, | ||
56 | platform: { | ||
57 | configurable: true, | ||
58 | enumerable: true, | ||
59 | get: simulateNativeFunction('platform', () => platform), | ||
60 | }, | ||
61 | getHighEntropyValues: { | ||
62 | configurable: true, | ||
63 | enumerable: false, | ||
64 | value: simulateNativeFunction('getHighEntropyValues', (...args: unknown[]) => { | ||
65 | if (args.length == 0) { | ||
66 | throw new TypeError("Failed to execute 'getHighEntropyValues' on 'NavigatorUAData': 1 argument required, but only 0 present."); | ||
67 | } | ||
68 | const hints = Array.from(args[0] as Iterable<string>); | ||
69 | if (hints.length === 0) { | ||
70 | return {}; | ||
71 | } | ||
72 | const data: Record<string, unknown> = { | ||
73 | brands, | ||
74 | mobile, | ||
75 | } | ||
76 | if (hints.includes('platform')) { | ||
77 | data['platform'] = platform; | ||
78 | } | ||
79 | return Promise.resolve(data); | ||
80 | }) | ||
81 | }, | ||
82 | toJSON: { | ||
83 | configurable: true, | ||
84 | enumerable: false, | ||
85 | value: simulateNativeFunction('toJSON', () => ({ | ||
86 | brands, | ||
87 | mobile, | ||
88 | })), | ||
89 | writable: false, | ||
90 | }, | ||
91 | }); | ||
92 | |||
93 | const simulatedUserAgentData = Reflect.construct(simulatedNavigatorUa, []); | ||
94 | defineProtoProperty(globalThis.navigator, 'userAgentData', { | ||
95 | configurable: true, | ||
96 | enumerable: true, | ||
97 | get: simulateNativeFunction('userAgentData', () => simulatedUserAgentData), | ||
98 | }); | ||
99 | } | ||
100 | |||
101 | export function deleteUserAgentData(): void { | ||
102 | deleteProtoProperty(globalThis.navigator, 'userAgentData'); | ||
103 | } | ||