Skip to content

Commit

Permalink
get aid map from overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 9, 2024
1 parent 2192807 commit 1484935
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 32 deletions.
23 changes: 23 additions & 0 deletions src/document/aidMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BiliVideoDetail } from "../requests/type/BilibiliRequestType";
import { VideoID } from "../types";
import { DataCache } from "../utils/cache";

const cache = new DataCache<string, VideoID>();

export function saveAidFromDetail(detail: BiliVideoDetail): void {
saveAid(detail.aid, detail.bvid);
}

export function saveAid(aid: number | string, bvid: VideoID): void {
if (typeof aid === "string" && aid.startsWith("av")) {
aid = aid.replace("av", "");
}
cache.set(aid.toString(), bvid);
}

export function getBvid(aid: number | string): VideoID {
if (window?.__INITIAL_STATE__?.aid && window.__INITIAL_STATE__.bvid) {
saveAid(window.__INITIAL_STATE__.aid, window.__INITIAL_STATE__.bvid);
}
return cache.getFromCache(aid.toString());
}
8 changes: 7 additions & 1 deletion src/document/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BilibiliResponse, BiliPlayInfo } from "../requests/type/BilibiliRequestType";
import { BilibiliResponse, BiliPlayInfo, BiliVideoDetail } from "../requests/type/BilibiliRequestType";
import { InjectedScriptMessageSend, sourceId } from "../utils/injectedScriptMessageUtils";
import { getBvid, saveAidFromDetail } from "./aidMap";
import { getFrameRate, playUrlResponseToPlayInfo, savePlayInfo } from "./frameRateUtils";

const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload): void => {
Expand Down Expand Up @@ -54,6 +55,9 @@ function processURLRequest(url: URL, responseText: string): void {
if (cid && response?.data?.dash?.video) {
savePlayInfo(cid, playUrlResponseToPlayInfo(response.data));
}
} else if (url.pathname.includes("/x/player/wbi/v2")) {
const response = JSON.parse(responseText) as BilibiliResponse<BiliVideoDetail>;
saveAidFromDetail(response.data);
}
}

Expand All @@ -71,6 +75,8 @@ function windowMessageListener(message: MessageEvent) {
sendMessageToContent(data, window?.__INITIAL_STATE__?.upData?.mid);
} else if (data.type === "getDescription") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.videoData?.desc);
} else if (data.type === "convertAidToBvid") {
sendMessageToContent(data, getBvid(data.payload as string));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SBObject from "./config";
import { BilibiliResponse, BiliPlayInfo } from "./requests/type/BilibiliRequestType";
import { BilibiliResponse, BiliPlayInfo, BiliVideoDetail } from "./requests/type/BilibiliRequestType";
declare global {
interface Window {
SB: typeof SBObject;
Expand All @@ -8,7 +8,7 @@ declare global {
aid: number;
cid: number;
upData: { mid: string };
videoData: { desc: string };
videoData: BiliVideoDetail;
};
__playinfo__?: BilibiliResponse<BiliPlayInfo>;
}
Expand Down
42 changes: 41 additions & 1 deletion src/requests/type/BilibiliRequestType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,47 @@ export interface BilibiliResponse<T> {
data: T;
}

export interface BiliVideoDetail {}
export interface BiliVideoDetail {
bvid: string;
aid: number;
cid: number;
owner: {
mid: number;
name: string;
face: string;
};

videos: number;
pages: BilibiliPagelistDetail[];

tid: number;
tname: string;
copyright: number;
pic: string;
title: string;
pubdate: number;
ctime: number;
desc: string;
desc_v2: {
raw_text: string;
type: number;
biz_id: number;
}[];
state: number;
duration: number;
}

export interface BilibiliPagelistDetail {
cid: number;
page: number;
from: string;
part: string;
duration: number;
vid: string | null;
weblink: string | null;
dimension: unknown | null;
first_frame: string | null;
}

export interface BiliPlayInfo {
quality: number;
Expand Down
9 changes: 4 additions & 5 deletions src/thumbnail-utils/thumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ export async function labelThumbnail(
// find all links in the thumbnail, reduce to only one video ID
const links = Array.from(thumbnail.querySelectorAll("a[href]")) as Array<HTMLAnchorElement>;
const videoIDs = new Set(
links
.filter((link) => link && link.href)
.map((link) => getBvIDFromURL(link.href))
.filter((id) => id)
(await Promise.allSettled(links.filter((link) => link && link.href).map((link) => getBvIDFromURL(link.href))))
.filter((result) => result.status === "fulfilled")
.map((result) => result.value)
);
if (videoIDs.size !== 1) {
// none or multiple video IDs found
Expand All @@ -35,7 +34,7 @@ export async function labelThumbnail(
const { overlay, text } = await createOrGetThumbnail(thumbnail);

if (thumbnailOldHref) {
const oldVideoID = getBvIDFromURL(thumbnailOldHref);
const oldVideoID = await getBvIDFromURL(thumbnailOldHref);
if (oldVideoID && oldVideoID == videoID) {
return overlay;
}
Expand Down
19 changes: 17 additions & 2 deletions src/utils/injectedScriptMessageUtils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { VideoID } from "./video";

export const sourceId = "biliSponsorBlock";

export interface InjectedScriptMessageBase {
interface InjectedScriptMessageBase {
source: string;
id: string;
type: string;
}

export interface InjectedScriptMessageSend extends InjectedScriptMessageBase {
responseType: string;
payload?: unknown;
}

export interface InjectedScriptMessageRecieve extends InjectedScriptMessageBase {
data: unknown;
}

export interface InjectedScriptMessageType {
interface InjectedScriptMessageType {
sendType: string;
responseType: string;
}

export async function getPropertyFromWindow<T>(
messageType: InjectedScriptMessageType,
payload?: unknown,
timeout = 200
): Promise<T | null> {
return new Promise((resolve) => {
Expand All @@ -42,6 +46,7 @@ export async function getPropertyFromWindow<T>(
source: sourceId,
type: messageType.sendType,
responseType: messageType.responseType,
payload: payload,
id: id,
} as InjectedScriptMessageSend,
"/"
Expand All @@ -61,3 +66,13 @@ export async function getVideoDescriptionFromWindow(): Promise<string | null> {
responseType: "returnDescription",
});
}

export async function getBvidFromAidFromWindow(aid: string): Promise<VideoID | null> {
return getPropertyFromWindow<VideoID>(
{
sendType: "convertAidToBvid",
responseType: "returnAidToBvid",
},
aid
);
}
24 changes: 3 additions & 21 deletions src/utils/parseVideoID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getBvIDfromAvIDBiliApi } from "../requests/bilibiliApi";
import { BILI_DOMAINS } from "./constants";
import { getPropertyFromWindow } from "./injectedScriptMessageUtils";
import { getBvidFromAidFromWindow, getPropertyFromWindow } from "./injectedScriptMessageUtils";
import { VideoID } from "./video";

export async function getBilibiliVideoID(url?: string): Promise<VideoID | null> {
Expand Down Expand Up @@ -32,7 +31,7 @@ const BILIBILI_VIDEO_URL_REGEX = /^\/video\/((BV1[a-zA-Z0-9]{9})|(av\d+))\/?/;
/**
* Parse without side effects
*/
export function getBvIDFromURL(url: string): VideoID | null {
export async function getBvIDFromURL(url: string): Promise<VideoID | null> {
//Attempt to parse url
let urlObject: URL | null = null;
try {
Expand All @@ -56,7 +55,7 @@ export function getBvIDFromURL(url: string): VideoID | null {
return idMatch[2] as VideoID;
} else if (idMatch && idMatch[3]) {
// av id
return getBvIDFromCache(idMatch[3], "-1" as VideoID);
return await getBvidFromAidFromWindow(idMatch[3]);
}
} else if (urlObject.host == "www.bilibili.com" && urlObject.pathname.startsWith("/list/")) {
const id = urlObject.searchParams.get("bvid");
Expand All @@ -66,23 +65,6 @@ export function getBvIDFromURL(url: string): VideoID | null {
return null;
}

const AvToBvMapCache = new Map<string, VideoID>();
const AvToBvLoading = new Set<string>();
function getBvIDFromCache(avID: string, placeholder: null | VideoID = null): VideoID | null {
const bvID = AvToBvMapCache.get(avID);
if (bvID) return bvID;

if (!AvToBvLoading.has(avID)) {
AvToBvLoading.add(avID);
getBvIDfromAvIDBiliApi(avID.replace("av", "")).then((bvID) => {
AvToBvMapCache.set(avID, bvID as VideoID);
AvToBvLoading.delete(avID);
});
}

return placeholder;
}

/**
* Validates and sanitizes a YouTube video ID.
*/
Expand Down

0 comments on commit 1484935

Please sign in to comment.