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

@@ -70,8 +70,8 @@ class BrowserError(Exception):
class BrowserSession:
"""One CDP connection to a tab of its own in the bot browser. Every wait ends at `deadline`
(a monotonic time the caller sets for each action)."""
"""One CDP connection to a page of its own (in a window of its own) in the bot browser. Every
wait ends at `deadline` (a monotonic time the caller sets for each action)."""
def __init__(self, cdp_url: str, deadline: float):
from websockets.sync.client import connect
@@ -79,18 +79,36 @@ class BrowserSession:
self.deadline = deadline
self._cdp = cdp_url
self._opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) # CDP is local: never a proxy
req = urllib.request.Request(f"{cdp_url}/json/new?about:blank", method="PUT")
page = json.loads(self._opener.open(req, timeout=self._left()).read())
self.target_id = page["id"]
# Entered like a context manager (newer websockets warns otherwise); close() ends it.
self._ws = connect(page["webSocketDebuggerUrl"], open_timeout=self._left(),
max_size=16 * 1024 * 1024).__enter__()
self._id = 0
self._events: deque = deque()
self._call("Page.enable")
version = json.loads(self._opener.open(f"{cdp_url}/json/version", timeout=self._left()).read())
with connect(version["webSocketDebuggerUrl"], open_timeout=self._left()) as browser:
self._ws = browser
self.target_id = self._call("Target.createTarget", self._new_window())["targetId"]
# Entered like a context manager (newer websockets warns otherwise); close() ends it.
host = urllib.parse.urlsplit(cdp_url).netloc
try:
self._ws = connect(f"ws://{host}/devtools/page/{self.target_id}", open_timeout=self._left(),
max_size=16 * 1024 * 1024).__enter__()
self._call("Page.enable")
except Exception:
self.close() # no window left behind on the shared screen
raise
def _new_window(self) -> dict:
"""A window of its own, opened behind (a tab brought to the front takes the keyboard focus
from the program the user is in), at the place and size of the bot browser's window."""
params = {"url": "about:blank", "newWindow": True, "background": True}
try:
page = next(t for t in self._call("Target.getTargets")["targetInfos"] if t["type"] == "page")
b = self._call("Browser.getWindowForTarget", {"targetId": page["targetId"]})["bounds"]
params.update({k: b[k] for k in ("left", "top", "width", "height")})
except (BrowserError, StopIteration, KeyError):
debug_log("bot browser: no window to take the place from", "browser")
return params
def close(self) -> None:
"""Close the connection and the tab."""
"""Close the connection and the page (and so its window)."""
try:
self._ws.close()
except Exception: # noqa: BLE001
@@ -205,7 +223,7 @@ class BrowserSession:
session_factory = BrowserSession
# One tab per person (Discord user ID), at most MAX_TABS; the least recently used closes first.
# One page (window) per person (Discord user ID), at most MAX_TABS; the least recently used closes first.
_sessions: Dict[str, Any] = {}
MAX_TABS = 5
_lock = threading.Lock()

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`.