Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #72: Refactor thumbnail label update methods #73

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/thumbnail-utils/thumbnailManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { waitFor } from "../utils/";
import { addCleanupListener } from "../utils/cleanup";
import { getPageType } from "../utils/video";
import { getThumbnailContainerElements, getThumbnailLink, getThumbnailSelectors } from "./thumbnail-selectors";
import { insertSBIconDefinition, labelThumbnails } from "./thumbnails";
import { insertSBIconDefinition, labelThumbnail, labelThumbnails } from "./thumbnails";

export type ThumbnailListener = (newThumbnails: HTMLElement[]) => void;

Expand Down Expand Up @@ -83,15 +83,15 @@ export function newThumbnails() {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "attributes" && mutation.attributeName === "href") {
labelThumbnails([thumbnail]);
labelThumbnail(thumbnail as HTMLImageElement, mutation.oldValue);
break;
}
}
});
handledThumbnailsObserverMap.set(thumbnail, observer);

const link = getThumbnailLink(thumbnail);
if (link) observer.observe(link, { attributes: true });
if (link) observer.observe(link, { attributes: true, attributeOldValue: true, attributeFilter: ["href"] });
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/thumbnail-utils/thumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export async function labelThumbnails(thumbnails: HTMLElement[]): Promise<void>
await Promise.all(thumbnails.map((t) => labelThumbnail(t as HTMLImageElement)));
}

export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLElement | null> {
export async function labelThumbnail(
thumbnail: HTMLImageElement,
thumbnailOldHref?: string
): Promise<HTMLElement | null> {
if (!Config.config?.fullVideoSegments || !Config.config?.fullVideoLabelsOnThumbnails) {
hideThumbnailLabel(thumbnail);
return null;
Expand All @@ -28,14 +31,22 @@ export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLE
}
const [videoID] = videoIDs;

// 获取或创建缩略图标签
const { overlay, text } = await createOrGetThumbnail(thumbnail);

if (thumbnailOldHref) {
const oldVideoID = getBvIDFromURL(thumbnailOldHref);
if (oldVideoID && oldVideoID == videoID) {
return overlay;
}
}

const category = await getVideoLabel(videoID);
if (!category) {
hideThumbnailLabel(thumbnail);
return null;
}

const { overlay, text } = await createOrGetThumbnail(thumbnail);

overlay.style.setProperty(
"--category-color",
`var(--sb-category-preview-${category}, var(--sb-category-${category}))`
Expand All @@ -51,7 +62,7 @@ export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLE
}

function getOldThumbnailLabel(thumbnail: HTMLImageElement): HTMLElement | null {
return thumbnail.querySelector(".sponsorThumbnailLabel") as HTMLElement | null;
return thumbnail.querySelector("#sponsorThumbnailLabel") as HTMLElement | null;
}

function hideThumbnailLabel(thumbnail: HTMLImageElement): void {
Expand Down