tor-browser

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

iq.js (2352B)


      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() {
      9    this.player = window.wrappedJSObject.playerObject;
     10  }
     11 
     12  play(video) {
     13    let playPauseButton = document.querySelector(
     14      `[data-player-hook="btnplaypause"]`
     15    );
     16    if (video.paused) {
     17      playPauseButton?.click();
     18    }
     19  }
     20 
     21  pause(video) {
     22    let playPauseButton = document.querySelector(
     23      `[data-player-hook="btnplaypause"]`
     24    );
     25    if (!video.paused) {
     26      playPauseButton?.click();
     27    }
     28  }
     29 
     30  setCurrentTime(video, position) {
     31    this.player.seek(position);
     32  }
     33 
     34  setCaptionContainerObserver(video, updateCaptionsFunction) {
     35    let container = document.querySelector(`[data-player-hook="plgcontainer"]`);
     36 
     37    const selector = [
     38      `[data-player-hook="subtitleelem"]`,
     39      ".subtitle_text",
     40    ].join(",");
     41 
     42    if (container) {
     43      updateCaptionsFunction("");
     44      const callback = mutationList => {
     45        if (mutationList) {
     46          let changed = false;
     47          for (const mutation of mutationList) {
     48            if (mutation.target.matches?.(selector)) {
     49              changed = true;
     50              break;
     51            }
     52          }
     53 
     54          if (!changed) {
     55            return;
     56          }
     57        }
     58 
     59        let subtitles = container.querySelectorAll(
     60          `.iqp-subtitle:has(> [data-player-hook="subtitleelem"] > .subtitle_text)`
     61        );
     62        if (!subtitles.length) {
     63          updateCaptionsFunction("");
     64          return;
     65        }
     66 
     67        if (subtitles.length > 1) {
     68          subtitles = Array.from(subtitles).sort(
     69            (a, b) => a.offsetTop - b.offsetTop
     70          );
     71        }
     72 
     73        updateCaptionsFunction(
     74          Array.from(subtitles, x => x.innerText.trim())
     75            .filter(String)
     76            .join("\n")
     77        );
     78      };
     79 
     80      callback();
     81 
     82      this.captionsObserver = new MutationObserver(callback);
     83 
     84      this.captionsObserver.observe(container, {
     85        childList: true,
     86        subtree: true,
     87      });
     88    }
     89  }
     90 
     91  removeCaptionContainerObserver() {
     92    this.captionsObserver?.disconnect();
     93  }
     94 }
     95 
     96 this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;