Source: lib/polyfill/fullscreen.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.Fullscreen');
  7. goog.require('shaka.polyfill');
  8. /**
  9. * @summary A polyfill to unify fullscreen APIs across browsers.
  10. * Many browsers have prefixed fullscreen methods on Element and document.
  11. * See {@link https://mzl.la/2K0xcHo Using fullscreen mode} on MDN for more
  12. * information.
  13. * @export
  14. */
  15. shaka.polyfill.Fullscreen = class {
  16. /**
  17. * Install the polyfill if needed.
  18. * @export
  19. */
  20. static install() {
  21. if (!window.Document) {
  22. // Avoid errors on very old browsers.
  23. return;
  24. }
  25. // eslint-disable-next-line no-restricted-syntax
  26. let proto = Element.prototype;
  27. proto.requestFullscreen = proto.requestFullscreen ||
  28. proto.mozRequestFullScreen ||
  29. proto.msRequestFullscreen ||
  30. proto.webkitRequestFullscreen;
  31. // eslint-disable-next-line no-restricted-syntax
  32. proto = Document.prototype;
  33. proto.exitFullscreen = proto.exitFullscreen ||
  34. proto.mozCancelFullScreen ||
  35. proto.msExitFullscreen ||
  36. proto.webkitCancelFullScreen;
  37. if (!('fullscreenElement' in document)) {
  38. Object.defineProperty(document, 'fullscreenElement', {
  39. get: () => {
  40. return document.mozFullScreenElement ||
  41. document.msFullscreenElement ||
  42. document.webkitCurrentFullScreenElement ||
  43. document.webkitFullscreenElement;
  44. },
  45. });
  46. Object.defineProperty(document, 'fullscreenEnabled', {
  47. get: () => {
  48. return document.mozFullScreenEnabled ||
  49. document.msFullscreenEnabled ||
  50. document.webkitFullscreenEnabled;
  51. },
  52. });
  53. }
  54. const proxy = shaka.polyfill.Fullscreen.proxyEvent_;
  55. document.addEventListener('webkitfullscreenchange', proxy);
  56. document.addEventListener('webkitfullscreenerror', proxy);
  57. document.addEventListener('mozfullscreenchange', proxy);
  58. document.addEventListener('mozfullscreenerror', proxy);
  59. document.addEventListener('MSFullscreenChange', proxy);
  60. document.addEventListener('MSFullscreenError', proxy);
  61. }
  62. /**
  63. * Proxy fullscreen events after changing their name.
  64. * @param {!Event} event
  65. * @private
  66. */
  67. static proxyEvent_(event) {
  68. const eventType = event.type.replace(/^(webkit|moz|MS)/, '').toLowerCase();
  69. const newEvent = document.createEvent('Event');
  70. newEvent.initEvent(eventType, event.bubbles, event.cancelable);
  71. event.target.dispatchEvent(newEvent);
  72. }
  73. };
  74. shaka.polyfill.register(shaka.polyfill.Fullscreen.install);