Skip to content

Commit

Permalink
add label to play list
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Jan 15, 2025
1 parent bab2fbd commit 55f3a16
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions public/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,11 @@ input::-webkit-inner-spin-button {
display: inline;
}

.pod-item #sponsorThumbnailLabel {
position: relative;
margin: 0;
}

.sponsorblock-chapter-visible {
display: inherit !important;
}
Expand Down
17 changes: 15 additions & 2 deletions src/thumbnail-utils/thumbnail-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ThumbnailSelector {
customLinkSelector?: string;
customLinkAttribute?: string;
labelAnchorSelector?: string;
waitForPageLoad?: boolean;
}

// TODO: support customLinkSelector
Expand Down Expand Up @@ -42,10 +43,18 @@ const thumbnailSelectors: { [key: string]: ThumbnailSelector } = {
},
"playerListCard": {
// 播放页播放列表
// TODO: 无法获取视频链接
containerSelector: "video-sections-v1",
thumbnailSelector: ".video-episode-card",
},
"playerListPod": {
// 播放页播放列表,文字形式
containerSelector: "div.video-pod",
thumbnailSelector: "div.pod-item",
customLinkSelector: "div.pod-item",
customLinkAttribute: "data-key",
labelAnchorSelector: "div > div > div",
waitForPageLoad: true,
},
"listPlayerListCard": {
// 列表播放页播放列表
// TODO: 无法获取视频链接
Expand Down Expand Up @@ -106,7 +115,7 @@ const pageTypeSepecialSelector: { [key in PageType]: string[] } = {
[PageType.Main]: ["mainPageRecommendation"],
[PageType.History]: ["history"],
[PageType.OldHistory]: ["oldHistory"],
[PageType.Video]: ["playerSideRecommendation", "playerListCard"],
[PageType.Video]: ["playerSideRecommendation", "playerListCard", "playerListPod"],
[PageType.List]: ["listPlayerSideRecommendation", "listPlayerListCard"],
[PageType.Search]: ["search"],
[PageType.Dynamic]: ["dynamic"],
Expand Down Expand Up @@ -156,3 +165,7 @@ export function getLabelAnchorSelector(containerType: string) {
"div:not(.b-img--face) > picture img:not(.bili-avatar-img), div.bili-cover-card__thumbnail > img"
);
}

export function waitForPageLoad(containerType: string): boolean {
return thumbnailSelectors[containerType].waitForPageLoad ?? false;
}
11 changes: 10 additions & 1 deletion src/thumbnail-utils/thumbnails.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Config from "../config";
import { getPageLoaded } from "../content";
import { getVideoLabel } from "../requests/videoLabels";
import { waitFor } from "../utils/";
import { getBvIDFromURL } from "../utils/parseVideoID";
import { getLabelAnchorSelector, getLinkAttribute, getLinkSelectors } from "./thumbnail-selectors";
import {
getLabelAnchorSelector,
getLinkAttribute,
getLinkSelectors,
waitForPageLoad as shouldWaitForPageLoad,
} from "./thumbnail-selectors";

export async function labelThumbnails(thumbnails: HTMLElement[], containerType: string): Promise<void> {
await Promise.all(thumbnails.map((t) => labelThumbnail(t as HTMLElement, containerType)));
Expand Down Expand Up @@ -111,6 +117,9 @@ async function createOrGetThumbnail(
const labelAnchor =
(await waitFor(() => thumbnail.querySelector(getLabelAnchorSelector(containerType)), 10000, 1000)) ??
thumbnail.lastChild;
if (shouldWaitForPageLoad(containerType)) {
await waitFor(getPageLoaded, 30000, 1000);
}
labelAnchor.after(overlay);

return {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/parseVideoID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ export function getBvIDFromWindow(timeout = 200): Promise<VideoID | null> {
}

const BILIBILI_VIDEO_URL_REGEX = /^\/video\/((BV1[a-zA-Z0-9]{9})|(av\d+))\/?/;
const BVID_REGEX = /^(BV1[a-zA-Z0-9]{9})$/;
/**
* Parse without side effects
*/
export async function getBvIDFromURL(url: string): Promise<VideoID | null> {
url = url.trim();
// check if is bvid already
if (BVID_REGEX.test(url)) {
return url as VideoID;
}

//Attempt to parse url
let urlObject: URL | null = null;
try {
Expand Down

0 comments on commit 55f3a16

Please sign in to comment.