Skip to content

Commit 996c04d

Browse files
committed
fix refactor by taking snapshot earlier
1 parent c2c5a46 commit 996c04d

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

src/McpPage.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ export class McpPage implements ContextPage {
253253
);
254254

255255
const elementHandles: ElementHandle[] = [];
256-
const cdpElementIds: string[] = [];
257256
for (let i = 0; i < (result.stashed ?? 0); i++) {
258257
const elementHandle = await this.pptrPage.evaluateHandle(index => {
259258
const el = window.__dtmcp?.stashedElements?.[index];
@@ -263,28 +262,32 @@ export class McpPage implements ContextPage {
263262
return el;
264263
}, i);
265264
elementHandles.push(elementHandle);
265+
}
266+
267+
if (elementHandles.length) {
268+
this.textSnapshot = await TextSnapshot.create(this, {
269+
extraHandles: elementHandles,
270+
});
271+
response.includeSnapshot();
272+
}
266273

274+
const cdpElementIds: string[] = [];
275+
// can this be mapped?
276+
for (const [index, elementHandle] of elementHandles.entries()) {
267277
const backendNodeId = await elementHandle.backendNodeId();
268278
if (!backendNodeId) {
269-
logger(`No backendNodeId for stashed DOM element with index ${i}`);
270-
cdpElementIds.push(`stashed-${i}`);
279+
logger(`No backendNodeId for stashed DOM element with index ${index}`);
280+
cdpElementIds.push(`stashed-${index}`);
271281
continue;
272282
}
273283
const cdpElementId = this.resolveCdpElementId(backendNodeId);
274284
if (!cdpElementId) {
275285
logger(`Could not get cdpElementId for backend node ${backendNodeId}`);
276-
cdpElementIds.push(`stashed-${i}`);
286+
cdpElementIds.push(`stashed-${index}`);
277287
continue;
278288
}
279289
cdpElementIds.push(cdpElementId);
280290
}
281-
const resultWithStashedElements = result.result;
282-
if (elementHandles.length) {
283-
this.textSnapshot = await TextSnapshot.create(this, {
284-
extraHandles: elementHandles,
285-
});
286-
response.includeSnapshot();
287-
}
288291

289292
const recursivelyReplaceStashedElements = (node: unknown): unknown => {
290293
if (Array.isArray(node)) {
@@ -309,9 +312,7 @@ export class McpPage implements ContextPage {
309312
return node;
310313
};
311314

312-
const resultWithUids = recursivelyReplaceStashedElements(
313-
resultWithStashedElements,
314-
);
315+
const resultWithUids = recursivelyReplaceStashedElements(result.result);
315316
response.appendResponseLine(JSON.stringify(resultWithUids, null, 2));
316317
}
317318

0 commit comments

Comments
 (0)