import Debug from '../../conf/Debug'; import PlayerData from './PlayerData'; import Resizer from '../video-transform/Resizer'; import ArDetector from '../ar-detect/ArDetector'; import AspectRatio from '../../../common/enums/aspect-ratio.enum'; class VideoData { constructor(video, settings, pageInfo){ this.vdid = (Math.random()*100).toFixed(); this.logger = pageInfo.logger; this.arSetupComplete = false; this.video = video; this.destroyed = false; this.settings = settings; this.pageInfo = pageInfo; this.extensionMode = pageInfo.extensionMode; this.videoStatusOk = false; this.userCssClassName = `uw-fuck-you-and-do-what-i-tell-you_${this.vdid}`; this.videoLoaded = false; this.videoDimensionsLoaded = true; this.dimensions = { width: this.video.offsetWidth, height: this.video.offsetHeight, }; this.setupStageOne(); } async onVideoLoaded() { if (!this.videoLoaded) { this.logger.log('info', 'init', '%c[VideoData::onVideoLoaded] ——————————— Initiating phase two of videoData setup ———————————', 'color: #0f9'); this.videoLoaded = true; this.videoDimensionsLoaded = true; try { await this.setupStageTwo(); this.logger.log('info', 'init', '%c[VideoData::onVideoLoaded] ——————————— videoData setup stage two complete ———————————', 'color: #0f9'); } catch (e) { this.logger.log('error', 'init', '%c[VideoData::onVideoLoaded] ——————————— Setup stage two failed. ———————————\n', 'color: #f00', e); } } else if (!this.videoDimensionsLoaded) { this.logger.log('info', 'debug', "%c[VideoData::restoreCrop] Recovering from illegal video dimensions. Resetting aspect ratio.", "background: #afd, color: #132"); this.restoreCrop(); this.videoDimensionsLoaded = true; } } async injectBaseCss() { try { await this.pageInfo.injectCss(` .uw-ultrawidify-base-wide-screen { margin: 0px 0px 0px 0px !important; width: initial !important; align-self: start !important; justify-self: start !important; } `); } catch (e) { console.error('Failed to inject base css!', e); } } unsetBaseClass() { this.video.classList.remove('uw-ultrawidify-base-wide-screen'); } async setupStageOne() { this.logger.log('info', 'init', '%c[VideoData::setupStageOne] ——————————— Starting setup stage one! ———————————', 'color: #0f9'); // ensure base css is loaded before doing anything this.injectBaseCss(); // this is in case extension loads before the video this.video.addEventListener('loadeddata', () => { this.logger.log('info', 'init', '[VideoData::ctor->video.onloadeddata] Video fired event "loaded data!"'); this.onVideoLoaded(); }); // this one is in case extension loads after the video is loaded this.video.addEventListener('timeupdate', () => { this.onVideoLoaded(); }); this.logger.log('info', 'init', '%c[VideoData::setupStageOne] ——————————— Setup stage one complete! ———————————', 'color: #0f9'); } async setupStageTwo() { // POZOR: VRSTNI RED JE POMEMBEN (arDetect mora bit zadnji) // NOTE: ORDERING OF OBJ INITIALIZATIONS IS IMPORTANT (arDetect needs to go last) // NOTE: We only init observers once player is confirmed valid const observerConf = { attributes: true, // attributeFilter: ['style', 'class'], attributeOldValue: true, }; this.player = new PlayerData(this); if (this.player.invalid) { this.invalid = true; return; } this.resizer = new Resizer(this); // INIT OBSERVERS this.observer = new MutationObserver( (m, o) => { this.logger.log('info', 'debug', `[VideoData::setupStageTwo->mutationObserver] Mutation observer detected a mutation:`, {m, o}); this.onVideoDimensionsChanged(m, o, this) }); this.observer.observe(this.video, observerConf); // INIT AARD this.arDetector = new ArDetector(this); // this starts Ar detection. needs optional parameter that prevets ardetdctor from starting // player dimensions need to be in: // this.player.dimensions // apply default align and stretch this.logger.log('info', 'debug', "%c[VideoData::ctor] Initial resizer reset!", "background: #afd, color: #132"); this.resizer.reset(); this.logger.log('info', ['debug', 'init'], '[VideoData::ctor] Created videoData with vdid', this.vdid, '\nextension mode:', this.extensionMode) this.pageInfo.initMouseActionHandler(this); // NOTE — since base class for our