tor-browser

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

peacocktv.js (3041B)


      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 /**
      8 * This wrapper is used for:
      9 * Peacock TV
     10 * Now TV
     11 * Showmax
     12 * Sky Showtime
     13 */
     14 class PictureInPictureVideoWrapper {
     15  constructor(video) {
     16    this.video = video.wrappedJSObject;
     17  }
     18 
     19  hasCvsdkSession() {
     20    return (
     21      this.video.cvsdkSession !== undefined && this.video.cvsdkSession !== null
     22    );
     23  }
     24 
     25  play() {
     26    if (!this.hasCvsdkSession()) {
     27      this.video.play();
     28      return;
     29    }
     30    this.video.cvsdkSession.play();
     31  }
     32 
     33  pause() {
     34    if (!this.hasCvsdkSession()) {
     35      this.video.pause();
     36      return;
     37    }
     38    this.video.cvsdkSession.pause();
     39  }
     40 
     41  setCurrentTime(video, position, wasPlaying) {
     42    if (!this.hasCvsdkSession()) {
     43      this.video.currentTime = position;
     44      return;
     45    }
     46    this.video.cvsdkSession.seek(position, wasPlaying);
     47  }
     48 
     49  /**
     50   * In the absence of a cvsdkSession, try to derive caption information
     51   * from the parent video element's DOM.
     52   */
     53  setDefaultCaptionMutationObserver(video, updateCaptionsFunction) {
     54    /*
     55     * Actual subtitles DOM element may be not present initially or
     56     * change during playback
     57     * So no need to check if it exists before setting the observer
     58     * Assume some sort of captions may be available
     59     *
     60     * Actual attributes on the subtitles element may vary with the build
     61     * and environment, so try a couple different selectors.
     62     */
     63    const captionsQuery = "[data-t-subtitles=true], [data-t=subtitles]";
     64    const getContainer = () => {
     65      return video.parentElement.querySelector(captionsQuery);
     66    };
     67 
     68    updateCaptionsFunction("");
     69 
     70    this.captionsObserver = new MutationObserver(() => {
     71      let text = getContainer().innerText;
     72      updateCaptionsFunction(text);
     73    });
     74 
     75    this.captionsObserver.observe(video.parentElement, {
     76      attributes: false,
     77      childList: true,
     78      subtree: true,
     79    });
     80  }
     81 
     82  setCaptionContainerObserver(video, updateCaptionsFunction) {
     83    if (!this.hasCvsdkSession()) {
     84      this.setDefaultCaptionMutationObserver(video, updateCaptionsFunction);
     85      return;
     86    }
     87    this.video.cvsdkSession.setSimpleCueHandler(updateCaptionsFunction);
     88  }
     89 
     90  getDuration() {
     91    if (!this.hasCvsdkSession()) {
     92      return this.video.duration;
     93    }
     94    return this.video.cvsdkSession.getDuration();
     95  }
     96 
     97  getCurrentTime() {
     98    if (!this.hasCvsdkSession()) {
     99      return this.video.currentTime;
    100    }
    101    return this.video.cvsdkSession.getCurrentTime();
    102  }
    103 
    104  setMuted(video, shouldMute) {
    105    if (!this.hasCvsdkSession()) {
    106      this.video.muted = shouldMute;
    107      return;
    108    }
    109    this.video.cvsdkSession.setMute(shouldMute);
    110  }
    111 
    112  getMuted() {
    113    if (!this.hasCvsdkSession()) {
    114      return this.video.muted;
    115    }
    116    return this.video.cvsdkSession.isMuted();
    117  }
    118 }
    119 
    120 this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;