Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Detect if window is inactive but still visible

A number of other answers have covered how a running Javascript script can tell whether its current window is active or not. However, that's a pretty restrictive set of choices, and makes no distinctions between (for example)

  1. a minimized window
  2. an inactive tab
  3. a floating window partially eclipsed by other programs' floating windows
  4. a tab that's fullscreened on one monitor while a user does things in another window on the other monitor.

All of these count as inactive, despite the fact that in the latter two cases the webpage remains visible to the user.

While there's evidence that this may be technically impossible given how web browser sandboxes work, I'd still like to be able to detect the differences between the above. For example, pausing an animation for (1) or (2), but keeping it going for (3) or (4). I'm wondering if Javascript (plus frameworks, I guess, or WebAssembly) provides a mechanism to determine this - or, failing that, whether any particular browsers have mechanisms to determine it from within the javascript code of one page.

like image 682
Green Cloak Guy Avatar asked Jun 11 '19 01:06

Green Cloak Guy


1 Answers

You can't. I looked into this for a project a while back and active vs inactive is the only distinction the browser can make.

Information about the positions of other windows on the system isn't passed to the browser, especially given that this route could possibly be reverse engineered to expose a security risk. It's much safer to keep each window in the dark about what other programmes on the system are doing.

You can gather information based on events happening in the current window - for example detecting the opening of a print dialogue - but not from other windows.

The Page Visibility API has 95% coverage and is probably the closest you can come - but you already know this, because it's the top answer in each question you linked. You're out of luck here.

like image 69
snazzybouche Avatar answered Sep 29 '22 10:09

snazzybouche