no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / content / TopLevelVideoDocument.js
blob4505a93d18e7bb14ab65814a2d5eae3859430f86
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 // Hide our variables from the web content, even though the spec allows them
8 // (and the DOM) to be accessible (see bug 1474832)
10   // <video> is used for top-level audio documents as well
11   let videoElement = document.getElementsByTagName("video")[0];
13   let setFocusToVideoElement = function (e) {
14     // We don't want to retarget focus if it goes to the controls in
15     // the video element. Because they're anonymous content, the target
16     // will be the video element in that case. Avoid calling .focus()
17     // for those events:
18     if (e && e.target == videoElement) {
19       return;
20     }
21     videoElement.focus();
22   };
24   // Redirect focus to the video element whenever the document receives
25   // focus.
26   document.addEventListener("focus", setFocusToVideoElement, true);
28   // Focus on the video in the newly created document.
29   setFocusToVideoElement();
31   // Opt out of moving focus away if the DOM tree changes (from add-on or web content)
32   let observer = new MutationObserver(() => {
33     observer.disconnect();
34     document.removeEventListener("focus", setFocusToVideoElement, true);
35   });
36   observer.observe(document.documentElement, {
37     childList: true,
38     subtree: true,
39   });
41   // Handle fullscreen mode
42   document.addEventListener("keypress", ev => {
43     // Maximize the standalone video when pressing F11,
44     // but ignore audio elements
45     if (
46       ev.key == "F11" &&
47       videoElement.videoWidth != 0 &&
48       videoElement.videoHeight != 0
49     ) {
50       // If we're in browser fullscreen mode, it means the user pressed F11
51       // while browser chrome or another tab had focus.
52       // Don't break leaving that mode, so do nothing here.
53       if (window.fullScreen) {
54         return;
55       }
57       // If we're not in browser fullscreen mode, prevent entering into that,
58       // so we don't end up there after pressing Esc.
59       ev.preventDefault();
60       ev.stopPropagation();
62       if (!document.fullscreenElement) {
63         videoElement.requestFullscreen();
64       } else {
65         document.exitFullscreen();
66       }
67     }
68   });