How to know whether the browser window has the focus or not?

Example:

let has_focus = ref true

let _ =
  let open Lwt_js_events in
  async (fun () -> focuses (Js.Unsafe