Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(web): agent clicks in background browsers no longer steal prompt focus #14331

ClosedRealDoc06(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/14331/headopened View on GitHub

Showing 1 changed file with 16 additions and 3 deletions.

x
0 of 1 viewed, 1 left
16 additions and 3 deletionsapps/web/src/browser/HostedBrowserWebview.tsxModified
View file
@@ -159,7 +159,20 @@
// A click inside the guest only reaches this document as a webview focus
// event, so open menus and popovers never see the outside press that
// would dismiss them. Replay it as a pointerdown on the webview itself.
- const dismissHostPopups = () => {
+ //
+ // Hidden guests cannot be clicked by a person, so their focus comes from
+ // agent automation (CDP mouse input focuses the target guest). Hand focus
+ // back to whatever the user was typing into instead of swallowing keys.
+ const onWebviewFocus = (event: FocusEvent) => {
+ const surface = useBrowserSurfaceStore.getState().byTabId[runtimeTabId];
+ if (!surface?.visible || !surface.rect) {
+ if (event.relatedTarget instanceof HTMLElement) {
+ event.relatedTarget.focus({ preventScroll: true });
+ } else {
+ webview.blur();
+ }
+ return;
+ }
webview.dispatchEvent(
new PointerEvent("pointerdown", { bubbles: true, pointerType: "mouse" }),
@@ -167,7 +180,7 @@
webview.addEventListener("did-attach", register);
webview.addEventListener("dom-ready", register);
webview.addEventListener("render-process-gone", recoverGuest);
- webview.addEventListener("focus", dismissHostPopups);
+ webview.addEventListener("focus", onWebviewFocus);
register();