-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbilibili-live-auto-chat.js
363 lines (316 loc) · 13.1 KB
/
bilibili-live-auto-chat.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
// ==UserScript==
// @name bilibili 直播间独轮车 LAPLACE ver.
// @namespace https://greasyfork.org/users/9967
// @version 1.2.6
// @description 这是 bilibili 直播间简易版独轮车,基于 quiet/thusiant cmd 版本 https://greasyfork.org/scripts/421507 继续维护而来
// @author sparanoid
// @license AGPL
// @match *://live.bilibili.com/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// ==/UserScript==
let MsgTemplates = GM_getValue('MsgTemplates', []);
let activeTemplateIndex = GM_getValue('activeTemplateIndex', 0);
const scriptInitVal = { msgSendInterval: 1, maxLength: 20, maxLogLines: 1000, randomColor: false, randomInterval: false };
for (let initVal in scriptInitVal) {
if (GM_getValue(initVal) === undefined) GM_setValue(initVal, scriptInitVal[initVal]);
}
let sendMsg = false;
function getGraphemes(str) {
const segmenter = new Intl.Segmenter('zh', { granularity: 'grapheme' });
return Array.from(segmenter.segment(str), ({ segment }) => segment);
}
function trimText(text, maxLength) {
if (!text) return [text];
const graphemes = getGraphemes(text);
if (graphemes.length <= maxLength) return [text];
const parts = [];
let currentPart = [];
let currentLength = 0;
for (const char of graphemes) {
if (currentLength >= maxLength) {
parts.push(currentPart.join(''));
currentPart = [char];
currentLength = 1;
} else {
currentPart.push(char);
currentLength++;
}
}
if (currentPart.length > 0) {
parts.push(currentPart.join(''));
}
return parts;
}
function appendToLimitedLog(logElement, message, maxLines) {
const lines = logElement.value.split('\n');
if (lines.length >= maxLines) {
// Keep only the last (maxLines - 1) lines and add the new message
lines.splice(0, lines.length - maxLines + 1);
}
lines.push(message);
logElement.value = lines.join('\n');
logElement.scrollTop = logElement.scrollHeight;
}
function extractRoomNumber(url) {
const urlObj = new URL(url);
const pathSegments = urlObj.pathname.split('/').filter(segment => segment !== '');
const roomNumber = pathSegments.find(segment => Number.isInteger(Number(segment)));
return roomNumber;
}
function processMessages(text, maxLength) {
return text
.split('\n')
.map(line => trimText(line, maxLength))
.flat()
.filter(line => line && line.trim());
}
(function () {
const check = setInterval(() => {
const toggleBtn = document.createElement('div');
toggleBtn.id = 'toggleBtn';
toggleBtn.textContent = '独轮车面版';
toggleBtn.style.cssText = `
position: fixed;
right: 14px;
bottom: 14px;
z-index: 2147483647;
cursor: pointer;
background: #777;
color: white;
padding: 6px 8px;
border-radius: 4px;
user-select: none;
`;
document.body.appendChild(toggleBtn);
const list = document.createElement('div');
list.style.cssText = `
position: fixed;
right: 14px;
bottom: calc(14px + 30px);
z-index: 2147483647;
background: white;
display: none;
padding: 14px;
box-shadow: 0 0 0 1px rgba(0, 0, 0, .2);
border-radius: 4px;
min-width: 50px;
width: 300px;
`;
list.innerHTML = `<div>
<div style="font-weight: bold;">独轮车 LAPLACE ver.</div>
<div style="margin: .5em 0; display: flex; align-items: center; flex-wrap: wrap; gap: .25em;">
<button id="sendBtn">开启独轮车</button>
<select id="templateSelect"></select>
<button id="addTemplateBtn">新增</button>
<button id="removeTemplateBtn">删除当前</button>
</div>
<textarea id="msgList" placeholder="在这输入弹幕,每行一句话,超过可发送字数的会自动进行分割" style="height: 100px; width: 100%; resize: none;"></textarea>
<div style="margin: .5em 0;">
<span id="msgCount"></span><span>间隔</span>
<input id="msgSendInterval" style="width: 30px;" autocomplete="off" type="number" min="0" value="${GM_getValue('msgSendInterval')}" />
<span>秒,</span>
<span>超过</span>
<input id="maxLength" style="width: 30px;" autocomplete="off" type="number" min="1" value="${GM_getValue('maxLength')}" />
<span>字自动分段,</span>
<span style="display: inline-flex; align-items: center; gap: .25em;">
<input id="randomColor" type="checkbox" ${GM_getValue('randomColor') ? 'checked' : ''} />
<label for="randomColor">随机颜色</label>
</span>
<span style="display: inline-flex; align-items: center; gap: .25em;">
<input id="randomInterval" type="checkbox" ${GM_getValue('randomInterval') ? 'checked' : ''} />
<label for="randomInterval">间隔增加随机性</label>
</span>
</div>
<textarea id="msgLogs" style="height: 80px; width: 100%; resize: none;" placeholder="此处将输出日志(最多保留 ${GM_getValue('maxLogLines')} 条)" readonly></textarea>
</div>`;
document.body.appendChild(list);
const sendBtn = document.getElementById('sendBtn');
const msgLogs = document.getElementById('msgLogs');
const maxLogLines = GM_getValue('maxLogLines');
sendBtn.addEventListener('click', () => {
if (!sendMsg) {
const currentTemplate = MsgTemplates[activeTemplateIndex] || '';
if (!currentTemplate.trim()) {
appendToLimitedLog(msgLogs, '⚠️ 当前模板为空,请先输入内容', maxLogLines);
return;
}
updateMessages();
sendMsg = true;
sendBtn.textContent = '关闭独轮车';
toggleBtn.style.background = 'rgb(0 186 143)';
} else {
sendMsg = false;
sendBtn.textContent = '开启独轮车';
toggleBtn.style.background = 'rgb(166 166 166)';
}
});
toggleBtn.addEventListener('click', () => {
list.style.display = list.style.display === 'none' ? 'block' : 'none';
});
const msgInput = document.getElementById('msgList');
const msgCount = document.getElementById('msgCount');
const msgIntervalInput = document.getElementById('msgSendInterval');
const maxLengthInput = document.getElementById('maxLength');
const randomColorInput = document.getElementById('randomColor');
const randomIntervalInput = document.getElementById('randomInterval');
const templateSelect = document.getElementById('templateSelect');
const addTemplateBtn = document.getElementById('addTemplateBtn');
const removeTemplateBtn = document.getElementById('removeTemplateBtn');
function updateMessages() {
const maxLength = parseInt(maxLengthInput.value) || 20;
MsgTemplates[activeTemplateIndex] = msgInput.value;
GM_setValue('MsgTemplates', MsgTemplates);
const Msg = processMessages(msgInput.value, maxLength);
msgCount.textContent = `${Msg.length || 0} 条,`;
}
function updateTemplateSelect() {
templateSelect.innerHTML = '';
MsgTemplates.forEach((template, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = `模板 ${index + 1}`;
templateSelect.appendChild(option);
});
templateSelect.value = activeTemplateIndex;
msgInput.value = MsgTemplates[activeTemplateIndex] || '';
updateMessages();
}
templateSelect.addEventListener('change', () => {
activeTemplateIndex = parseInt(templateSelect.value);
GM_setValue('activeTemplateIndex', activeTemplateIndex);
msgInput.value = MsgTemplates[activeTemplateIndex] || '';
updateMessages();
});
addTemplateBtn.addEventListener('click', () => {
MsgTemplates.push('');
activeTemplateIndex = MsgTemplates.length - 1;
GM_setValue('MsgTemplates', MsgTemplates);
GM_setValue('activeTemplateIndex', activeTemplateIndex);
updateTemplateSelect();
});
removeTemplateBtn.addEventListener('click', () => {
if (MsgTemplates.length > 1) {
MsgTemplates.splice(activeTemplateIndex, 1);
activeTemplateIndex = Math.max(0, activeTemplateIndex - 1);
GM_setValue('MsgTemplates', MsgTemplates);
GM_setValue('activeTemplateIndex', activeTemplateIndex);
updateTemplateSelect();
}
});
msgInput.addEventListener('input', () => {
updateMessages();
});
msgIntervalInput.addEventListener('input', () => {
if (!(parseInt(msgIntervalInput.value) >= 0)) msgIntervalInput.value = 0;
GM_setValue('msgSendInterval', msgIntervalInput.value);
});
randomColorInput.addEventListener('input', () => {
GM_setValue('randomColor', randomColorInput.checked);
});
randomIntervalInput.addEventListener('input', () => {
GM_setValue('randomInterval', randomIntervalInput.checked);
});
maxLengthInput.addEventListener('input', () => {
const value = parseInt(maxLengthInput.value);
if (value < 1) maxLengthInput.value = 1;
GM_setValue('maxLength', maxLengthInput.value);
updateMessages();
});
updateTemplateSelect();
loop();
clearInterval(check);
}, 100);
})();
async function loop() {
let count = 0;
const msgLogs = document.getElementById('msgLogs');
const maxLogLines = GM_getValue('maxLogLines');
const shortUid = extractRoomNumber(window.location.href);
const room = await fetch(`https://api.live.bilibili.com/room/v1/Room/room_init?id=${shortUid}`, {
method: 'GET',
credentials: 'include'
});
const roomData = await room.json();
const roomId = roomData.data.room_id;
const csrfToken = document.cookie
.split(';')
.map(c => c.trim())
.find(c => c.startsWith('bili_jct='))
?.split('bili_jct=')[1];
while (true) {
if (sendMsg) {
const currentTemplate = MsgTemplates[activeTemplateIndex] || '';
if (!currentTemplate.trim()) {
appendToLimitedLog(msgLogs, '⚠️ 当前模板为空,已自动停止运行', maxLogLines);
sendMsg = false;
const sendBtn = document.getElementById('sendBtn');
const toggleBtn = document.getElementById('toggleBtn');
sendBtn.textContent = '开启独轮车';
toggleBtn.style.background = 'rgb(166 166 166)';
continue;
}
const msgSendInterval = GM_getValue('msgSendInterval');
const enableRandomColor = GM_getValue('randomColor');
const enableRandomInterval = GM_getValue('randomInterval');
const Msg = processMessages(currentTemplate, GM_getValue('maxLength'));
for (const message of Msg) {
if (sendMsg) {
try {
if (enableRandomColor) {
const colorSet = ['0xe33fff', '0x54eed8', '0x58c1de', '0x455ff6', '0x975ef9', '0xc35986', '0xff8c21', '0x00fffc', '0x7eff00', '0xffed4f', '0xff9800']
const randomColor = colorSet[Math.floor(Math.random() * colorSet.length)];
const configForm = new FormData();
configForm.append('room_id', String(roomId));
configForm.append('color', randomColor);
configForm.append('csrf_token', csrfToken);
configForm.append('csrf', csrfToken);
configForm.append('visit_id', '');
const updateConfig = await fetch('https://api.live.bilibili.com/xlive/web-room/v1/dM/AjaxSetConfig', {
method: 'POST',
credentials: 'include',
body: configForm
});
}
const form = new FormData();
form.append('bubble', '2');
form.append('msg', message);
form.append('color', '16777215');
form.append('mode', '1');
form.append('room_type', '0');
form.append('jumpfrom', '0');
form.append('reply_mid', '0');
form.append('reply_attr', '0');
form.append('replay_dmid', '');
form.append('statistics', '{"appId":100,"platform":5}');
form.append('fontsize', '25');
form.append('rnd', String(Math.floor(Date.now() / 1000)));
form.append('roomid', String(roomId));
form.append('csrf', csrfToken);
form.append('csrf_token', csrfToken);
const send = await fetch('https://api.live.bilibili.com/msg/send', {
method: 'POST',
credentials: 'include',
body: form
});
const sendApiRes = await send.json();
const logMessage = sendApiRes.message
? `❌「${message}」,原因:${sendApiRes.message}。`
: `✅「${message}」`;
appendToLimitedLog(msgLogs, logMessage, maxLogLines);
const resolvedRandomInterval = enableRandomInterval ? Math.floor(Math.random() * 500) : 0
await new Promise(r => setTimeout(r, msgSendInterval * 1000 - resolvedRandomInterval));
} catch (error) {
appendToLimitedLog(msgLogs, `🔴「${message}」失败,错误:${error.message}`, maxLogLines);
}
}
}
count += 1;
appendToLimitedLog(msgLogs, `🔵第 ${count} 轮发送完成`, maxLogLines);
} else {
count = 0;
await new Promise(r => setTimeout(r, 1000));
}
}
}