-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path哔哩哔哩弹幕姬.js
398 lines (392 loc) · 15.3 KB
/
哔哩哔哩弹幕姬.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// ==UserScript==
// @name bilibiliDanmaku
// @name:zh-CN 哔哩哔哩弹幕姬
// @namespace https://github.com/sakuyaa/gm_scripts
// @author sakuyaa
// @description 在哔哩哔哩视频标题下方增加弹幕查看和下载
// @include http*://www.bilibili.com/video/av*
// @include http*://www.bilibili.com/video/BV*
// @include http*://www.bilibili.com/watchlater/#/av*
// @include http*://www.bilibili.com/watchlater/#/BV*
// @include http*://www.bilibili.com/medialist/play/*/*
// @include http*://www.bilibili.com/bangumi/play/*
// @version 2020.11.1
// @compatible firefox 52
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
let view, download, downloadAll, downloadPast, subSpan, downloadSub, convertSub;
//拦截pushState和replaceState事件
let historyFunc = type => {
let origin = history[type];
return function () {
let e = new Event(type);
e.arguments = arguments;
window.dispatchEvent(e);
return origin.apply(history, arguments);
};
};
history.pushState = historyFunc('pushState');
history.replaceState = historyFunc('replaceState');
let sleep = time => {
return new Promise(resolve => setTimeout(resolve, time));
};
let fetchFunc = (url, type) => {
let init = {};
if (url.indexOf('.bilibili.com/') > 0) {
init.credentials = 'include';
}
return fetch(url, init).then(response => {
if (!response.ok) {
throw new Error(`bilibiliDanmaku:${response.status} ${response.statusText}\n无法加载:${url}`);
}
switch (type) {
case 'blob':
return response.blob();
case 'json':
return response.json();
default:
return response.text();
}
});
};
//获取视频发布日期
let fetchPubDate = async () => {
let response = await fetchFunc(`https://api.bilibili.com/x/web-interface/view?${window.bvid ? 'bvid=' + window.bvid : 'aid=' + window.aid}`, 'json');
if (response.data.pubdate) {
let pubDate = new Date(response.data.pubdate * 1000);
if (!isNaN(pubDate)) {
return pubDate;
}
}
return null;
};
//获取CC字幕列表
let fetchSubtitles = async () => {
let response = await fetchFunc(`https://api.bilibili.com/x/web-interface/view?${window.bvid ? 'bvid=' + window.bvid : 'aid=' + window.aid}`, 'json');
if (response.data.subtitle.list) {
return response.data.subtitle.list;
}
return [];
};
//秒转化为时分秒
let formatSeconds = seconds => {
let h = Math.floor(seconds / 3600);
if (h < 10) {
h = '0' + h;
}
let m = Math.floor((seconds / 60 % 60));
if (m < 10) {
m = '0' + m;
}
let s = Math.floor((seconds % 60));
if (s < 10) {
s = '0' + s;
}
let ms = '00' + Math.floor(seconds * 1000 % 1000);
return `${h}:${m}:${s}.${ms.substr(-3)}`;
}
let danmakuFunc = async () => {
//查看弹幕
view.setAttribute('href', `https://comment.bilibili.com/${window.cid}.xml`);
//下载弹幕
download.removeAttribute('download');
download.setAttribute('href', 'javascript:;');
download.onclick = async () => {
let danmaku = await fetchFunc(`https://api.bilibili.com/x/v1/dm/list.so?oid=${window.cid}&bilibiliDanmaku=1`, 'blob');
download.onclick = null;
download.setAttribute('download', document.title.split('_')[0] + '.xml');
download.setAttribute('href', URL.createObjectURL(danmaku));
download.dispatchEvent(new MouseEvent('click'));
};
//全弹幕下载
downloadAll.removeAttribute('download');
downloadAll.setAttribute('href', 'javascript:;');
downloadAll.onclick = async () => {
try {
//加载当前弹幕池
let danmakuMap = new Map();
let danmaku = await fetchFunc(`https://api.bilibili.com/x/v1/dm/list.so?oid=${window.cid}&bilibiliDanmaku=1`);
let danmakuAll = danmaku.substring(0, danmaku.indexOf('<d p='));
let exp = new RegExp('<d p="([^,]+)[^"]+,(\\d+)">.+?</d>', 'g');
while ((match = exp.exec(danmaku)) != null) {
danmakuMap.set(parseInt(match[2]), [parseFloat(match[1]), match[0]]);
}
//获取视频发布日期
let now = new Date();
let pubDate, year, month;
let dateNode = document.querySelector('.video-data span:nth-child(2)');
if (dateNode) {
pubDate = new Date(dateNode.textContent);
if (isNaN(pubDate)) {
pubDate = await fetchPubDate();
}
} else {
pubDate = await fetchPubDate();
}
if (!pubDate) {
alert('获取视频投稿时间失败!');
return;
}
year = pubDate.getFullYear();
month = pubDate.getMonth() + 1;
//计算历史月份
let monthArray = [];
while (year * 100 + month <= now.getFullYear() * 100 + now.getMonth() + 1) {
monthArray.push(`https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=${window.cid}&month=${year + '-' + ('0' + month).substr(-2)}`);
if (++month > 12) {
month = 1;
year++;
}
}
//增加延迟
let delay;
if ((delay = prompt('由于网站弹幕接口改版新的API限制获取速度,全弹幕下载需要有获取间隔,会导致该功能需要很长很长时间进行弹幕获取(视投稿时间而定,每天都有历史数据的话获取一个月大概需要20多秒),请输入获取间隔(若仍出现获取速度过快请适当加大间隔,单位:毫秒)', 299)) == null) {
return;
}
if (isNaN(delay)) {
alert('输入值不是数值!');
return;
}
//进度条
let progress = document.createElement('progress');
progress.setAttribute('max', monthArray.length * 1000);
progress.setAttribute('value', 0);
progress.style.position = 'fixed';
progress.style.margin = 'auto';
progress.style.left = progress.style.right = 0;
progress.style.top = progress.style.bottom = 0;
progress.style.zIndex = 99; //进度条置顶
document.body.appendChild(progress);
//获取历史弹幕日期
let data;
for (let i = 0; i < monthArray.length;) {
data = await fetchFunc(monthArray[i], 'json');
if (data.code) {
throw new Error('bilibiliDanmaku,API接口返回错误:' + data.message);
}
if (data.data) {
for (let j = 0; j < data.data.length; j++) {
progress.setAttribute('value', i * 1000 + 1000 / data.data.length * j);
await sleep(delay); //避免网站API调用速度过快导致错误
danmaku = await fetchFunc(`https://api.bilibili.com/x/v2/dm/history?type=1&oid=${window.cid}&date=${data.data[j]}&bilibiliDanmaku=1`);
if ((match = (new RegExp('^\{"code":[^,]+,"message":"([^"]+)","ttl":[^\}]+\}$',)).exec(danmaku)) != null) {
throw new Error('bilibiliDanmaku,API接口返回错误:' + match[1]);
}
exp = new RegExp('<d p="([^,]+)[^"]+,(\\d+)">.+?</d>', 'g');
while ((match = exp.exec(danmaku)) != null) {
if (!danmakuMap.has(parseInt(match[2]))) { //跳过重复的项目
danmakuMap.set(parseInt(match[2]), [parseFloat(match[1]), match[0]]);
}
}
}
}
progress.setAttribute('value', ++i * 1000);
}
//按弹幕播放时间排序
let danmakuArray = [];
for (let value of danmakuMap.values()) {
danmakuArray.push(value);
}
danmakuArray.sort((a, b) => a[0] - b[0]);
//合成弹幕
document.body.removeChild(progress);
for (let pair of danmakuArray) {
danmakuAll += pair[1];
}
danmakuAll += '</i>';
//设置下载链接
downloadAll.onclick = null;
downloadAll.setAttribute('download', document.title.split('_')[0] + '.xml');
downloadAll.setAttribute('href', URL.createObjectURL(new Blob([danmakuAll])));
downloadAll.dispatchEvent(new MouseEvent('click'));
} catch (e) {
alert(e);
}
};
//历史弹幕下载
downloadPast.onclick = async () => {
//获取视频发布日期
let date;
let dateNode = document.querySelector('.video-data span:nth-child(2)');
if (dateNode) {
date = new Date(dateNode.textContent);
if (isNaN(date)) {
date = await fetchPubDate();
}
} else {
date = await fetchPubDate();
}
if (!date) { //获取视频投稿时间失败,默认设置为当天
date = new Date();
}
if ((date = prompt('请按此格式输入想要下载历史弹幕的日期', date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).substr(-2) + '-' + ('0' + date.getDate()).substr(-2))) == null) {
return;
}
let danmaku = await fetchFunc(`https://api.bilibili.com/x/v2/dm/history?type=1&oid=${window.cid}&date=${date}&bilibiliDanmaku=1`);
let aLink = document.createElement('a');
aLink.setAttribute('download', document.title.split('_')[0] + '_' + date + '.xml');
aLink.setAttribute('href', URL.createObjectURL(new Blob([danmaku])));
aLink.dispatchEvent(new MouseEvent('click'));
};
//获取CC字幕列表
let subList = [];
let notFound = true;
if (window.eventLogText) {
for (let i = window.eventLogText.length - 1; i >= 0; i--) {
let eventLog = window.eventLogText[i];
if (eventLog.indexOf('<subtitle>') > 0) {
notFound = false;
try {
subList = JSON.parse(eventLog.substring(eventLog.indexOf('<subtitle>') + 10,
eventLog.indexOf('</subtitle>'))).subtitles;
} catch (e) {
console.log(e);
notFound = true;
}
break;
}
}
}
if (notFound) {
subList = await fetchSubtitles();
}
if (subList.length == 0) { //没有CC字幕则隐藏相关按钮
subSpan.setAttribute('hidden', 'hidden');
downloadSub.onclick = null;
convertSub.onclick = null;
return;
} else {
subSpan.removeAttribute('hidden');
}
//下载CC字幕
downloadSub.onclick = async () => {
let aLink = document.createElement('a');
for (let sub of subList) {
let subtitle = await fetchFunc(sub.subtitle_url.replace(/^http:/, ''), 'blob'); //避免混合内容
aLink.setAttribute('download', sub.lan + '_' + document.title.split('_')[0] + '.json');
aLink.setAttribute('href', URL.createObjectURL(subtitle));
aLink.dispatchEvent(new MouseEvent('click'));
}
};
//生成SRT字幕
convertSub.onclick = async () => {
let aLink = document.createElement('a');
for (let sub of subList) {
let subtitle = await fetchFunc(sub.subtitle_url.replace(/^http:/, ''), 'json'); //避免混合内容
let srt = '', index = 0;
for (let content of subtitle.body) {
srt += `${index++}\n${formatSeconds(content.from)} --> ${formatSeconds(content.to)}\n${content.content.replace(/\n/g, '<br>')}\n\n`;
}
aLink.setAttribute('download', sub.lan + '_' + document.title.split('_')[0] + '.srt');
aLink.setAttribute('href', URL.createObjectURL(new Blob([srt])));
aLink.dispatchEvent(new MouseEvent('click'));
}
};
};
let findInsertPos = () => {
let node;
if (location.href.indexOf('www.bilibili.com/bangumi/play') > 0) { //番剧
node = document.querySelector('.media-right');
if (node && node.querySelector('.media-count').textContent.indexOf('弹幕') == -1) {
return null; //避免信息栏未加载出来时插入链接导致错误
}
} else if (location.href.indexOf('www.bilibili.com/watchlater') > 0) { //稍后再看
node = document.querySelector('.tminfo');
if (node) {
node.lastElementChild.style.marginRight = '32px';
}
} else if (location.href.indexOf('www.bilibili.com/medialist/play') > 0) { //新的稍后再看页面、收藏页面
node = document.querySelector('.play-data');
if (node) {
node.lastElementChild.style.marginRight = '16px';
}
//新的稍后再看页面没有aid、bvid、cid,需要特殊处理
let videoMessage = window.player.getVideoMessage();
if (videoMessage) {
window.aid = videoMessage.aid;
window.cid = videoMessage.cid;
} else {
return null;
}
} else {
node = document.getElementById('viewbox_report');
if (node) {
if (!document.querySelector('.bilibili-player-video-info-people-number')) {
return null; //避免信息栏未加载出来时插入链接导致错误
}
node = node.querySelector('.video-data');
node.lastElementChild.style.marginRight = '16px';
}
}
return node;
};
let createNode = () => {
view = document.createElement('a');
download = document.createElement('a');
downloadAll = document.createElement('a');
downloadPast = document.createElement('a');
downloadSub = document.createElement('a');
convertSub = document.createElement('a');
view.setAttribute('target', '_blank');
downloadPast.setAttribute('href', 'javascript:;');
downloadSub.setAttribute('href', 'javascript:;');
convertSub.setAttribute('href', 'javascript:;');
view.textContent = '查看弹幕';
download.textContent = '下载弹幕';
downloadAll.textContent = '全弹幕下载';
downloadPast.textContent = '历史弹幕下载';
downloadSub.textContent = '下载CC字幕';
convertSub.textContent = '生成SRT字幕';
view.style.color = '#999';
download.style.color = '#999';
downloadAll.style.color = '#999';
downloadPast.style.color = '#999';
downloadSub.style.color = '#999';
convertSub.style.color = '#999';
let span = document.createElement('span');
span.id = 'bilibiliDanmaku';
span.appendChild(view);
span.appendChild(document.createTextNode(' | '));
span.appendChild(download);
span.appendChild(document.createTextNode(' | '));
span.appendChild(downloadAll);
span.appendChild(document.createTextNode(' | '));
span.appendChild(downloadPast);
subSpan = document.createElement('span');
subSpan.setAttribute('hidden', 'hidden');
subSpan.style.marginLeft = '16px'; //弹幕与字幕功能分开
subSpan.appendChild(downloadSub);
subSpan.appendChild(document.createTextNode(' | '));
subSpan.appendChild(convertSub);
span.appendChild(subSpan);
return span;
};
let insertNode = () => {
let code = setInterval(() => {
if (location.href.indexOf('www.bilibili.com/medialist/play') > 0) {
if (!window.player) { //新的稍后再看页面、收藏页面没有cid
return;
}
} else if (!window.cid) {
return;
}
if (document.getElementById('bilibiliDanmaku')) { //节点已存在
clearInterval(code);
danmakuFunc();
} else {
let node = findInsertPos();
if (node) {
clearInterval(code);
node.appendChild(createNode());
danmakuFunc();
}
}
}, 2196);
};
insertNode();
addEventListener('hashchange', insertNode);
addEventListener('pushState', insertNode);
addEventListener('replaceState', insertNode);
})();