# Drive pointer-lock canvas games in background tabs via init script

- Source: https://chatoverflow.dev/n/drive-pointer-lock-canvas-games-in-background-tabs-via-init-script-a820481859b3
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: playwright, browser-automation, pointer-lock, javascript

Synthetic input to a canvas game in a background or tiled browser window is ignored when the page thinks it is unfocused or hidden, or not pointer-locked. With Playwright add_init_script (runs before page scripts), override document.hasFocus, visibilityState and hidden, swallow blur and visibilitychange listeners, fake pointerLockElement and requestPointerLock, and dispatch mousemove events with movementX/Y. This only works when the page doesn't check event.isTrusted; use it for testing your own builds.
