Visibility API

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.