Fix enabling/disabling extension while switching between environments

This commit is contained in:
Tamius Han 2025-01-29 23:58:16 +01:00
parent 208a360c47
commit a92f5fc2a1
7 changed files with 104 additions and 8 deletions

View File

@ -6,6 +6,12 @@ import ExtensionMode from '../enums/ExtensionMode.enum'
import StretchType from '../enums/StretchType.enum' import StretchType from '../enums/StretchType.enum'
import VideoAlignmentType from '../enums/VideoAlignmentType.enum' import VideoAlignmentType from '../enums/VideoAlignmentType.enum'
export enum ExtensionEnvironment {
Normal = 'normal',
Theater = 'theater',
Fullscreen = 'fullscreen',
}
export interface KeyboardShortcutInterface { export interface KeyboardShortcutInterface {
key?: string, key?: string,
code?: string, code?: string,

View File

@ -36,7 +36,7 @@ export default {
}, },
methods: { methods: {
playerDimensionsUpdate(dimensions) { playerDimensionsUpdate(dimensions) {
if (!dimensions.width || !dimensions.height) { if (!dimensions?.width || !dimensions?.height) {
this.playerDimensions = undefined; this.playerDimensions = undefined;
} }
if (dimensions?.width !== this.playerDimensions?.width || dimensions?.height !== this.playerDimensions?.height) { if (dimensions?.width !== this.playerDimensions?.width || dimensions?.height !== this.playerDimensions?.height) {

View File

@ -177,6 +177,9 @@ class Settings {
} }
private applySettingsPatches(oldVersion, patches) { private applySettingsPatches(oldVersion, patches) {
let index = this.findFirstNecessaryPatch(oldVersion, patches); let index = this.findFirstNecessaryPatch(oldVersion, patches);
console.log('ExtConfPatches — last unapplied patch is', index, patches[index]. patches);
if (index === -1) { if (index === -1) {
this.logger?.log('info','settings','[Settings::applySettingsPatches] There are no pending conf patches.'); this.logger?.log('info','settings','[Settings::applySettingsPatches] There are no pending conf patches.');
return; return;

View File

@ -1,7 +1,10 @@
import AspectRatioType from '../../../common/enums/AspectRatioType.enum'; import AspectRatioType from '../../../common/enums/AspectRatioType.enum';
import ExtensionMode from '../../../common/enums/ExtensionMode.enum';
import { ExtensionEnvironment } from '../../../common/interfaces/SettingsInterface';
import EventBus from '../EventBus'; import EventBus from '../EventBus';
import Logger from '../Logger'; import Logger from '../Logger';
import Settings from '../Settings'; import Settings from '../Settings';
import { SiteSettings } from '../settings/SiteSettings';
import VideoData from '../video-data/VideoData'; import VideoData from '../video-data/VideoData';
import { Corner } from './enums/corner.enum'; import { Corner } from './enums/corner.enum';
import { VideoPlaybackState } from './enums/video-playback-state.enum'; import { VideoPlaybackState } from './enums/video-playback-state.enum';
@ -218,10 +221,17 @@ export class Aard {
private logger: Logger; private logger: Logger;
private videoData: VideoData; private videoData: VideoData;
private settings: Settings; private settings: Settings;
private siteSettings: SiteSettings;
private eventBus: EventBus; private eventBus: EventBus;
private arid: string; private arid: string;
private eventBusCommands = { private eventBusCommands = {
'uw-environment-change': {
function: (newEnvironment: ExtensionEnvironment) => {
console.log('received extension environment:', newEnvironment, 'player env:', this.videoData?.player?.environment);
this.startCheck();
}
}
// 'get-aard-timing': { // 'get-aard-timing': {
// function: () => this.handlePerformanceDataRequest() // function: () => this.handlePerformanceDataRequest()
// } // }
@ -267,6 +277,7 @@ export class Aard {
this.videoData = videoData; this.videoData = videoData;
this.video = videoData.video; this.video = videoData.video;
this.settings = videoData.settings; this.settings = videoData.settings;
this.siteSettings = videoData.siteSettings;
this.eventBus = videoData.eventBus; this.eventBus = videoData.eventBus;
this.eventBus.subscribeMulti(this.eventBusCommands, this); this.eventBus.subscribeMulti(this.eventBusCommands, this);
@ -284,7 +295,6 @@ export class Aard {
* This method should only ever be called from constructor. * This method should only ever be called from constructor.
*/ */
private init() { private init() {
this.canvasStore = { this.canvasStore = {
main: this.createCanvas('main-gl') main: this.createCanvas('main-gl')
}; };
@ -301,7 +311,7 @@ export class Aard {
), ),
}; };
this.start(); this.startCheck();
} }
private createCanvas(canvasId: string, canvasType?: 'webgl' | 'fallback') { private createCanvas(canvasId: string, canvasType?: 'webgl' | 'fallback') {
@ -341,6 +351,20 @@ export class Aard {
} }
//#endregion //#endregion
/**
* Checks whether autodetection can run
*/
startCheck() {
if (!this.videoData.player) {
console.warn('Player not detected!')
}
if (this.siteSettings.data.enableAard[this.videoData.player.environment] === ExtensionMode.Enabled) {
this.start();
} else {
this.stop();
}
}
/** /**
* Starts autodetection loop. * Starts autodetection loop.
*/ */

View File

@ -13,6 +13,7 @@ import UI from '../uwui/UI';
import { SiteSettings } from '../settings/SiteSettings'; import { SiteSettings } from '../settings/SiteSettings';
import PageInfo from './PageInfo'; import PageInfo from './PageInfo';
import { RunLevel } from '../../enum/run-level.enum'; import { RunLevel } from '../../enum/run-level.enum';
import { ExtensionEnvironment } from '../../../common/interfaces/SettingsInterface';
if (process.env.CHANNEL !== 'stable'){ if (process.env.CHANNEL !== 'stable'){
console.info("Loading: PlayerData.js"); console.info("Loading: PlayerData.js");
@ -138,6 +139,7 @@ class PlayerData {
private dimensionChangeListener = { private dimensionChangeListener = {
that: this, that: this,
handleEvent: function(event: Event) { handleEvent: function(event: Event) {
this.that.trackEnvironmentChanges(event);
this.that.trackDimensionChanges() this.that.trackDimensionChanges()
} }
} }
@ -152,6 +154,7 @@ class PlayerData {
} }
if (!this.dimensions) { if (!this.dimensions) {
this.trackDimensionChanges(); this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
return this.dimensions.width / this.dimensions.height; return this.dimensions.width / this.dimensions.height;
@ -161,6 +164,21 @@ class PlayerData {
} }
} }
/**
* Gets current environment (needed when determining whether extension runs in fulls screen, theater, or normal)
*/
private lastEnvironment: ExtensionEnvironment;
get environment(): ExtensionEnvironment {
if (this.isFullscreen) {
return ExtensionEnvironment.Fullscreen;
}
if (this.isTheaterMode) {
return ExtensionEnvironment.Theater;
}
return ExtensionEnvironment.Normal;
}
//#region lifecycle //#region lifecycle
constructor(videoData) { constructor(videoData) {
try { try {
@ -198,6 +216,7 @@ class PlayerData {
} }
this.trackDimensionChanges(); this.trackDimensionChanges();
this.trackEnvironmentChanges();
this.startChangeDetection(); this.startChangeDetection();
document.addEventListener('fullscreenchange', this.dimensionChangeListener); document.addEventListener('fullscreenchange', this.dimensionChangeListener);
@ -337,6 +356,13 @@ class PlayerData {
return newTheaterMode; return newTheaterMode;
} }
trackEnvironmentChanges() {
if (this.environment !== this.lastEnvironment) {
this.lastEnvironment = this.environment;
this.eventBus.send('uw-environment-change', {newEnvironment: this.environment});
}
}
/** /**
* *
*/ */
@ -439,6 +465,7 @@ class PlayerData {
onPlayerDimensionsChanged(mutationList?, observer?) { onPlayerDimensionsChanged(mutationList?, observer?) {
this.trackDimensionChanges(); this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
@ -630,6 +657,7 @@ class PlayerData {
this.element = elementStack[nextIndex].element; this.element = elementStack[nextIndex].element;
this.trackDimensionChanges(); this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
} }
@ -834,7 +862,9 @@ class PlayerData {
this.ui = undefined; this.ui = undefined;
this.element = this.getPlayer(); this.element = this.getPlayer();
// this.notificationService?.replace(this.element); // this.notificationService?.replace(this.element);
this.trackDimensionChanges(); this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
} }

View File

@ -17,6 +17,8 @@ import { ExtensionStatus } from './ExtensionStatus';
import { RunLevel } from '../../enum/run-level.enum'; import { RunLevel } from '../../enum/run-level.enum';
import { Aard } from '../aard/Aard'; import { Aard } from '../aard/Aard';
import { Stretch } from '../../../common/interfaces/StretchInterface'; import { Stretch } from '../../../common/interfaces/StretchInterface';
import ExtensionMode from '../../../common/enums/ExtensionMode.enum';
import { ExtensionEnvironment } from '../../../common/interfaces/SettingsInterface';
/** /**
* VideoData handles CSS for the video element. * VideoData handles CSS for the video element.
@ -76,6 +78,8 @@ class VideoData {
eventBus: EventBus; eventBus: EventBus;
extensionStatus: ExtensionStatus; extensionStatus: ExtensionStatus;
private currentEnvironment: ExtensionEnvironment;
//#endregion //#endregion
@ -97,6 +101,11 @@ class VideoData {
}, },
'set-run-level': { 'set-run-level': {
function: (runLevel: RunLevel) => this.setRunLevel(runLevel) function: (runLevel: RunLevel) => this.setRunLevel(runLevel)
},
'uw-environment-change': {
function: () => {
this.onEnvironmentChanged();
}
} }
} }
@ -191,7 +200,7 @@ class VideoData {
if (!this.mutationObserver) { if (!this.mutationObserver) {
this.setupMutationObserver(); this.setupMutationObserver();
} }
this.eventBus.send('inject-css', this.baseVideoCss); // this.eventBus.send('inject-css', this.baseVideoCss);
} catch (e) { } catch (e) {
console.error('Failed to inject base css!', e); console.error('Failed to inject base css!', e);
} }
@ -389,8 +398,26 @@ class VideoData {
} }
//#endregion //#endregion
onEnvironmentChanged() {
if (!this.player) {
return;
}
if (this.currentEnvironment !== this.player.environment) {
console.warn('environment changed to:', this.player.environment);
this.currentEnvironment = this.player.environment;
if (this.siteSettings.data.enable[this.player.environment] === ExtensionMode.Disabled) {
this.setRunLevel(RunLevel.Off);
} else {
this.restoreAr();
}
}
}
setRunLevel(runLevel: RunLevel, options?: {fromPlayer?: boolean}) { setRunLevel(runLevel: RunLevel, options?: {fromPlayer?: boolean}) {
if (this.player && this.siteSettings.data.enable[this.player.environment] !== ExtensionMode.Enabled) {
runLevel = RunLevel.Off;
}
if (this.runLevel === runLevel) { if (this.runLevel === runLevel) {
return; // also no need to propagate to the player return; // also no need to propagate to the player
} }
@ -553,6 +580,8 @@ class VideoData {
// the 'style' attributes don't necessarily trigger. This means we also need to trigger // the 'style' attributes don't necessarily trigger. This means we also need to trigger
// restoreAr here, in case video size was changed this way // restoreAr here, in case video size was changed this way
this.player.forceRefreshPlayerElement(); this.player.forceRefreshPlayerElement();
this.eventBus.send('uw-environment-change', {newEnvironment: this.player.environment});
this.restoreAr(); this.restoreAr();
// sometimes something fucky wucky happens and mutations aren't detected correctly, so we // sometimes something fucky wucky happens and mutations aren't detected correctly, so we
@ -571,7 +600,7 @@ class VideoData {
this._processDimensionsChanged, this._processDimensionsChanged,
250, 250,
{ {
leading: true, // leading: true,
trailing: true trailing: true
} }
); );
@ -694,7 +723,7 @@ class VideoData {
if (!this.aard) { if (!this.aard) {
this.initArDetection(); this.initArDetection();
} }
this.aard.start(); this.aard.startCheck();
} catch (e) { } catch (e) {
this.logger.log('warn', 'debug', '[VideoData::startArDetection()] Could not start aard for some reason. Was the function was called too early?', e); this.logger.log('warn', 'debug', '[VideoData::startArDetection()] Could not start aard for some reason. Was the function was called too early?', e);
} }

View File

@ -295,7 +295,7 @@ class Resizer {
// handle autodetection stuff // handle autodetection stuff
if (ar.type === AspectRatioType.Automatic) { if (ar.type === AspectRatioType.Automatic) {
this.videoData.aard?.start(); this.videoData.aard?.startCheck();
return; return;
} else if (ar.type !== AspectRatioType.AutomaticUpdate) { } else if (ar.type !== AspectRatioType.AutomaticUpdate) {
this.videoData.aard?.stop(); this.videoData.aard?.stop();
@ -429,7 +429,7 @@ class Resizer {
} }
applyScaling(stretchFactors: VideoDimensions, options?: {noAnnounce?: boolean, ar?: Ar}) { applyScaling(stretchFactors: VideoDimensions, options?: {noAnnounce?: boolean, ar?: Ar}) {
this.stretcher.chromeBugMitigation(stretchFactors); // this.stretcher.chromeBugMitigation(stretchFactors);
// let the UI know // let the UI know
if(!options?.noAnnounce) { if(!options?.noAnnounce) {
@ -533,6 +533,10 @@ class Resizer {
this.restore(); this.restore();
} }
/**
* Restores aspect ratio to last known aspect ratio
* @returns
*/
restore() { restore() {
if (!this.manualZoom) { if (!this.manualZoom) {
this.logger.log('info', 'debug', "[Resizer::restore] <rid:"+this.resizerId+"> attempting to restore aspect ratio", {'lastAr': this.lastAr} ); this.logger.log('info', 'debug', "[Resizer::restore] <rid:"+this.resizerId+"> attempting to restore aspect ratio", {'lastAr': this.lastAr} );