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

@@ -9,12 +9,14 @@ browser off local addresses is the app's test (app/tests/bot_browser.rs).
from __future__ import annotations
import http.server
import json
import os
import subprocess
import tempfile
import threading
import time
import urllib.parse
import urllib.request
from pathlib import Path
import pytest
@@ -332,6 +334,39 @@ def test_links_that_would_open_a_new_tab_open_in_the_same_tab(chrome):
assert s.click("Pop up")["title"] == "Second page"
_local = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def _windows(cdp, ids):
"""Each target's (window id, bounds) over the browser connection."""
from websockets.sync.client import connect
ws = json.loads(_local.open(f"{cdp}/json/version").read())["webSocketDebuggerUrl"]
out = []
with connect(ws) as c:
for i, t in enumerate(ids, 1):
c.send(json.dumps({"id": i, "method": "Browser.getWindowForTarget", "params": {"targetId": t}}))
while (m := json.loads(c.recv(timeout=5))).get("id") != i:
pass
b = m["result"]["bounds"]
out.append((m["result"]["windowId"], (b["left"], b["top"], b["width"], b["height"])))
return out
def test_each_session_has_a_window_of_its_own_where_the_bot_browser_window_is(chrome):
cdp, site, _ = chrome
first = [t["id"] for t in json.loads(_local.open(f"{cdp}/json/list").read()) if t["type"] == "page"]
a, b = BrowserSession(cdp, time.monotonic() + 15), BrowserSession(cdp, time.monotonic() + 15)
windows = _windows(cdp, first + [a.target_id, b.target_id])
assert len({w for w, _ in windows}) == 3, windows
assert {bounds for _, bounds in windows} == {windows[0][1]}, windows
assert a.open(site + "/")["title"] == "Home page"
a.close()
b.close()
left = [t["id"] for t in json.loads(_local.open(f"{cdp}/json/list").read()) if t["type"] == "page"]
assert left == first
def test_two_sessions_have_their_own_tabs(chrome):
cdp, site, _ = chrome
a, b = BrowserSession(cdp, time.monotonic() + 15), BrowserSession(cdp, time.monotonic() + 15)