fix(browser): the bot opens its pages in windows of their own behind, so it never takes the focus

On the shared screen a tab brought to the front makes Chrome bring its window forward and take the
keyboard focus from the game or program the user is in (seen on the game PC). The botBrowser tool
now opens each person's page in a window of its own, behind and at the place and size of the bot
browser's window (Target.createTarget with newWindow and background), and closes it again if the
connection to it fails. While the browser runs on the virtual monitor, the app looks for new Chrome
windows every 200 ms and settles each like the first (tool window, fitted to the work area, without
activating it), the newest in front of the older ones, so the shared screen shows the page the bot
opened last.

Checked on the game PC: the bot's window loading a public page comes in front of the first, both
out of the taskbar and Alt+Tab, the focus staying in Discord also with the user idle for hours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-15 16:15:36 +09:00
parent cb2dda25b6
commit bd7b69d4f4
8 changed files with 221 additions and 55 deletions

View File

@@ -20,9 +20,12 @@ read it, click a link or button, type into a field, go back. Product rules: `doc
- **Bounded.** One deadline of 15 s covers an action: connecting, the action and waiting for the
page; events arriving in between do not extend it. Text is cut at 6,000 characters and the list
of links and buttons at 40.
- **A tab per person.** Each asker (`ToolContext.user_id`) has a tab and connection of their own,
so one person's page and numbered list are never another's. At most 5 tabs; the least recently
used one is closed first.
- **A window per person.** Each asker (`ToolContext.user_id`) has a page and connection of their
own, so one person's page and numbered list are never another's. The page opens in a window of
its own, behind, at the place and size of the bot browser's window: a tab brought to the front
would take the keyboard focus from the game or program the user is in, while a window opened
behind does not, and on the shared screen the app puts the newest window in front (admin.spec.md,
"Virtual monitor"). At most 5; the least recently used one is closed first.
- **One tab, also for "new tab" links.** Before a click or typing, links, forms and `<base>` lose
their `target` and `window.open` loads the address in the same tab, so what opens is what the
tool reads next.
@@ -50,8 +53,11 @@ buttons, all inside the fence. Failures come back as `Error: ...` with `success=
## Backend
`BrowserSession(cdp_url, deadline)` opens a tab of its own (`/json/new`) and speaks CDP over its
WebSocket (`websockets` sync client); `close()` closes the tab: `Page.navigate`, `Runtime.evaluate` (reading, finding and clicking elements),
`BrowserSession(cdp_url, deadline)` opens its page on the browser's connection
(`Target.createTarget` with `newWindow` and `background`, at the window bounds of the first page
there, `Browser.getWindowForTarget`; without them where Chrome puts it) and speaks CDP over the
page's WebSocket (`websockets` sync client); a failure after the page opened closes it again;
`close()` closes the page and with it its window: `Page.navigate`, `Runtime.evaluate` (reading, finding and clicking elements),
`Input.insertText` and `Input.dispatchKeyEvent` (typing, Enter). The tool keeps a session per person
and makes a new one when the old one fails. Tests and evals replace `session_factory`.