The Page Visibility API 📎
You only have to register a visibilitychange listener:     
document.addEventListener('visibilitychange', _ => { 
    const state = document.visibilityState;
    console.log('document is: ',state);
})    
Hiding the page / making it visible again prints:
document is:  hidden
document is:  visible    
See it in action:
See you at "Build to last" effectively progressive applications with webstandards only -- the "no frameworks, no migrations" approach, at Munich Airport, Terminal 2 or effectiveweb.training (online).