





























@@ -34,9 +34,11 @@ import {
3434readBody,
3535requirePwAi,
3636resolveTargetIdFromBody,
37+resolveSafeRouteTabUrl,
3738withRouteTabContext,
3839SELECTOR_UNSUPPORTED_MESSAGE,
3940} from "./agent.shared.js";
41+import { resolveTargetIdAfterNavigate } from "./agent.snapshot-target.js";
4042import { EXISTING_SESSION_LIMITS } from "./existing-session-limits.js";
4143import type { BrowserRouteRegistrar } from "./types.js";
4244import { asyncBrowserRoute, jsonError, toNumber, toStringOrEmpty } from "./utils.js";
@@ -388,11 +390,35 @@ export function registerBrowserAgentActRoutes(
388390run: async ({ profileCtx, cdpUrl, tab, resolveTabUrl }) => {
389391const evaluateEnabled = ctx.state().resolved.evaluateEnabled;
390392const ssrfPolicy = ctx.state().resolved.ssrfPolicy;
391-const jsonOk = async (extra?: Record<string, unknown>) => {
392-const url = await resolveTabUrl(tab.url);
393+const isExistingSession = getBrowserProfileCapabilities(profileCtx.profile).usesChromeMcp;
394+const hasNavigationResultPolicy = Boolean(
395+withBrowserNavigationPolicy(ssrfPolicy).ssrfPolicy,
396+);
397+const jsonOk = async (
398+extra?: Record<string, unknown>,
399+options?: { resolveCurrentTarget?: boolean },
400+) => {
401+const shouldResolveCurrentTarget =
402+options?.resolveCurrentTarget && (!isExistingSession || hasNavigationResultPolicy);
403+const responseTargetId = shouldResolveCurrentTarget
404+ ? await resolveTargetIdAfterNavigate({
405+oldTargetId: tab.targetId,
406+navigatedUrl: tab.url,
407+listTabs: () => profileCtx.listTabs(),
408+})
409+ : tab.targetId;
410+const url =
411+responseTargetId === tab.targetId
412+ ? await resolveTabUrl(tab.url)
413+ : await resolveSafeRouteTabUrl({
414+ ctx,
415+ profileCtx,
416+targetId: responseTargetId,
417+fallbackUrl: tab.url,
418+});
393419return res.json({
394420ok: true,
395-targetId: tab.targetId,
421+targetId: responseTargetId,
396422 ...(url ? { url } : {}),
397423 ...extra,
398424});
@@ -405,10 +431,9 @@ export function registerBrowserAgentActRoutes(
405431"action targetId must match request targetId",
406432);
407433}
408-const isExistingSession = getBrowserProfileCapabilities(profileCtx.profile).usesChromeMcp;
409434const profileName = profileCtx.profile.name;
410435if (isExistingSession) {
411-const initialTabTargetIds = withBrowserNavigationPolicy(ssrfPolicy).ssrfPolicy
436+const initialTabTargetIds = hasNavigationResultPolicy
412437 ? new Set((await profileCtx.listTabs()).map((currentTab) => currentTab.targetId))
413438 : new Set<string>();
414439const existingSessionNavigationGuard = {
@@ -443,7 +468,7 @@ export function registerBrowserAgentActRoutes(
443468}),
444469guard: existingSessionNavigationGuard,
445470});
446-return await jsonOk();
471+return await jsonOk(undefined, { resolveCurrentTarget: true });
447472case "clickCoords":
448473await runExistingSessionActionWithNavigationGuard({
449474execute: () =>
@@ -459,7 +484,7 @@ export function registerBrowserAgentActRoutes(
459484}),
460485guard: existingSessionNavigationGuard,
461486});
462-return await jsonOk();
487+return await jsonOk(undefined, { resolveCurrentTarget: true });
463488case "type":
464489await runExistingSessionActionWithNavigationGuard({
465490execute: async () => {
@@ -481,7 +506,7 @@ export function registerBrowserAgentActRoutes(
481506},
482507guard: existingSessionNavigationGuard,
483508});
484-return await jsonOk();
509+return await jsonOk(undefined, { resolveCurrentTarget: true });
485510case "press":
486511await runExistingSessionActionWithNavigationGuard({
487512execute: () =>
@@ -493,7 +518,7 @@ export function registerBrowserAgentActRoutes(
493518}),
494519guard: existingSessionNavigationGuard,
495520});
496-return await jsonOk();
521+return await jsonOk(undefined, { resolveCurrentTarget: true });
497522case "hover":
498523await runExistingSessionActionWithNavigationGuard({
499524execute: () =>
@@ -631,15 +656,19 @@ export function registerBrowserAgentActRoutes(
631656});
632657switch (action.kind) {
633658case "batch":
634-return await jsonOk({ results: result.results ?? [] });
659+return await jsonOk(
660+{ results: result.results ?? [] },
661+{ resolveCurrentTarget: true },
662+);
635663case "evaluate":
636-return await jsonOk({ result: result.result });
664+return await jsonOk({ result: result.result }, { resolveCurrentTarget: true });
637665case "click":
638666case "clickCoords":
667+return await jsonOk(undefined, { resolveCurrentTarget: true });
639668case "resize":
640669return await jsonOk();
641670default:
642-return await jsonOk();
671+return await jsonOk(undefined, { resolveCurrentTarget: true });
643672}
644673},
645674});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。