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 info menu invisible in Widescreen mode #107

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Changes from 1 commit
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
Next Next commit
get aid map from overwrite
hanydd committed Dec 9, 2024
commit 98ae842796fc0f9e193508e5916b3dc4ee7db771
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 => {
@@ -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);
}
}

@@ -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));
}
}
}
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;
@@ -8,7 +8,7 @@ declare global {
aid: number;
cid: number;
upData: { mid: string };
videoData: { desc: string };
videoData: BiliVideoDetail;
};
__playinfo__?: BilibiliResponse<BiliPlayInfo>;
}
42 changes: 41 additions & 1 deletion src/requests/type/BilibiliRequestType.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 4 additions & 5 deletions src/thumbnail-utils/thumbnails.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
}
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) => {
@@ -42,6 +46,7 @@ export async function getPropertyFromWindow<T>(
source: sourceId,
type: messageType.sendType,
responseType: messageType.responseType,
payload: payload,
id: id,
} as InjectedScriptMessageSend,
"/"
@@ -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
);
}
38 changes: 19 additions & 19 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> {
@@ -32,7 +31,7 @@
/**
* 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 {
@@ -56,7 +55,7 @@
return idMatch[2] as VideoID;
} else if (idMatch && idMatch[3]) {
// av id
return getBvIDFromCache(idMatch[3], "-1" as VideoID);
return await getBvIDFromCache(idMatch[3], "-1" as VideoID);
}
} else if (urlObject.host == "www.bilibili.com" && urlObject.pathname.startsWith("/list/")) {
const id = urlObject.searchParams.get("bvid");
@@ -66,21 +65,22 @@
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;
// const AvToBvMapCache = new Map<string, VideoID>();
// const AvToBvLoading = new Set<string>();
async function getBvIDFromCache(avID: string, placeholder: null | VideoID = null): Promise<VideoID> {

Check failure on line 70 in src/utils/parseVideoID.ts

GitHub Actions / Create artifacts

'placeholder' is assigned a value but never used
// 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 await getBvidFromAidFromWindow(avID);

// return placeholder;
}

/**