Skip to content

Commit 975b286

Browse files
committed
move resolveCdpElementId
1 parent da33cb5 commit 975b286

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

src/McpContext.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -181,33 +181,6 @@ export class McpContext implements Context {
181181
return this.#networkCollector.getIdForResource(request);
182182
}
183183

184-
resolveCdpElementId(
185-
page: McpPage,
186-
cdpBackendNodeId: number,
187-
): string | undefined {
188-
if (!cdpBackendNodeId) {
189-
this.logger('no cdpBackendNodeId');
190-
return;
191-
}
192-
const snapshot = page.textSnapshot;
193-
if (!snapshot) {
194-
this.logger('no text snapshot');
195-
return;
196-
}
197-
// TODO: index by backendNodeId instead.
198-
const queue = [snapshot.root];
199-
while (queue.length) {
200-
const current = queue.pop()!;
201-
if (current.backendNodeId === cdpBackendNodeId) {
202-
return current.id;
203-
}
204-
for (const child of current.children) {
205-
queue.push(child);
206-
}
207-
}
208-
return;
209-
}
210-
211184
getNetworkRequests(
212185
page: McpPage,
213186
includePreservedRequests?: boolean,
@@ -785,8 +758,7 @@ export class McpContext implements Context {
785758
const data = devtoolsData ?? (await this.getDevToolsData(page));
786759
if (data?.cdpBackendNodeId) {
787760
snapshot.hasSelectedElement = true;
788-
snapshot.selectedElementUid = this.resolveCdpElementId(
789-
page,
761+
snapshot.selectedElementUid = page.resolveCdpElementId(
790762
data?.cdpBackendNodeId,
791763
);
792764
}

src/McpPage.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import {logger} from './logger.js';
78
import type {
89
Dialog,
910
ElementHandle,
@@ -164,4 +165,28 @@ export class McpPage implements ContextPage {
164165
getAXNodeByUid(uid: string) {
165166
return this.textSnapshot?.idToNode.get(uid);
166167
}
168+
169+
resolveCdpElementId(cdpBackendNodeId: number): string | undefined {
170+
if (!cdpBackendNodeId) {
171+
logger('no cdpBackendNodeId');
172+
return;
173+
}
174+
const snapshot = this.textSnapshot;
175+
if (!snapshot) {
176+
logger('no text snapshot');
177+
return;
178+
}
179+
// TODO: index by backendNodeId instead.
180+
const queue = [snapshot.root];
181+
while (queue.length) {
182+
const current = queue.pop()!;
183+
if (current.backendNodeId === cdpBackendNodeId) {
184+
return current.id;
185+
}
186+
for (const child of current.children) {
187+
queue.push(child);
188+
}
189+
}
190+
return;
191+
}
167192
}

src/McpResponse.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,7 @@ export class McpResponse implements Response {
513513
context,
514514
this.#page,
515515
),
516-
elementIdResolver: context.resolveCdpElementId.bind(
517-
context,
518-
this.#page,
519-
),
516+
elementIdResolver: this.#page.resolveCdpElementId.bind(this.#page),
520517
});
521518
if (!formatter.isValid()) {
522519
throw new Error(

0 commit comments

Comments
 (0)