Skip to content

Commit 8cb4d6f

Browse files
committed
feat(desktop): configure the preview User-Agent per profile
1 parent f4f148e commit 8cb4d6f

5 files changed

Lines changed: 128 additions & 7 deletions

File tree

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const clientSettings: ClientSettings = {
2727
browserRecordingFrameRate: 60,
2828
browserLinkTarget: "app",
2929
browserAutoShowFloatingPreview: false,
30-
browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
30+
browserProfiles: [{ id: "work", name: "Work", kind: "persistent", userAgentMode: "chrome" }],
3131
browserDefaultProfileId: "work",
3232
confirmQuit: "double-click",
3333
confirmThreadArchive: true,

‎apps/web/src/browser/HostedBrowserWebview.tsx‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
"use client";
22

3-
import type {
4-
DesktopPreviewColorScheme,
5-
PreviewViewportSetting,
6-
ScopedThreadRef,
3+
import {
4+
browserProfileUserAgent,
5+
findBrowserProfile,
6+
resolveBrowserProfiles,
7+
type DesktopPreviewColorScheme,
8+
type PreviewViewportSetting,
9+
type ScopedThreadRef,
710
} from "@t3tools/contracts";
811
import { useShallow } from "zustand/react/shallow";
912
import { useCallback, useEffect, useRef, useState } from "react";
1013

1114
import { previewBridge } from "~/components/preview/previewBridge";
1215
import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
13-
import { useClientSettingsHydrated } from "~/hooks/useSettings";
16+
import { useClientSettings, useClientSettingsHydrated } from "~/hooks/useSettings";
1417
import { cn, isMacPlatform } from "~/lib/utils";
1518

1619
import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
@@ -84,6 +87,11 @@ export function HostedBrowserWebview(props: {
8487
serverRendering,
8588
} = props;
8689
const clientSettingsHydrated = useClientSettingsHydrated();
90+
const userAgentMode = useClientSettings(
91+
(settings) =>
92+
findBrowserProfile(resolveBrowserProfiles(settings.browserProfiles), profileId)
93+
?.userAgentMode,
94+
);
8795
const config = usePreviewWebviewConfig(threadRef.environmentId, profileId);
8896
const [initialSrc] = useState(() => initialUrl ?? "about:blank");
8997
const tabLeaseRef = useRef<AcquiredDesktopTab | null>(null);
@@ -342,6 +350,8 @@ export function HostedBrowserWebview(props: {
342350
{...({ allowpopups: "true" } as unknown as { readonly allowpopups?: boolean })}
343351
src={webviewGeneration === 0 ? initialSrc : recoverySrc}
344352
partition={config.partition}
353+
// Electron reads the identity at attach; reopening applies profile changes.
354+
useragent={browserProfileUserAgent(navigator.userAgent, userAgentMode)}
345355
webpreferences={config.webPreferences}
346356
{...(config.preloadUrl ? { preload: config.preloadUrl } : {})}
347357
data-preview-tab={runtimeTabId}

‎apps/web/src/components/settings/IntegrationsSettings.tsx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
BROWSER_PROFILE_MAX_COUNT,
1717
type BrowserLinkTarget,
1818
type BrowserProfile,
19+
type BrowserProfileUserAgentMode,
1920
type EnvironmentId,
2021
BROWSER_PROFILE_NAME_MAX_LENGTH,
2122
BROWSER_RECORDING_FRAME_RATES,
@@ -70,7 +71,13 @@ import {
7071
MenuGroupLabel,
7172
MenuItem,
7273
MenuPopup,
74+
MenuRadioGroup,
75+
MenuRadioItem,
76+
MenuRadioItemIndicator,
7377
MenuSeparator,
78+
MenuSub,
79+
MenuSubPopup,
80+
MenuSubTrigger,
7481
MenuTrigger,
7582
} from "../ui/menu";
7683
import { readLocalApi } from "~/localApi";
@@ -1005,6 +1012,15 @@ function BrowserProfilesSetting({ disabled }: { readonly disabled: boolean }) {
10051012
});
10061013
};
10071014

1015+
const setProfileUserAgent = (id: string, userAgentMode: BrowserProfileUserAgentMode) => {
1016+
if (!settingsHydrated || importInFlightRef.current) return;
1017+
updateSettings({
1018+
browserProfiles: getClientSettings().browserProfiles.map((profile) =>
1019+
profile.id === id ? { ...profile, userAgentMode } : profile,
1020+
),
1021+
});
1022+
};
1023+
10081024
const clearProfileData = (id: string, name: string) => {
10091025
if (!settingsHydrated || importInFlightRef.current) return;
10101026
if (!removalAvailable) {
@@ -1341,6 +1357,37 @@ function BrowserProfilesSetting({ disabled }: { readonly disabled: boolean }) {
13411357
<MoreVertical />
13421358
</MenuTrigger>
13431359
<MenuPopup align="end">
1360+
{!builtIn && previewBridge ? (
1361+
<MenuSub>
1362+
<MenuSubTrigger>User-Agent</MenuSubTrigger>
1363+
<MenuSubPopup>
1364+
<MenuGroup>
1365+
<MenuGroupLabel>Reopen tabs to apply</MenuGroupLabel>
1366+
<MenuRadioGroup
1367+
value={profile.userAgentMode ?? "native"}
1368+
onValueChange={(value) =>
1369+
setProfileUserAgent(profile.id, value as BrowserProfileUserAgentMode)
1370+
}
1371+
>
1372+
<MenuRadioItem value="native">
1373+
<span className="flex items-center gap-2">
1374+
<span className="flex-1">Native (default)</span>
1375+
<MenuRadioItemIndicator />
1376+
</span>
1377+
</MenuRadioItem>
1378+
<MenuRadioItem value="chrome">
1379+
<span className="flex items-center gap-2">
1380+
<span className="flex-1">Chrome compatibility</span>
1381+
<MenuRadioItemIndicator />
1382+
</span>
1383+
</MenuRadioItem>
1384+
</MenuRadioGroup>
1385+
<MenuSeparator />
1386+
<MenuItem disabled>Some sign-in pages require Native</MenuItem>
1387+
</MenuGroup>
1388+
</MenuSubPopup>
1389+
</MenuSub>
1390+
) : null}
13441391
<MenuItem
13451392
disabled={!settingsHydrated || isDefault}
13461393
onClick={() => {

‎packages/contracts/src/browserProfile.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
findBrowserProfile,
1111
isBuiltInBrowserProfileId,
1212
resolveBrowserProfiles,
13-
type BrowserProfile,
13+
browserProfileUserAgent,
14+
BrowserProfile,
1415
} from "./browserProfile.ts";
1516

1617
const work: BrowserProfile = { id: "profile-work", name: "Work", kind: "persistent" };
18+
const isBrowserProfile = Schema.is(BrowserProfile);
1719

1820
describe("resolveBrowserProfiles", () => {
1921
it("lists built-ins ahead of the user's own profiles", () => {
@@ -109,3 +111,44 @@ describe("BrowserProfileId", () => {
109111
expect(Schema.is(BrowserProfileId)("profile-a")).toBe(true);
110112
});
111113
});
114+
115+
describe("browser profile User-Agent", () => {
116+
const native =
117+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) T3Code(Nightly)/0.0.46 Chrome/152.0.7977.130 Electron/44.4.2 Safari/537.36";
118+
const chrome =
119+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.7977.130 Safari/537.36";
120+
121+
it("leaves existing profiles and built-in identities untouched", () => {
122+
expect(browserProfileUserAgent(native, undefined)).toBeUndefined();
123+
const profiles = resolveBrowserProfiles([
124+
{ id: "default", name: "Override", kind: "persistent", userAgentMode: "chrome" },
125+
{ id: "teams", name: "Teams", kind: "persistent", userAgentMode: "chrome" },
126+
]);
127+
expect(profiles[0]?.userAgentMode).toBeUndefined();
128+
expect(profiles.find((profile) => profile.id === "teams")?.userAgentMode).toBe("chrome");
129+
});
130+
131+
it("uses the running Chromium version and restores the exact native identity", () => {
132+
expect(browserProfileUserAgent(native, "chrome")).toBe(chrome);
133+
expect(browserProfileUserAgent(native, "native")).toBe(native);
134+
});
135+
136+
it.each([
137+
"X11; Linux x86_64",
138+
"Windows NT 10.0; Win64; x64",
139+
"Macintosh; Intel Mac OS X 10_15_7",
140+
])("preserves the platform and engine tokens on %s", (platform) => {
141+
const agent = native.replace("Macintosh; Intel Mac OS X 10_15_7", platform);
142+
expect(browserProfileUserAgent(agent, "chrome")).toBe(
143+
chrome.replace("Macintosh; Intel Mac OS X 10_15_7", platform),
144+
);
145+
});
146+
147+
it("accepts only the supported modes while retaining old saved profiles", () => {
148+
const profile = { id: "teams", name: "Teams", kind: "persistent" };
149+
expect(isBrowserProfile(profile)).toBe(true);
150+
expect(isBrowserProfile({ ...profile, userAgentMode: "chrome" })).toBe(true);
151+
expect(isBrowserProfile({ ...profile, userAgentMode: "native" })).toBe(true);
152+
expect(isBrowserProfile({ ...profile, userAgentMode: "firefox" })).toBe(false);
153+
});
154+
});

‎packages/contracts/src/browserProfile.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ export const BrowserProfileName = TrimmedNonEmptyString.check(
4141
export const BrowserProfileKind = Schema.Literals(["persistent", "incognito"]);
4242
export type BrowserProfileKind = typeof BrowserProfileKind.Type;
4343

44+
export const BrowserProfileUserAgentMode = Schema.Literals(["native", "chrome"]);
45+
export type BrowserProfileUserAgentMode = typeof BrowserProfileUserAgentMode.Type;
46+
4447
export const BrowserProfile = Schema.Struct({
4548
id: BrowserProfileId,
4649
name: BrowserProfileName,
4750
kind: BrowserProfileKind,
51+
/** Optional compatibility override for this profile's Electron guests. */
52+
userAgentMode: Schema.optionalKey(BrowserProfileUserAgentMode),
4853
});
4954
export type BrowserProfile = typeof BrowserProfile.Type;
5055

@@ -97,3 +102,19 @@ export function findBrowserProfile(
97102
): BrowserProfile | undefined {
98103
return id === undefined ? undefined : profiles.find((profile) => profile.id === id);
99104
}
105+
106+
/**
107+
* Omitted modes keep Electron's native identity untouched (#5002). An explicit
108+
* native choice restores the exact string after a Chrome override. The Chrome
109+
* variant retains the running engine and platform; it only removes app tokens.
110+
*/
111+
export function browserProfileUserAgent(
112+
nativeUserAgent: string,
113+
mode: BrowserProfileUserAgentMode | undefined,
114+
): string | undefined {
115+
if (mode === undefined) return undefined;
116+
if (mode === "native") return nativeUserAgent;
117+
return nativeUserAgent
118+
.replace(/( \(KHTML, like Gecko\)) .*?( Chrome\/\S+)/, "$1$2")
119+
.replace(/ Electron\/\S+/, "");
120+
}

0 commit comments

Comments
 (0)