From cd4905ef5ce62f52fe960a22cc5536a4beb95116 Mon Sep 17 00:00:00 2001 From: hanyd Date: Wed, 15 Jan 2025 15:01:29 +0800 Subject: [PATCH] handle multiple thumbnail label insertion --- src/thumbnail-utils/thumbnails.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/thumbnail-utils/thumbnails.ts b/src/thumbnail-utils/thumbnails.ts index 72d23c9a..18e47c49 100644 --- a/src/thumbnail-utils/thumbnails.ts +++ b/src/thumbnail-utils/thumbnails.ts @@ -33,7 +33,7 @@ export async function labelThumbnail(thumbnail: HTMLElement, containerType: stri ); if (videoIDs.size !== 1) { // none or multiple video IDs found - hideThumbnailLabel(thumbnail); + await hideThumbnailLabel(thumbnail); return null; } const [videoID] = videoIDs; @@ -43,7 +43,7 @@ export async function labelThumbnail(thumbnail: HTMLElement, containerType: stri const category = await getVideoLabel(videoID); if (!category) { - hideThumbnailLabel(thumbnail); + await hideThumbnailLabel(thumbnail); return null; } @@ -65,12 +65,12 @@ export async function labelThumbnail(thumbnail: HTMLElement, containerType: stri return overlay; } -function getOldThumbnailLabel(thumbnail: HTMLElement): HTMLElement | null { - return thumbnail.querySelector("#sponsorThumbnailLabel") as HTMLElement | null; +async function getOldThumbnailLabel(thumbnail: HTMLElement): Promise { + return waitFor(() => thumbnail.querySelector("#sponsorThumbnailLabel"), 50, 5).catch(() => null); } -function hideThumbnailLabel(thumbnail: HTMLElement): void { - const oldLabel = getOldThumbnailLabel(thumbnail); +async function hideThumbnailLabel(thumbnail: HTMLElement): Promise { + const oldLabel = await getOldThumbnailLabel(thumbnail); if (oldLabel) { oldLabel.classList.remove("sponsorThumbnailLabelVisible"); oldLabel.removeAttribute("data-category"); @@ -81,7 +81,7 @@ async function createOrGetThumbnail( thumbnail: HTMLElement, containerType: string ): Promise<{ overlay: HTMLElement; text: HTMLElement }> { - const oldLabelElement = getOldThumbnailLabel(thumbnail); + const oldLabelElement = await getOldThumbnailLabel(thumbnail); if (oldLabelElement) { return { overlay: oldLabelElement as HTMLElement,