Change handling of getPlayer()

This commit is contained in:
Tamius Han 2025-03-30 23:49:13 +02:00
parent ab79f1e0e6
commit f0840b78e1

View File

@ -92,10 +92,10 @@ class PlayerData {
isTheaterMode: boolean = false; // note: fullscreen mode will count as theaterMode if player was in theater mode before fs switch. This is desired, so far. isTheaterMode: boolean = false; // note: fullscreen mode will count as theaterMode if player was in theater mode before fs switch. This is desired, so far.
isTooSmall: boolean = true; isTooSmall: boolean = true;
//#endregion
//#region misc stuff //#region misc stuff
extensionMode: any; extensionMode: any;
dimensions: PlayerDimensions; dimensions: PlayerDimensions;
private playerIdElement: any;
private observer: ResizeObserver; private observer: ResizeObserver;
private trackChangesTimeout: any; private trackChangesTimeout: any;
@ -123,7 +123,7 @@ class PlayerData {
function: (data) => this.markElement(data) function: (data) => this.markElement(data)
}], }],
'update-player': [{ 'update-player': [{
function: () => this.getPlayer() function: () => this.updatePlayer()
}], }],
'set-run-level': [{ 'set-run-level': [{
function: (runLevel) => this.setRunLevel(runLevel) function: (runLevel) => this.setRunLevel(runLevel)
@ -179,6 +179,14 @@ class PlayerData {
return ExtensionEnvironment.Normal; return ExtensionEnvironment.Normal;
} }
/**
*
* END OF PROPERTIES
*
*
*/
//#region lifecycle //#region lifecycle
constructor(videoData) { constructor(videoData) {
try { try {
@ -192,14 +200,10 @@ class PlayerData {
// do the rest // do the rest
this.invalid = false; this.invalid = false;
this.element = this.getPlayer(); this.updatePlayer();
this.isTooSmall = (this.element.clientWidth < 1208 || this.element.clientHeight < 720); this.isTooSmall = (this.element.clientWidth < 1208 || this.element.clientHeight < 720);
this.initEventBus(); this.initEventBus();
// we defer UI creation until player element is big enough
// this happens in trackDimensionChanges!
this.dimensions = undefined; this.dimensions = undefined;
this.periodicallyRefreshPlayerElement = false; this.periodicallyRefreshPlayerElement = false;
@ -215,8 +219,6 @@ class PlayerData {
return; return;
} }
this.trackDimensionChanges();
this.trackEnvironmentChanges();
this.startChangeDetection(); this.startChangeDetection();
document.addEventListener('fullscreenchange', this.dimensionChangeListener); document.addEventListener('fullscreenchange', this.dimensionChangeListener);
@ -231,7 +233,7 @@ class PlayerData {
private reloadPlayerDataConfig(siteConfUpdate) { private reloadPlayerDataConfig(siteConfUpdate) {
// this.siteSettings = siteConfUpdate; // this.siteSettings = siteConfUpdate;
this.element = this.getPlayer(); this.updatePlayer();
this.periodicallyRefreshPlayerElement = false; this.periodicallyRefreshPlayerElement = false;
try { try {
@ -271,6 +273,7 @@ class PlayerData {
return; return;
} }
if ( if (
this.isFullscreen this.isFullscreen
|| ( || (
@ -279,6 +282,7 @@ class PlayerData {
&& playerDimensions.height > 720 && playerDimensions.height > 720
) )
) { ) {
this.ui = new UI( this.ui = new UI(
'ultrawidifyUi', 'ultrawidifyUi',
{ {
@ -574,10 +578,38 @@ class PlayerData {
return this.elementStack; return this.elementStack;
} }
updatePlayer(options?: {verbose?: boolean, newElement?: HTMLElement}) {
const newPlayer = options?.newElement ?? this.getPlayer(options);
if (newPlayer === this.element) {
return;
}
// clean up and re-initialize UI
this.ui?.destroy();
delete this.ui;
this.element = newPlayer;
this.ui = new UI(
'ultrawidifyUi',
{
parentElement: this.element,
eventBus: this.eventBus,
playerData: this,
uiSettings: this.videoData.settings.active.ui
}
);
this.trackDimensionChanges();
this.trackEnvironmentChanges();
}
/** /**
* Finds and returns HTML element of the player * Finds and returns HTML element of the player
*/ */
getPlayer(options?: {verbose?: boolean}): HTMLElement { private getPlayer(options?: {verbose?: boolean}): HTMLElement {
const host = window.location.hostname; const host = window.location.hostname;
let element = this.videoElement.parentNode; let element = this.videoElement.parentNode;
const videoWidth = this.videoElement.offsetWidth; const videoWidth = this.videoElement.offsetWidth;
@ -652,12 +684,7 @@ class PlayerData {
console.log('updated site settings:', this.siteSettings.data.playerAutoConfig); console.log('updated site settings:', this.siteSettings.data.playerAutoConfig);
this.videoData.settings.saveWithoutReload(); this.videoData.settings.saveWithoutReload();
this.ui?.destroy(); this.updatePlayer({newElement: elementStack[nextIndex].element});
this.ui = undefined;
this.element = elementStack[nextIndex].element;
this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
} }
@ -817,7 +844,7 @@ class PlayerData {
*/ */
private handlePlayerTreeRequest() { private handlePlayerTreeRequest() {
// this populates this.elementStack fully // this populates this.elementStack fully
this.getPlayer({verbose: true}); this.updatePlayer({verbose: true});
console.log('tree:', JSON.parse(JSON.stringify(this.elementStack))); console.log('tree:', JSON.parse(JSON.stringify(this.elementStack)));
console.log('————————————————————— handling player tree request!') console.log('————————————————————— handling player tree request!')
this.eventBus.send('uw-config-broadcast', {type: 'player-tree', config: JSON.parse(JSON.stringify(this.elementStack))}); this.eventBus.send('uw-config-broadcast', {type: 'player-tree', config: JSON.parse(JSON.stringify(this.elementStack))});
@ -858,13 +885,7 @@ class PlayerData {
} }
forceRefreshPlayerElement() { forceRefreshPlayerElement() {
this.ui?.destroy(); this.updatePlayer();
this.ui = undefined;
this.element = this.getPlayer();
// this.notificationService?.replace(this.element);
this.trackDimensionChanges();
this.trackEnvironmentChanges();
} }
} }