Skip to content

Commit

Permalink
supplement play info with request cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 5, 2024
1 parent 6de7d3a commit 5f3209e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getFrameRate, PlayInfo } from "./document/frameRateUtils";
import { DataCache } from "./utils/cache";
import { InjectedScriptMessageSend, sourceId } from "./utils/injectedScriptMessageUtils";

const cache = new DataCache<string, PlayInfo[]>(() => []);
const playInfoCache = new DataCache<string, PlayInfo[]>(() => []);

const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload): void => {
window.postMessage(
Expand All @@ -29,8 +29,13 @@ function overwriteFetch() {
const url = new URL(urlStr);
if (url.pathname.includes("/player/wbi/playurl")) {
const cid = url.searchParams.get("cid");
if (!cache.getFromCache(cid) && res?.data?.dash?.video) {
cache.setupCache(cid).push(...res.data.dash.video);
if (!playInfoCache.getFromCache(cid) && res?.data?.dash?.video) {
playInfoCache.setupCache(cid).push(
...res.data.dash.video.map((v) => ({
id: v.id,
frameRate: parseFloat(v.frameRate),
}))
);
}
}
})
Expand All @@ -48,8 +53,7 @@ function windowMessageListener(message: MessageEvent) {
if (data.type === "getBvID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.bvid);
} else if (data.type === "getFrameRate") {
console.log("Frame rate", getFrameRate());
sendMessageToContent(data, getFrameRate());
sendMessageToContent(data, getFrameRate(playInfoCache));
} else if (data.type === "getChannelID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.upData?.mid);
} else if (data.type === "getDescription") {
Expand Down
10 changes: 7 additions & 3 deletions src/document/frameRateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DataCache } from "../utils/cache";

export interface PlayInfo {
id: number;
frameRate: number;
}

export function getPlayInfo(): PlayInfo[] {
export function getPlayInfo(playInfoCache: DataCache<string, PlayInfo[]>): PlayInfo[] {
if (window.__playinfo__?.data?.dash?.video) {
return window.__playinfo__.data.dash.video.map((v) => ({ id: v.id, frameRate: parseFloat(v.frameRate) }));
} else if (playInfoCache.getFromCache(window?.__INITIAL_STATE__?.cid.toString())) {
return playInfoCache.getFromCache(window.__INITIAL_STATE__.cid.toString());
}
return [];
}

export function getFrameRate() {
export function getFrameRate(playInfoCache: DataCache<string, PlayInfo[]>) {
let currentQuality = null;
try {
currentQuality ||= JSON.parse(window?.localStorage?.bpx_player_profile)?.media?.quality;
Expand All @@ -19,7 +23,7 @@ export function getFrameRate() {
}
currentQuality = currentQuality ?? window?.__playinfo__?.data?.quality;

const possibleQuality = getPlayInfo().filter((v) => v.id === currentQuality && !!v.frameRate);
const possibleQuality = getPlayInfo(playInfoCache).filter((v) => v.id === currentQuality && !!v.frameRate);

if (possibleQuality.length > 0) {
return possibleQuality[0].frameRate;
Expand Down

0 comments on commit 5f3209e

Please sign in to comment.