-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainworld.js
More file actions
37 lines (37 loc) · 1.43 KB
/
Copy pathmainworld.js
File metadata and controls
37 lines (37 loc) · 1.43 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
// mainworld.js — 运行在页面主世界(manifest 声明 world: "MAIN",不受页面 CSP 限制)
// 作用:按需读取 window.__INITIAL_STATE__.videoData(B站 单页切换视频时会更新此对象),
// 把"当前真正在播的视频"的 bvid/aid/cid 回传给 isolated world 的 content.js。
// 解决:自动连播/推荐换片后地址栏 BV 号过期,导致拿错视频字幕的问题。
(function () {
window.addEventListener('message', function (e) {
if (e.source !== window || !e.data || e.data.__bili_dl_req !== 'cur_video') return;
var out = {};
try {
var s = window.__INITIAL_STATE__;
if (s) {
var vd = s.videoData;
if (vd) {
out.bvid = vd.bvid;
out.aid = vd.aid;
out.cid = vd.cid;
out.title = vd.title;
out.pic = vd.pic;
if (vd.pages && vd.pages.length) {
out.pages = vd.pages.map(function (p) { return { cid: p.cid, part: p.part }; });
}
}
if (s.epInfo) {
out.epAid = s.epInfo.aid;
out.epCid = s.epInfo.cid;
out.epId = s.epInfo.ep_id;
out.epBvid = s.epInfo.bvid;
}
if (s.bvid && !out.bvid) out.bvid = s.bvid;
if (s.aid && !out.aid) out.aid = s.aid;
}
} catch (err) {
out.error = String(err);
}
window.postMessage({ __bili_dl_resp: 'cur_video', reqId: e.data.reqId, payload: out }, '*');
});
})();