tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

primeVideo.js (3264B)


      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/. */
      4 
      5 "use strict";
      6 
      7 class PictureInPictureVideoWrapper {
      8  /**
      9   * Playing the video when the readyState is HAVE_METADATA (1) can cause play
     10   * to fail but it will load the video and trying to play again allows enough
     11   * time for the second play to successfully play the video.
     12   *
     13   * @param {HTMLVideoElement} video
     14   *  The original video element
     15   */
     16  play(video) {
     17    video.play().catch(() => {
     18      video.play();
     19    });
     20  }
     21 
     22  /**
     23   * Seeking large amounts of time can cause the video readyState to
     24   * HAVE_METADATA (1) and it will throw an error when trying to play the video.
     25   * To combat this, after seeking we check if the readyState changed and if so,
     26   * we will play to video to "load" the video at the new time and then play or
     27   * pause the video depending on if the video was playing before we seeked.
     28   *
     29   * @param {HTMLVideoElement} video
     30   *  The original video element
     31   * @param {number} position
     32   *  The new time to set the video to
     33   * @param {boolean} wasPlaying
     34   *  True if the video was playing before seeking else false
     35   */
     36  setCurrentTime(video, position, wasPlaying) {
     37    if (wasPlaying === undefined) {
     38      this.wasPlaying = !video.paused;
     39    }
     40    video.currentTime = position;
     41    if (video.readyState < video.HAVE_CURRENT_DATA) {
     42      video
     43        .play()
     44        .then(() => {
     45          if (!wasPlaying) {
     46            video.pause();
     47          }
     48        })
     49        .catch(() => {
     50          if (wasPlaying) {
     51            this.play(video);
     52          }
     53        });
     54    }
     55  }
     56 
     57  setCaptionContainerObserver(video, updateCaptionsFunction) {
     58    let container = document?.querySelector("#dv-web-player");
     59 
     60    if (container) {
     61      updateCaptionsFunction("");
     62      const callback = function (mutationsList) {
     63        // eslint-disable-next-line no-unused-vars
     64        for (const mutation of mutationsList) {
     65          let text;
     66          // windows, mac
     67          if (container?.querySelector(".atvwebplayersdk-player-container")) {
     68            text = container
     69              ?.querySelector(".f35bt6a")
     70              ?.querySelector(".atvwebplayersdk-captions-text")?.innerText;
     71          } else {
     72            // linux
     73            text = container
     74              ?.querySelector(".persistentPanel")
     75              ?.querySelector("span")?.innerText;
     76          }
     77 
     78          if (!text) {
     79            updateCaptionsFunction("");
     80            return;
     81          }
     82 
     83          updateCaptionsFunction(text);
     84        }
     85      };
     86 
     87      // immediately invoke the callback function to add subtitles to the PiP window
     88      callback([1], null);
     89 
     90      this.captionsObserver = new MutationObserver(callback);
     91 
     92      this.captionsObserver.observe(container, {
     93        attributes: true,
     94        childList: true,
     95        subtree: true,
     96      });
     97    }
     98  }
     99 
    100  removeCaptionContainerObserver() {
    101    this.captionsObserver?.disconnect();
    102  }
    103 
    104  shouldHideToggle(video) {
    105    return !!video.classList.contains("tst-video-overlay-player-html5");
    106  }
    107 }
    108 
    109 this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;