Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions packages/extension/src/ui/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ type Status =

const SUPPORTED_PROTOCOL_VERSION = 2;

// Client name comes from the URL and never changes for the lifetime of this page.
const clientInfo = (() => {
try {
return JSON.parse(new URLSearchParams(window.location.search).get('client') || '{}').name || 'unknown';
} catch {
return 'unknown';
}
})();

const ConnectApp: React.FC = () => {
const [tabs, setTabs] = useState<chrome.tabs.Tab[]>([]);
const [status, setStatus] = useState<Status | null>(null);
const [showTabList, setShowTabList] = useState(true);
const [clientInfo, setClientInfo] = useState('unknown');

const setError = useCallback((message: string) => {
setShowTabList(false);
Expand Down Expand Up @@ -59,18 +67,10 @@ const ConnectApp: React.FC = () => {
return;
}

try {
const client = JSON.parse(params.get('client') || '{}');
const info = `${client.name || 'unknown'}`;
setClientInfo(info);
setStatus({
type: 'connecting',
message: `"${info}" is trying to connect to the Playwright Extension.`
});
} catch (e) {
setStatus({ type: 'error', message: 'Failed to parse client version.' });
return;
}
setStatus({
type: 'connecting',
message: `"${clientInfo}" is trying to connect to the Playwright Extension.`
});

const parsedVersion = parseInt(params.get('protocolVersion') ?? '', 10);
const requestedVersion = isNaN(parsedVersion) ? 1 : parsedVersion;
Expand Down Expand Up @@ -152,7 +152,7 @@ const ConnectApp: React.FC = () => {
message: `"${clientInfo}" failed to connect: ${e}`
});
}
}, [clientInfo]);
}, []);

useEffect(() => {
const listener = (message: any) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/extension/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ test(`bypass connection dialog with token`, async ({ browserWithExtension, start
const token = await page.locator('.auth-token-code').textContent();
const [, value] = token?.split('=') || [];

const clientName = 'token-bypass-client';
const { client } = await startClient({
clientName,
args: [`--extension`],
env: {
PLAYWRIGHT_MCP_EXTENSION_TOKEN: value,
Expand All @@ -344,6 +346,9 @@ test(`bypass connection dialog with token`, async ({ browserWithExtension, start
expect(await navigateResponse).toHaveResponse({
snapshot: expect.stringContaining(`- generic [active] [ref=f1e1]: Hello, world!`),
});

await page.goto(`chrome-extension://${extensionId}/status.html`);
await expect(page.locator('.client-info')).toContainText(`Connected to "${clientName}"`);
});

test(`pending connection closed when client disconnects`, async ({ startExtensionClient, server, protocolVersion }) => {
Expand Down