-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilibili-playlist-pb.js
More file actions
201 lines (162 loc) · 5.83 KB
/
bilibili-playlist-pb.js
File metadata and controls
201 lines (162 loc) · 5.83 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
/**
* 处理一般播放列表的 总播放进度
*
* 不同 bvid 简单列表
* https://www.bilibili.com/video/BV1W7411F79S/
*
* 同 bvid 简单列表
* https://www.bilibili.com/video/BV1Q44y157Xh?p=6
*
* 多层列表 单 bvid
* https://www.bilibili.com/video/BV12rYkzcEQ4
*
* 多层列表 单 bvid 多 p
* https://www.bilibili.com/video/BV1EP41167eq/
* https://www.bilibili.com/video/BV1EP41167eq?p=4
*
* 自制list 单 bvid 多 p
* https://www.bilibili.com/list/ml62693834?oid=973735712&bvid=BV1944y1B7MJ&p=3
*
* 全部播放list 单 bvid 多 p
* https://www.bilibili.com/list/7980111?from=space&business=space&sort_field=pubtime&bvid=BV18kakziE3h&p=2
*
*/
// * -------------------------------- build playlist time
/** @type {Map<string, any>} */
const fetchCache = new Map();
/**
* @typedef {Object} PlaylistTime
* @property {[number,number]} all
* @property {[number,number]} [sub]
*/
const buildListTime = async (url = location.href) => {
// * ---------------- params
const u = new URL(url);
const bvid = u.searchParams.get("bvid") ?? u.href.match(/\bBV[^/?]+\b/)?.[0];
if (!bvid) return;
const data =
fetchCache.get(bvid) ??
(await fetch(`https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`)
.then((e) => e.json())
.then((e) => {
fetchCache.set(bvid);
return e;
}));
const p = Number(u.searchParams.get("p") ?? 1);
// * ---------------- build
if (data.data.ugc_season) {
const eps = data.data.ugc_season?.sections.flatMap((e) => e.episodes);
const epIndex = eps?.findIndex((e) => e.bvid === bvid);
const pages = eps?.find((e) => e.bvid === bvid).pages;
const pageIndex = p - 1;
const totalTimeAll = eps
.flatMap((e) => e.pages)
.map((e) => e.duration)
.reduce((a, e) => a + e, 0);
const beforeTimeAll = [...(epIndex > 0 ? eps.slice(0, epIndex).flatMap((e) => e.pages) : []), ...(pageIndex > 0 ? pages.slice(0, pageIndex) : [])].map((e) => e.duration).reduce((a, e) => a + e, 0);
const TotalTimeSub = pages.map((e) => e.duration).reduce((a, e) => a + e, 0);
const beforeTimeSub = (pageIndex > 0 ? pages.slice(0, pageIndex) : []).map((e) => e.duration).reduce((a, e) => a + e, 0);
/** @type {PlaylistTime} */
const result = {
all: [beforeTimeAll, totalTimeAll],
sub: [beforeTimeSub, TotalTimeSub],
};
const isSinglePage = pages.length <= 1;
if (isSinglePage) delete result.sub;
return result;
} else {
const pages = data.data.pages;
const isSinglePage = pages.length <= 1;
if (isSinglePage) return null;
const pageIndex = p - 1;
const totalTimeAll = pages.map((e) => e.duration).reduce((a, e) => a + e, 0);
const beforeTimeAll = (pageIndex > 0 ? pages.slice(0, pageIndex) : []).map((e) => e.duration).reduce((a, e) => a + e, 0);
/** @type {PlaylistTime} */
const result = {
all: [beforeTimeAll, totalTimeAll],
};
return result;
}
};
// * -------------------------------- build bind
/** @type { PlaylistTime | null } */
let time = null;
buildListTime().then((result) => (time = result));
// @ts-ignore
window.navigation.addEventListener("navigate", (event) => {
buildListTime(event.destination.url).then((result) => (time = result));
});
// * -------------------------------- progress bar
const pb = window.makeProgressBar();
const pb2 = window.makeProgressBar();
document.addEventListener(
"loadstart",
async (e) => {
/** @type {HTMLMediaElement} */
// @ts-ignore
const media = window.player?.mediaElement();
if (e.target !== media) return;
media.addEventListener("timeupdate", () => {
if (!time) return;
/** 注:仅合集类型的视频,有带视频列表 */
/** @type {HTMLElement} */
const playlistEl = document.querySelector("#mirror-vdcon .video-pod, #mirror-vdcon .action-list-container");
if (!playlistEl) return;
const currentTime = media.currentTime;
// * ---------------- main pb
/** attach pb element */
if (!playlistEl.contains(pb.pbEl)) {
playlistEl.style.position = "relative";
playlistEl.style.overflow = "initial";
Object.assign(pb.pbEl.style, {
position: "absolute",
top: "0",
right: "0",
width: "100%",
});
Object.assign(pb.textEl.style, {
left: "0",
top: "0",
transform: "translateY(-100%)",
fontSize: "12px",
});
// ! setTimeout for 等待B站dom检测功能执行完
setTimeout(() => {
playlistEl.appendChild(pb.pbEl);
}, 1500);
}
pb.updateProgressBar(time.all[0] + currentTime, time.all[1]);
// * ---------------- sub pb
if (time.sub) {
pb2.pbEl.style.display = "initial";
if (!playlistEl.contains(pb2.pbEl)) {
Object.assign(pb2.pbEl.style, {
position: "absolute",
top: "0",
right: "0",
width: "100%",
});
Object.assign(pb2.barEl.style, {
top: "2px",
});
Object.assign(pb2.textEl.style, {
right: "0",
top: "0",
transform: "translateY(-100%)",
fontSize: "12px",
});
// ! setTimeout for 等待B站dom检测功能执行完
setTimeout(() => {
playlistEl.appendChild(pb2.pbEl);
}, 1500);
}
pb2.updateProgressBar(time.sub[0] + currentTime, time.sub[1]);
} else {
pb2.pbEl.style.display = "none";
}
});
},
true,
);
}