forked from hanydd/BilibiliSponsorBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.ts
More file actions
57 lines (50 loc) · 1.89 KB
/
document.ts
File metadata and controls
57 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { InjectedScriptMessageSend, sourceId } from "./utils/injectedScriptMessageUtils";
const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload): void => {
window.postMessage(
{
source: sourceId,
type: messageData.responseType,
id: messageData.id,
data: 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;
if (!data || !data?.source) {
return;
}
if (data?.source === sourceId && data?.responseType) {
if (data.type === "getBvID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.bvid);
} else if (data.type === "getFrameRate") {
sendMessageToContent(data, frameRate);
} else if (data.type === "getChannelID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.upData?.mid);
} else if (data.type === "getDescription") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.videoData?.desc);
}
}
}
function init(): void {
window.addEventListener("message", windowMessageListener);
}
init();