Skip to content

Commit 1d859b2

Browse files
committed
Capture iframes when retrieving page snapshot through agent-server API
1 parent f4b29ff commit 1d859b2

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

agent-server/nodejs/src/lib/BrowserAgentServer.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,13 +1188,35 @@ export class BrowserAgentServer extends EventEmitter {
11881188
// Capture HTML from each frame
11891189
const htmlParts = [];
11901190

1191-
for (const frame of frames) {
1191+
for (let i = 0; i < frames.length; i++) {
1192+
const frame = frames[i];
11921193
try {
1193-
const result = await this.sendCDPCommandToTarget(tabId, 'Runtime.evaluate', {
1194-
expression: 'document.documentElement.outerHTML',
1195-
returnByValue: true,
1196-
contextId: frame.id // Execute in specific frame context
1197-
});
1194+
// For the main frame (first frame), use Runtime.evaluate without frameId
1195+
// For child frames, we need to create an execution context in that frame
1196+
1197+
let result;
1198+
if (i === 0) {
1199+
// Main frame - simple evaluation
1200+
result = await this.sendCDPCommandToTarget(tabId, 'Runtime.evaluate', {
1201+
expression: 'document.documentElement.outerHTML',
1202+
returnByValue: true
1203+
});
1204+
} else {
1205+
// Child frame - need to create isolated world in the frame's context
1206+
// First, create an execution context in this frame
1207+
const contextResult = await this.sendCDPCommandToTarget(tabId, 'Page.createIsolatedWorld', {
1208+
frameId: frame.id,
1209+
grantUniversalAccess: true,
1210+
worldName: 'iframe-capture'
1211+
});
1212+
1213+
// Now evaluate in that context
1214+
result = await this.sendCDPCommandToTarget(tabId, 'Runtime.evaluate', {
1215+
expression: 'document.documentElement.outerHTML',
1216+
returnByValue: true,
1217+
contextId: contextResult.executionContextId
1218+
});
1219+
}
11981220

11991221
htmlParts.push({
12001222
frameId: frame.id,
@@ -1212,6 +1234,7 @@ export class BrowserAgentServer extends EventEmitter {
12121234
logger.warn('Failed to capture frame HTML', {
12131235
tabId,
12141236
frameId: frame.id,
1237+
url: frame.url,
12151238
error: error.message
12161239
});
12171240
}

0 commit comments

Comments
 (0)