netflix.js (2570B)
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 let netflixPlayerAPI = 10 window.wrappedJSObject.netflix.appContext.state.playerApp.getAPI() 11 .videoPlayer; 12 let sessionId = null; 13 for (let id of netflixPlayerAPI.getAllPlayerSessionIds()) { 14 if (id.startsWith("watch-")) { 15 sessionId = id; 16 break; 17 } 18 } 19 this.player = netflixPlayerAPI.getVideoPlayerBySessionId(sessionId); 20 } 21 22 /** 23 * The Netflix player returns the current time in milliseconds so we convert 24 * to seconds before returning. 25 * 26 * @returns {number} The current time in seconds 27 */ 28 getCurrentTime() { 29 return this.player.getCurrentTime() / 1000; 30 } 31 32 /** 33 * The Netflix player returns the duration in milliseconds so we convert to 34 * seconds before returning. 35 * 36 * @returns {number} The duration in seconds 37 */ 38 getDuration() { 39 return this.player.getDuration() / 1000; 40 } 41 42 play() { 43 this.player.play(); 44 } 45 46 pause() { 47 this.player.pause(); 48 } 49 50 setCaptionContainerObserver(video, updateCaptionsFunction) { 51 let container = document.querySelector(".watch-video"); 52 53 if (container) { 54 updateCaptionsFunction(""); 55 const callback = function () { 56 let text = container.querySelector(".player-timedtext").innerText; 57 updateCaptionsFunction(text); 58 }; 59 60 // immediately invoke the callback function to add subtitles to the PiP window 61 callback([1], null); 62 63 this.captionsObserver = new MutationObserver(callback); 64 65 this.captionsObserver.observe(container, { 66 attributes: false, 67 childList: true, 68 subtree: true, 69 }); 70 } 71 } 72 73 removeCaptionContainerObserver() { 74 this.captionsObserver?.disconnect(); 75 } 76 77 /** 78 * Set the current time of the video in milliseconds. 79 * 80 * @param {HTMLVideoElement} video The original video element 81 * @param {number} position The new time in seconds 82 */ 83 setCurrentTime(video, position) { 84 this.player.seek(position * 1000); 85 } 86 87 setVolume(video, volume) { 88 this.player.setVolume(volume); 89 } 90 91 getVolume() { 92 return this.player.getVolume(); 93 } 94 95 setMuted(video, shouldMute) { 96 this.player.setMuted(shouldMute); 97 } 98 99 isMuted() { 100 return this.player.isMuted(); 101 } 102 } 103 104 this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;