跪拜 Guibai
← All articles
Chrome · Frontend · Node.js

Why My Scraping Script Never Steals Your Cursor

By stormsha ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A scraping script can stay entirely out of sight while doing its job, which means desktop automation can run quietly alongside real work. More importantly, verifying the owning pid before reuse stops a script from acting on a browser that belongs to another process, a failure mode that can cause unwanted posts under someone else's session.

Summary

Chrome automation that never steals the cursor comes down to a few CDP calls plus strict discipline about when to touch window state. The standard `--start-minimized` flag is ignored by modern Chrome, so the working approach is to call `Browser.setWindowBounds` immediately after launching, first pinning the restore position inside the screen, then setting the state to minimized. With three flags that disable renderer backgrounding and timer throttling, a minimized window still renders pages fully, so scraping gets everything it needs from a hidden browser.

The bigger traps are on reuse. Every rerun of the script must not call `bringToFront` or any visibility method, or it will pop the user's manually minimized window back up. To safely reuse a browser, the script records its own pid in a state file and only reuses when that pid is still alive and still owns the port. Port 9222 is shared by every automation tool, so a bare port check can end up operating on someone else's browser. Login is the only reason to show a window, and even then it should appear centered and auto-minimize after five minutes.

CDP flatten mode is a separate trap: page commands need a sessionId, and the error `'Runtime.evaluate' wasn't found` means the command wasn't attached to a page. Finally, these scripts default to read-only; any write operation requires an explicit switch and manual confirmation.

Takeaways
— Modern Chrome ignores the `--start-minimized` flag; use CDP `Browser.setWindowBounds` to minimize a launched browser.
— A minimized Chrome window still renders pages fully as long as you launch with `--disable-renderer-backgrounding`, `--disable-background-timer-throttling`, and `--disable-features=CalculateNativeWinOcclusion`.
— Never call `bringToFront` or similar window-visibility methods when reusing an existing browser; that will undo the user's manual minimize.
— Track the browser's pid in a state file and verify that pid is still alive and owns the port before reuse; port 9222 alone can belong to another tool.
— Pop the window only for QR-code login, center it, and auto-minimize it after a 5-minute timeout.
— Check login state with a real read-only business request and its error code, not by inspecting cookie names; `document.cookie` misses httpOnly cookies and `Storage.getCookies` can return empty before the page context is ready.
— In CDP flatten mode, page-level commands need an explicit `sessionId`; the error `'Runtime.evaluate' wasn't found` means the command wasn't attached to a page.
— Default these scripts to read-only operations; write actions require an explicit switch and manual confirmation before execution.
Conclusions

Port 9222 is a shared default on most machines, so a pid check is the only real identity check before driving a browser; without it, an automation script can act on another process's session.

Hiding a Chrome window costs nothing in scraping capability as long as background throttling is disabled, which makes invisible automation a matter of window management discipline rather than a headless-mode trade-off.

The read-only default is a sensible safety posture for unattended scripts, because a script that can write has the same abuse potential as a logged-in user.

Concepts & terms
CDP (Chrome DevTools Protocol)
The protocol that lets tools control a running Chrome instance over WebSocket, covering navigation, evaluation, and window state via methods like `Browser.setWindowBounds`.
flatten mode
A CDP option that requires explicit session IDs for page-level commands; without it, commands like `Runtime.evaluate` fail with `'...' wasn't found` even though the method exists.
background throttling
Chrome's mechanism to reduce rendering and timer frequency for hidden windows; the article disables it with flags to keep scraping working in a minimized window.
From the discussion
Featured comments
组合搭配函数

Sharing one browser between the script and a human is a genuinely smart move. You attach via CDP and each does its own thing, never stepping on the other's toes. The login state comes straight from the browser, so you skip the back-and-forth of logging in repeatedly.

stormsha

Thanks. But once you actually try it, 'sharing' turns out to be the biggest pitfall: two scripts crammed into the same browser fight over not just the login state, but also tabs and windows. Today I hit a whole string of them: one script's --stop killed the entire process tree, and another platform's scraper got ECONNREFUSED; a cleanup script also proactively closed all editor tabs, destroying someone's half-filled content. The current approach is 'one browser per platform': CDP port, --user-data-dir, and pid state file each get their own set, and you scan the login state once per platform. Sharing only pays off under one condition: you run exactly one thing at a time. Reusing the login state is indeed nice, that part is correct.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗