summaryrefslogtreecommitdiffstats
path: root/toolkit/content/TopLevelVideoDocument.js
blob: 4505a93d18e7bb14ab65814a2d5eae3859430f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Hide our variables from the web content, even though the spec allows them
// (and the DOM) to be accessible (see bug 1474832)
{
  // <video> is used for top-level audio documents as well
  let videoElement = document.getElementsByTagName("video")[0];

  let setFocusToVideoElement = function (e) {
    // We don't want to retarget focus if it goes to the controls in
    // the video element. Because they're anonymous content, the target
    // will be the video element in that case. Avoid calling .focus()
    // for those events:
    if (e && e.target == videoElement) {
      return;
    }
    videoElement.focus();
  };

  // Redirect focus to the video element whenever the document receives
  // focus.
  document.addEventListener("focus", setFocusToVideoElement, true);

  // Focus on the video in the newly created document.
  setFocusToVideoElement();

  // Opt out of moving focus away if the DOM tree changes (from add-on or web content)
  let observer = new MutationObserver(() => {
    observer.disconnect();
    document.removeEventListener("focus", setFocusToVideoElement, true);
  });
  observer.observe(document.documentElement, {
    childList: true,
    subtree: true,
  });

  // Handle fullscreen mode
  document.addEventListener("keypress", ev => {
    // Maximize the standalone video when pressing F11,
    // but ignore audio elements
    if (
      ev.key == "F11" &&
      videoElement.videoWidth != 0 &&
      videoElement.videoHeight != 0
    ) {
      // If we're in browser fullscreen mode, it means the user pressed F11
      // while browser chrome or another tab had focus.
      // Don't break leaving that mode, so do nothing here.
      if (window.fullScreen) {
        return;
      }

      // If we're not in browser fullscreen mode, prevent entering into that,
      // so we don't end up there after pressing Esc.
      ev.preventDefault();
      ev.stopPropagation();

      if (!document.fullscreenElement) {
        videoElement.requestFullscreen();
      } else {
        document.exitFullscreen();
      }
    }
  });
}