Skip to content

Commit 7b033b5

Browse files
fix(ambient-mode): fix ambient-mode not working for videos after restart (#2294)
* Fix Ambient Mode not working for videos after restart (#2255) This should fix #1641 * fix: fix waitForElement --------- Co-authored-by: craftgeil <[email protected]>
1 parent 8924ec2 commit 7b033b5

File tree

4 files changed

+24
-33
lines changed

4 files changed

+24
-33
lines changed

src/plugins/album-actions/index.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { t } from '@/i18n';
22
import { createPlugin } from '@/utils';
33
import { ElementFromHtml } from '@/plugins/utils/renderer';
4+
import { waitForElement } from '@/utils/wait-for-element';
45

56
import undislikeHTML from './templates/undislike.html?raw';
67
import dislikeHTML from './templates/dislike.html?raw';
@@ -16,7 +17,6 @@ export default createPlugin<
1617
changeObserver?: MutationObserver;
1718
waiting: boolean;
1819
onPageChange(): void;
19-
waitForElem(selector: string): Promise<HTMLElement>;
2020
loadFullList: (event: MouseEvent) => void;
2121
applyToList(id: string, loader: HTMLElement): void;
2222
start(): void;
@@ -50,7 +50,7 @@ export default createPlugin<
5050
} else {
5151
this.waiting = true;
5252
}
53-
const continuations = await this.waitForElem('#continuations');
53+
const continuations = await waitForElement<HTMLElement>('#continuations');
5454
this.waiting = false;
5555
//Gets the for buttons
5656
const buttons: Array<HTMLElement> = [
@@ -183,16 +183,5 @@ export default createPlugin<
183183
button.remove();
184184
}
185185
},
186-
waitForElem(selector: string) {
187-
return new Promise((resolve) => {
188-
const interval = setInterval(() => {
189-
const elem = document.querySelector<HTMLElement>(selector);
190-
if (!elem) return;
191-
192-
clearInterval(interval);
193-
resolve(elem);
194-
});
195-
});
196-
},
197186
},
198187
});

src/plugins/ambient-mode/index.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { t } from '@/i18n';
44
import { createPlugin } from '@/utils';
55
import { menu } from './menu';
66
import { AmbientModePluginConfig } from './types';
7+
import { waitForElement } from '@/utils/wait-for-element';
78

89
const defaultConfig: AmbientModePluginConfig = {
910
enabled: false,
@@ -36,7 +37,7 @@ export default createPlugin({
3637
unregister: null as (() => void) | null,
3738
update: null as (() => void) | null,
3839
interval: null as NodeJS.Timeout | null,
39-
lastMediaType: null as "video" | "image" | null,
40+
lastMediaType: null as 'video' | 'image' | null,
4041
lastVideoSource: null as string | null,
4142
lastImageSource: null as string | null,
4243

@@ -53,7 +54,8 @@ export default createPlugin({
5354
const songImage = document.querySelector<HTMLImageElement>('#song-image');
5455
const songVideo = document.querySelector<HTMLDivElement>('#song-video');
5556
const image = songImage?.querySelector<HTMLImageElement>('yt-img-shadow > img');
56-
const video = songVideo?.querySelector<HTMLVideoElement>('.html5-video-container > video');
57+
const video = await waitForElement<HTMLVideoElement>('.html5-video-container > video');
58+
5759
const videoWrapper = document.querySelector('#song-video > .player-wrapper');
5860

5961
const injectBlurImage = () => {
@@ -179,12 +181,12 @@ export default createPlugin({
179181
const isVideoMode = () => {
180182
const songVideo = document.querySelector<HTMLDivElement>('#song-video');
181183
if (!songVideo) {
182-
this.lastMediaType = "image";
184+
this.lastMediaType = 'image';
183185
return false;
184186
}
185187

186188
const isVideo = getComputedStyle(songVideo).display !== 'none';
187-
this.lastMediaType = isVideo ? "video" : "image";
189+
this.lastMediaType = isVideo ? 'video' : 'image';
188190
return isVideo;
189191
};
190192

@@ -196,16 +198,16 @@ export default createPlugin({
196198
if (isPageOpen) {
197199
const isVideo = isVideoMode();
198200
if (!force) {
199-
if (this.lastMediaType === "video" && this.lastVideoSource === video?.src) return false;
200-
if (this.lastMediaType === "image" && this.lastImageSource === image?.src) return false;
201+
if (this.lastMediaType === 'video' && this.lastVideoSource === video?.src) return false;
202+
if (this.lastMediaType === 'image' && this.lastImageSource === image?.src) return false;
201203
}
202204
this.unregister?.();
203205
this.unregister = (isVideo ? injectBlurVideo() : injectBlurImage()) ?? null;
204206
} else {
205207
this.unregister?.();
206208
this.unregister = null;
207209
}
208-
}
210+
};
209211

210212
/* needed for switching between different views (e.g. miniplayer) */
211213
const observer = new MutationObserver((mutationsList) => {

src/plugins/skip-disliked-songs/index.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { t } from '@/i18n';
22
import { createPlugin } from '@/utils';
3+
import { waitForElement } from '@/utils/wait-for-element';
34

45
export default createPlugin<
56
unknown,
67
unknown,
78
{
89
observer?: MutationObserver;
9-
waitForElem(selector: string): Promise<HTMLElement>;
1010
start(): void;
1111
stop(): void;
1212
}
@@ -15,19 +15,8 @@ export default createPlugin<
1515
description: () => t('plugins.skip-disliked-songs.description'),
1616
restartNeeded: false,
1717
renderer: {
18-
waitForElem(selector: string) {
19-
return new Promise<HTMLElement>((resolve) => {
20-
const interval = setInterval(() => {
21-
const elem = document.querySelector<HTMLElement>(selector);
22-
if (!elem) return;
23-
24-
clearInterval(interval);
25-
resolve(elem);
26-
});
27-
});
28-
},
2918
start() {
30-
this.waitForElem('#dislike-button-renderer').then((dislikeBtn) => {
19+
waitForElement<HTMLElement>('#dislike-button-renderer').then((dislikeBtn) => {
3120
this.observer = new MutationObserver(() => {
3221
if (dislikeBtn?.getAttribute('like-status') == 'DISLIKE') {
3322
document

src/utils/wait-for-element.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const waitForElement = <T extends Element>(selector: string): Promise<T> => {
2+
return new Promise<T>((resolve) => {
3+
const interval = setInterval(() => {
4+
const elem = document.querySelector<T>(selector);
5+
if (!elem) return;
6+
7+
clearInterval(interval);
8+
resolve(elem);
9+
});
10+
});
11+
};

0 commit comments

Comments
 (0)