Skip to content

Commit

Permalink
Merge pull request #97 from etherfun/dev_0
Browse files Browse the repository at this point in the history
Update windowMessageListener
  • Loading branch information
hanydd authored Dec 1, 2024
2 parents eca1405 + 9d1c141 commit 5477069
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload):
"/"
);
};
let frameRate: number = 30;

(function () {
const originalFetch = window.fetch;

window.fetch = async function (input, init) {
const url = typeof input === 'string' ? input : (input as Request).url;

const response = await originalFetch(input, init);
if (url.includes('/player/wbi/playurl') && url.includes(window.__INITIAL_STATE__.cid.toString())) {
response.clone().json().then(data => {
frameRate = data.data.dash.video
.filter((v) => v.id === data.data.quality && v.codecid === data.data.video_codecid)[0]?.frameRate;
}).catch(() => {
frameRate = 30;
});
}
return response;
};
})();

function windowMessageListener(message: MessageEvent) {
const data: InjectedScriptMessageSend = message.data;
Expand All @@ -21,12 +41,9 @@ function windowMessageListener(message: MessageEvent) {
if (data.type === "getBvID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.bvid);
} else if (data.type === "getFrameRate") {
const currentQuality = window?.__playinfo__?.data?.quality;
const frameRate = window?.__playinfo__?.data?.dash?.video.filter((v) => v.id === currentQuality)[0]
?.frameRate;
sendMessageToContent(data, frameRate);
} else if (data.type === "getChannelID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.upInfo?.mid);
sendMessageToContent(data, window?.__INITIAL_STATE__?.upData?.mid);
} else if (data.type === "getDescription") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.videoData?.desc);
}
Expand Down
3 changes: 1 addition & 2 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import SBObject from "./config";
declare global {
interface Window {
SB: typeof SBObject;
__INITIAL_STATE__?: { bvid: string; upInfo: { mid: number }; videoData: { desc: string } };
__playinfo__?: { data: { quality: number; dash: { video: { id: number; frameRate: number }[] } } };
__INITIAL_STATE__?: { bvid: string; aid: number; cid: number; upData: { mid: string }; videoData: { desc: string }};
}
}

0 comments on commit 5477069

Please sign in to comment.