Skip to content
Merged
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
30 changes: 1 addition & 29 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,33 +178,6 @@ export class McpContext implements Context {
return this.#networkCollector.getIdForResource(request);
}

resolveCdpElementId(
page: McpPage,
cdpBackendNodeId: number,
): string | undefined {
if (!cdpBackendNodeId) {
this.logger('no cdpBackendNodeId');
return;
}
const snapshot = page.textSnapshot;
if (!snapshot) {
this.logger('no text snapshot');
return;
}
// TODO: index by backendNodeId instead.
const queue = [snapshot.root];
while (queue.length) {
const current = queue.pop()!;
if (current.backendNodeId === cdpBackendNodeId) {
return current.id;
}
for (const child of current.children) {
queue.push(child);
}
}
return;
}

getNetworkRequests(
page: McpPage,
includePreservedRequests?: boolean,
Expand Down Expand Up @@ -782,8 +755,7 @@ export class McpContext implements Context {
const data = devtoolsData ?? (await this.getDevToolsData(page));
if (data?.cdpBackendNodeId) {
snapshot.hasSelectedElement = true;
snapshot.selectedElementUid = this.resolveCdpElementId(
page,
snapshot.selectedElementUid = page.resolveCdpElementId(
data?.cdpBackendNodeId,
);
}
Expand Down
25 changes: 25 additions & 0 deletions src/McpPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {logger} from './logger.js';
import type {
Dialog,
ElementHandle,
Expand Down Expand Up @@ -164,4 +165,28 @@ export class McpPage implements ContextPage {
getAXNodeByUid(uid: string) {
return this.textSnapshot?.idToNode.get(uid);
}

resolveCdpElementId(cdpBackendNodeId: number): string | undefined {
if (!cdpBackendNodeId) {
logger('no cdpBackendNodeId');
return;
}
const snapshot = this.textSnapshot;
if (!snapshot) {
logger('no text snapshot');
return;
}
// TODO: index by backendNodeId instead.
const queue = [snapshot.root];
while (queue.length) {
const current = queue.pop()!;
if (current.backendNodeId === cdpBackendNodeId) {
return current.id;
}
for (const child of current.children) {
queue.push(child);
}
}
return;
}
}
5 changes: 1 addition & 4 deletions src/McpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,7 @@ export class McpResponse implements Response {
context,
this.#page,
),
elementIdResolver: context.resolveCdpElementId.bind(
context,
this.#page,
),
elementIdResolver: this.#page.resolveCdpElementId.bind(this.#page),
});
if (!formatter.isValid()) {
throw new Error(
Expand Down
Loading