Skip to content

Commit

Permalink
update fetch overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 5, 2024
1 parent bfe7994 commit c183287
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
32 changes: 18 additions & 14 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getFrameRate } from "./document/frameRateUtils";
import { getFrameRate, PlayInfo } from "./document/frameRateUtils";
import { DataCache } from "./utils/cache";
import { InjectedScriptMessageSend, sourceId } from "./utils/injectedScriptMessageUtils";

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

const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload): void => {
window.postMessage(
{
Expand All @@ -13,24 +16,25 @@ const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload):
);
};

let frameRate: number;
function overwriteFetch() {
const originalFetch = window.fetch;

window.fetch = async function (input, init) {
const url = typeof input === "string" ? input : (input as Request).url;
const urlStr = 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;
});
}
response
.clone()
.json()
.then((res) => {
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);
}
}
})
.catch(() => {});
return response;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/document/frameRateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface PlayInfo {
export interface PlayInfo {
id: number;
frameRate: number;
}
Expand Down

0 comments on commit c183287

Please sign in to comment.