tor-browser

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

youtube.js (2554B)


      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  constructor(video) {
      9    // Use shorts player only if video is from YouTube Shorts.
     10    let shortsPlayer = video.closest("#shorts-player")?.wrappedJSObject;
     11    let isYTShorts = !!(video.baseURI.includes("shorts") && shortsPlayer);
     12 
     13    this.player = isYTShorts
     14      ? shortsPlayer
     15      : video.closest("#movie_player")?.wrappedJSObject;
     16  }
     17 
     18  isLive() {
     19    return !!document.querySelector(".ytp-live");
     20  }
     21 
     22  setMuted(video, shouldMute) {
     23    if (this.player) {
     24      if (shouldMute) {
     25        this.player.mute();
     26      } else {
     27        this.player.unMute();
     28      }
     29    } else {
     30      video.muted = shouldMute;
     31    }
     32  }
     33 
     34  getDuration(video) {
     35    if (this.isLive(video)) {
     36      return Infinity;
     37    }
     38    return video.duration;
     39  }
     40 
     41  setCaptionContainerObserver(video, updateCaptionsFunction) {
     42    let container = document.getElementById("ytp-caption-window-container");
     43 
     44    if (container) {
     45      updateCaptionsFunction("");
     46      const callback = function (mutationsList) {
     47        // eslint-disable-next-line no-unused-vars
     48        for (const mutation of mutationsList) {
     49          let textNodeList = container
     50            .querySelector(".captions-text")
     51            ?.querySelectorAll(".caption-visual-line");
     52          if (!textNodeList) {
     53            updateCaptionsFunction("");
     54            return;
     55          }
     56 
     57          updateCaptionsFunction(
     58            Array.from(textNodeList, x => x.textContent).join("\n")
     59          );
     60        }
     61      };
     62 
     63      // immediately invoke the callback function to add subtitles to the PiP window
     64      callback([1], null);
     65 
     66      this.captionsObserver = new MutationObserver(callback);
     67 
     68      this.captionsObserver.observe(container, {
     69        attributes: false,
     70        childList: true,
     71        subtree: true,
     72      });
     73    }
     74  }
     75 
     76  removeCaptionContainerObserver() {
     77    this.captionsObserver?.disconnect();
     78  }
     79 
     80  shouldHideToggle(video) {
     81    return !!video.closest(".ytd-video-preview");
     82  }
     83 
     84  setVolume(video, volume) {
     85    if (this.player) {
     86      this.player.setVolume(volume * 100);
     87    } else {
     88      video.volume = volume;
     89    }
     90  }
     91 
     92  getVolume(video) {
     93    if (this.player) {
     94      return this.player.getVolume() / 100;
     95    }
     96    return video.volume;
     97  }
     98 }
     99 
    100 this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;