Skip to content

Commit b53752d

Browse files
authored
chore: move resolveCdpElementId (#1923)
This is a refactoring, a follow-up to the introduction of `McpPage.ts`.
1 parent f0da776 commit b53752d

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
@@ -178,33 +178,6 @@ export class McpContext implements Context {
178178
return this.#networkCollector.getIdForResource(request);
179179
}
180180

181-
resolveCdpElementId(
182-
page: McpPage,
183-
cdpBackendNodeId: number,
184-
): string | undefined {
185-
if (!cdpBackendNodeId) {
186-
this.logger('no cdpBackendNodeId');
187-
return;
188-
}
189-
const snapshot = page.textSnapshot;
190-
if (!snapshot) {
191-
this.logger('no text snapshot');
192-
return;
193-
}
194-
// TODO: index by backendNodeId instead.
195-
const queue = [snapshot.root];
196-
while (queue.length) {
197-
const current = queue.pop()!;
198-
if (current.backendNodeId === cdpBackendNodeId) {
199-
return current.id;
200-
}
201-
for (const child of current.children) {
202-
queue.push(child);
203-
}
204-
}
205-
return;
206-
}
207-
208181
getNetworkRequests(
209182
page: McpPage,
210183
includePreservedRequests?: boolean,
@@ -782,8 +755,7 @@ export class McpContext implements Context {
782755
const data = devtoolsData ?? (await this.getDevToolsData(page));
783756
if (data?.cdpBackendNodeId) {
784757
snapshot.hasSelectedElement = true;
785-
snapshot.selectedElementUid = this.resolveCdpElementId(
786-
page,
758+
snapshot.selectedElementUid = page.resolveCdpElementId(
787759
data?.cdpBackendNodeId,
788760
);
789761
}

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)