mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-11 03:13:12 +00:00
fix(browser): 修复 download 事件的 pageIndex 过滤
为 browser_wait_for_event 的 download 路径补齐按页面 frame tree 过滤,避免多页面或动态 iframe 下载时误命中或漏命中。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
f6ef59ef96
commit
40c718c584
2 files changed
+220
-4
No files matched your search
@@ -4174,8 +4174,38 @@ async function waitForPageEvent({ page, timeoutMs, event }) {
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForBrowserDownloadEvent(timeoutMs, event) {
|
||||
const targets = await fetchJson(`${getHttpUrl()}/json/list`);
|
||||
async function getPageFrameIds(page) {
|
||||
const response = await sendCdpCommand(page, 'Page.getFrameTree', {});
|
||||
const frameTree = response?.frameTree;
|
||||
const frameIds = new Set();
|
||||
|
||||
function visitFrameTree(node) {
|
||||
if (!node || typeof node !== 'object') {
|
||||
return;
|
||||
}
|
||||
const frameId = node.frame?.id;
|
||||
if (typeof frameId === 'string' && frameId) {
|
||||
frameIds.add(frameId);
|
||||
}
|
||||
const childFrames = Array.isArray(node.childFrames) ? node.childFrames : [];
|
||||
for (const childFrame of childFrames) {
|
||||
visitFrameTree(childFrame);
|
||||
}
|
||||
}
|
||||
|
||||
visitFrameTree(frameTree);
|
||||
|
||||
if (frameIds.size === 0) {
|
||||
throw new Error('Browser MCP could not determine the selected page frame IDs.');
|
||||
}
|
||||
return frameIds;
|
||||
}
|
||||
|
||||
async function waitForBrowserDownloadEvent(page, timeoutMs, event) {
|
||||
const [targets, frameIds] = await Promise.all([
|
||||
fetchJson(`${getHttpUrl()}/json/list`),
|
||||
getPageFrameIds(page),
|
||||
]);
|
||||
const browserTarget = Array.isArray(targets)
|
||||
? targets.find((target) => target && typeof target === 'object' && target.type === 'browser')
|
||||
: null;
|
||||
@@ -4227,6 +4257,16 @@ async function waitForBrowserDownloadEvent(timeoutMs, event) {
|
||||
if (message?.method !== 'Browser.downloadWillBegin') {
|
||||
return;
|
||||
}
|
||||
const eventFrameId = message.params?.frameId;
|
||||
if (!frameIds.has(eventFrameId)) {
|
||||
const refreshedFrameIds = await getPageFrameIds(page);
|
||||
if (!refreshedFrameIds.has(eventFrameId)) {
|
||||
return;
|
||||
}
|
||||
for (const frameId of refreshedFrameIds) {
|
||||
frameIds.add(frameId);
|
||||
}
|
||||
}
|
||||
const observed = {
|
||||
url: message.params?.url || '',
|
||||
suggestedFilename: message.params?.suggestedFilename || '',
|
||||
@@ -4248,7 +4288,7 @@ async function waitForBrowserDownloadEvent(timeoutMs, event) {
|
||||
|
||||
async function waitForMatchingEvent({ page, timeoutMs, event }) {
|
||||
if (event.kind === 'download') {
|
||||
return await waitForBrowserDownloadEvent(timeoutMs, event);
|
||||
return await waitForBrowserDownloadEvent(page, timeoutMs, event);
|
||||
}
|
||||
return await waitForPageEvent({ page, timeoutMs, event });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user