fix(desktop): correctly await executeJavaScript for element focus restoration #5894
Closed
mohamedmastouri-hue(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/5894/head·opened View on GitHubShowing 1 changed file with 24 additions and 0 deletions.
x
@@ -3024,6 +3024,17 @@
+ yield* attemptPromise(
+ {
+ operation: "automationPress.restoreActiveElement",
+ tabId,
+ webContentsId: previouslyFocused.id,
+ },
+ () =>
+ previouslyFocused.executeJavaScript(
+ `if (window.__t3_restoreFocus && typeof window.__t3_restoreFocus.focus === 'function') { window.__t3_restoreFocus.focus(); delete window.__t3_restoreFocus; }`,
+ ),
+ ).pipe(Effect.ignore);
@@ -3032,6 +3043,19 @@
Thread on line 3045
🟠 High preview/Manager.ts:3045
The focus-save/restore logic uses the hardcoded global window.__t3_restoreFocus to stash the previously focused element. If the page already defines that property, the save script unconditionally overwrites it and the cleanup script permanently deletes it, destroying the page's own value. Consider using a Symbol key or saving and restoring any pre-existing value so automation never clobbers a page-owned property.
🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/desktop/src/preview/Manager.ts around line 3045:
The focus-save/restore logic uses the hardcoded global `window.__t3_restoreFocus` to stash the previously focused element. If the page already defines that property, the save script unconditionally overwrites it and the cleanup script permanently deletes it, destroying the page's own value. Consider using a `Symbol` key or saving and restoring any pre-existing value so automation never clobbers a page-owned property.
+ if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
+ yield* attemptPromise(
+ {
+ operation: "automationPress.saveActiveElement",
+ tabId,
+ webContentsId: previouslyFocused.id,