MCPcopy Index your code
hub / github.com/Dimillian/CodexMonitor / withNavigatorValues

Function withNavigatorValues

src/utils/platformPaths.test.ts:6–60  ·  view source on GitHub ↗
(
  values: Partial<Pick<Navigator, "platform" | "userAgent" | "maxTouchPoints">>,
  run: () => void,
)

Source from the content-addressed store, hash-verified

4const globalScope = globalThis as typeof globalThis & { navigator?: Navigator };
5
6function withNavigatorValues(
7 values: Partial<Pick<Navigator, "platform" | "userAgent" | "maxTouchPoints">>,
8 run: () => void,
9) {
10 const hadNavigator = typeof globalScope.navigator !== "undefined";
11 if (!hadNavigator) {
12 Object.defineProperty(globalScope, "navigator", {
13 configurable: true,
14 writable: true,
15 value: {},
16 });
17 }
18
19 const activeNavigator = globalScope.navigator as Navigator;
20 const originalPlatform = Object.getOwnPropertyDescriptor(activeNavigator, "platform");
21 const originalUserAgent = Object.getOwnPropertyDescriptor(activeNavigator, "userAgent");
22 const originalMaxTouchPoints = Object.getOwnPropertyDescriptor(
23 activeNavigator,
24 "maxTouchPoints",
25 );
26 Object.defineProperty(activeNavigator, "platform", {
27 configurable: true,
28 value: values.platform ?? activeNavigator.platform ?? "",
29 });
30 Object.defineProperty(activeNavigator, "userAgent", {
31 configurable: true,
32 value: values.userAgent ?? activeNavigator.userAgent ?? "",
33 });
34 Object.defineProperty(activeNavigator, "maxTouchPoints", {
35 configurable: true,
36 value: values.maxTouchPoints ?? activeNavigator.maxTouchPoints ?? 0,
37 });
38 try {
39 run();
40 } finally {
41 if (originalPlatform) {
42 Object.defineProperty(activeNavigator, "platform", originalPlatform);
43 } else {
44 delete (activeNavigator as { platform?: string }).platform;
45 }
46 if (originalUserAgent) {
47 Object.defineProperty(activeNavigator, "userAgent", originalUserAgent);
48 } else {
49 delete (activeNavigator as { userAgent?: string }).userAgent;
50 }
51 if (originalMaxTouchPoints) {
52 Object.defineProperty(activeNavigator, "maxTouchPoints", originalMaxTouchPoints);
53 } else {
54 delete (activeNavigator as { maxTouchPoints?: number }).maxTouchPoints;
55 }
56 if (!hadNavigator) {
57 Reflect.deleteProperty(globalScope, "navigator");
58 }
59 }
60}
61
62describe("isMobilePlatform", () => {
63 it("returns true for iPhone-like user agents", () => {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected