Visibility API

We are working on a web app in which we need to fire an event whenever the user closes the laptop lid or locked the screen, for this we are using VisibilityAPI , now this API listen when the window hides, we need to exclude the minimize and tabl switch event from this, can you please let us know that how can we exclude the minimize and tab switch event and only listen to the window lock event?

1 Like

There are several points you need to be aware of.

  1. Not all “closing laptop lid” events fire visibilitychange events, it depends on system configuration within which “whether to keep screen on when closing laptop lid” is set to false.

  2. visibilitychange events are fired only when the interested tab is visible on screen, which means besides ①"minimize", ②"tab switch" and ③"locking screen"events, ④"virtual desktop switch" and ⑤"other windows dragged to entirely cover the interested tab" events also exist.

  3. you can never distinguish between ③, ④, ⑤.

  4. you can recognize â‘  when using extension to get the active window or the interested window is the active window, of which innerWidth and outerWidth are updated and outerWidth - innerWidth < 0 is true.

  5. you can recognize ② only when using extension to get the active window and judge whether the active window is equal to the interested window.

Using statistics and smart algorithm may be of some help and not mention above.