aboutsummaryrefslogtreecommitdiffstats
path: root/packages/service-inject/src/shims/userAgentData.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/service-inject/src/shims/userAgentData.ts')
-rw-r--r--packages/service-inject/src/shims/userAgentData.ts102
1 files changed, 102 insertions, 0 deletions
diff --git a/packages/service-inject/src/shims/userAgentData.ts b/packages/service-inject/src/shims/userAgentData.ts
new file mode 100644
index 0000000..be43823
--- /dev/null
+++ b/packages/service-inject/src/shims/userAgentData.ts
@@ -0,0 +1,102 @@
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
21import {
22 defineProtoProperty,
23 deleteProtoProperty,
24 simulateNativeClass,
25 simulateNativeFunction,
26} from '../utils';
27
28export function shimUserAgentData(): void {
29 const brands = [
30 {
31 brand: ' Not A; Brand',
32 version: '99',
33 },
34 {
35 brand: 'Chromium',
36 version: '96',
37 }
38 ];
39 const mobile = false;
40 const platform = 'Linux';
41
42 const simulatedNavigatorUa = simulateNativeClass('NavigatorUAData', function NavigatorUAData() {
43 // Nothing to initiailize.
44 }, {
45 brands: {
46 configurable: true,
47 enumerable: true,
48 get: simulateNativeFunction('brands', () => brands),
49 },
50 mobile: {
51 configurable: true,
52 enumerable: true,
53 get: simulateNativeFunction('mobile', () => mobile),
54 },
55 platform: {
56 configurable: true,
57 enumerable: true,
58 get: simulateNativeFunction('platform', () => platform),
59 },
60 getHighEntropyValues: {
61 configurable: true,
62 enumerable: false,
63 value: simulateNativeFunction('getHighEntropyValues', (...args: unknown[]) => {
64 if (args.length == 0) {
65 throw new TypeError("Failed to execute 'getHighEntropyValues' on 'NavigatorUAData': 1 argument required, but only 0 present.");
66 }
67 const hints = Array.from(args[0] as Iterable<string>);
68 if (hints.length === 0) {
69 return {};
70 }
71 const data: Record<string, unknown> = {
72 brands,
73 mobile,
74 }
75 if (hints.includes('platform')) {
76 data['platform'] = platform;
77 }
78 return Promise.resolve(data);
79 })
80 },
81 toJSON: {
82 configurable: true,
83 enumerable: false,
84 value: simulateNativeFunction('toJSON', () => ({
85 brands,
86 mobile,
87 })),
88 writable: false,
89 },
90 });
91
92 const simulatedUserAgentData = Reflect.construct(simulatedNavigatorUa, []);
93 defineProtoProperty(window.navigator, 'userAgentData', {
94 configurable: true,
95 enumerable: true,
96 get: simulateNativeFunction('userAgentData', () => simulatedUserAgentData),
97 });
98}
99
100export function deleteUserAgentData(): void {
101 deleteProtoProperty(window.navigator, 'userAgentData');
102}