Skip to content
Closed
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
16 changes: 10 additions & 6 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type PageMessage = {
// selection happens.
tab?: chrome.tabs.Tab;
clientName?: string;
// False when the MCP server asked not to take focus on connect. Absent means true.
focusTab?: boolean;
} | {
type: 'getConnectionStatus';
} | {
Expand Down Expand Up @@ -74,7 +76,7 @@ class PlaywrightExtension {
// sender.tab and UI-supplied tabs come from chrome.tabs.query / runtime
// message sender, where `id` is always defined.
const selectedTab = (message.tab ?? sender.tab!) as chrome.tabs.Tab & { id: number };
this._connectTab(sender.tab!.id!, selectedTab, message.clientName).then(
this._connectTab(sender.tab!.id!, selectedTab, message.clientName, message.focusTab !== false).then(
() => sendResponse({ success: true }),
(error: any) => sendResponse({ success: false, error: error.message }));
return true; // Return true to indicate that the response will be sent asynchronously
Expand All @@ -99,7 +101,7 @@ class PlaywrightExtension {
}
}

private async _connectTab(selectorTabId: number, tab: chrome.tabs.Tab & { id: number }, clientName: string | undefined): Promise<void> {
private async _connectTab(selectorTabId: number, tab: chrome.tabs.Tab & { id: number }, clientName: string | undefined, focusTab: boolean): Promise<void> {
try {
await this._cleanupPromise;
this._releaseTab(selectorTabId);
Expand All @@ -116,10 +118,12 @@ class PlaywrightExtension {
group.onclose = () => this._connections.delete(id);
this._connections.set(id, group);

await Promise.all([
chrome.tabs.update(tab.id, { active: true }),
chrome.windows.update(tab.windowId, { focused: true }),
]).catch(() => {});
if (focusTab) {
await Promise.all([
chrome.tabs.update(tab.id, { active: true }),
chrome.windows.update(tab.windowId, { focused: true }),
]).catch(() => {});
}

if (tab.id !== selectorTabId)
await chrome.tabs.remove(selectorTabId).catch(() => {});
Expand Down
6 changes: 4 additions & 2 deletions packages/extension/src/ui/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ const ConnectApp: React.FC = () => {
window.history.replaceState(null, '', url);
}
if (token === expectedToken) {
await handleConnectToTab();
// Token bypass: no user picked a tab, so do not steal focus.
await handleConnectToTab(undefined, false);
return;
}
if (token) {
Expand Down Expand Up @@ -134,14 +135,15 @@ const ConnectApp: React.FC = () => {
setStatus({ type: 'error', message: 'Failed to load tabs: ' + response.error });
}, []);

const handleConnectToTab = useCallback(async (tab?: chrome.tabs.Tab) => {
const handleConnectToTab = useCallback(async (tab?: chrome.tabs.Tab, focusTab = true) => {
setShowTabList(false);

try {
const response = await chrome.runtime.sendMessage({
type: 'connectToTab',
tab,
clientName: clientInfo,
focusTab,
});

if (response?.success) {
Expand Down