-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBilibiliStreamKeyHelper.user.js
More file actions
2019 lines (1827 loc) · 68.4 KB
/
BilibiliStreamKeyHelper.user.js
File metadata and controls
2019 lines (1827 loc) · 68.4 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name B站推流码获取工具
// @namespace https://github.com/smathsp
// @version 1.14
// @description 获取第三方推流码
// @author smathsp
// @license GPL-3.0
// @match *://*.bilibili.com/*
// @icon https://www.bilibili.com/favicon.ico
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_notification
// @connect api.live.bilibili.com
// @connect passport.bilibili.com
// @require https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js
// @run-at document-end
// ==/UserScript==
(function () {
"use strict";
// 存储键名常量
const STORAGE_KEYS = {
LAST_ROOM_ID: "bili_last_roomid",
DARK_MODE: "bili_dark_mode",
IS_LIVE_STARTED: "isLiveStarted",
STREAM_INFO: "streamInfo",
LAST_GROUP_ID: "bili_last_groupid",
LAST_AREA_ID: "bili_last_areaid",
AREA_LIST_TIME: "bili_area_list_time",
AREA_LIST: "bili_area_list",
USER_MID: "bili_user_mid",
LAST_TITLE: "bili_last_title",
};
// API URL Constants
const API_URL_AREA_LIST =
"https://api.live.bilibili.com/room/v1/Area/getList?show_pinyin=1";
const API_URL_START_LIVE =
"https://api.live.bilibili.com/room/v1/Room/startLive";
const API_URL_UPDATE_ROOM =
"https://api.live.bilibili.com/room/v1/Room/update";
const API_URL_STOP_LIVE =
"https://api.live.bilibili.com/room/v1/Room/stopLive";
const API_URL_FACE_AUTH =
"https://api.live.bilibili.com/xlive/app-blink/v1/preLive/IsUserIdentifiedByFaceAuth";
// 将 GM_xmlhttpRequest Promise 化
function gmRequest(options) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
...options,
onload: resolve,
onerror: reject,
ontimeout: reject,
onabort: reject,
});
});
}
// SVG 图标常量
const SUN_SVG =
'<svg viewBox="0 0 24 24" width="20" height="20"><circle cx="12" cy="12" r="5" fill="#FFD600"/><g stroke="#FFD600" stroke-width="2"><line x1="12" y1="1" x2="12" y2="4"/><line x1="12" y1="20" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="6.34" y2="6.34"/><line x1="17.66" y1="17.66" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="4" y2="12"/><line x1="20" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="6.34" y2="17.66"/><line x1="17.66" y1="6.34" x2="19.78" y2="4.22"/></g></svg>';
const MOON_SVG =
'<svg viewBox="0 0 24 24" width="20" height="20"><path d="M21 12.79A9 9 0 0 1 12.21 3c-.55 0-.66.71-.19.93A7 7 0 1 0 20.07 12c.22.47-.38.74-.87.79z" fill="#888"/></svg>';
const CLOSE_SVG =
'<svg viewBox="0 0 1024 1024" width="16" height="16"><path d="M512 421.49 331.09 240.58c-24.74-24.74-64.54-24.71-89.28 0.03-24.74 24.74-24.72 64.54 0.03 89.28L422.75 510.8 241.84 691.71c-24.74 24.74-24.72 64.54 0.03 89.33 24.74 24.74 64.54 24.71 89.28-0.03L512 600.1l180.91 180.91c24.74 24.74 64.54 24.71 89.28-0.03 24.74-24.74 24.72-64.54-0.03-89.28L601.25 510.8 782.16 329.89c24.74-24.74 24.72-64.54-0.03-89.33-24.74-24.74-64.54-24.71-89.28 0.03L512 421.49z" fill="#888888"></path></svg>';
// 插入全局样式表,统一亮暗色模式
function insertGlobalStyle() {
if (document.getElementById("bili-stream-global-style")) return;
const style = document.createElement("style");
style.id = "bili-stream-global-style";
style.innerHTML = `
:root {
--bili-bg: rgba(255, 255, 255, 0.8);
--bili-fg: #222;
--bili-panel-shadow: 0 8px 32px rgba(0,0,0,0.15);
--bili-border: rgba(238, 238, 238, 0.6);
--bili-input-bg: rgba(255, 255, 255, 0.9);
--bili-input-fg: #222;
--bili-input-border: rgba(221, 221, 221, 0.8);
--bili-tip-bg: rgba(254, 240, 241, 0.9);
--bili-tip-fg: #d92b46;
--bili-tip-border: #fb7299;
--bili-btn-main: #fb7299;
--bili-btn-main-hover: #fc8bab;
--bili-btn-main-disabled: #bfbfbf;
--bili-btn-stop: #ff4b4b;
--bili-btn-stop-hover: #d9363e;
--bili-btn-stop-disabled: #999;
--bili-btn-text: #fff;
--bili-title-color: #fb7299;
--bili-label-color: #666;
--bili-tip-yellow-bg: rgba(255, 251, 230, 0.9);
--bili-tip-yellow-border: #faad14;
--bili-tip-yellow-fg: #faad14;
--bili-tip-green-bg: rgba(230, 255, 237, 0.9);
--bili-tip-green-border: #52c41a;
--bili-tip-green-fg: #389e0d;
}
.bili-dark-mode {
--bili-bg: rgba(35, 35, 36, 0.8);
--bili-fg: #eee;
--bili-panel-shadow: 0 8px 32px rgba(0,0,0,0.4);
--bili-border: rgba(68, 68, 68, 0.6);
--bili-input-bg: rgba(24, 24, 26, 0.9);
--bili-input-fg: #eee;
--bili-input-border: rgba(68, 68, 68, 0.8);
--bili-tip-bg: rgba(45, 35, 38, 0.9);
--bili-tip-fg: #ffb6c1;
--bili-tip-border: #fb7299;
--bili-btn-main: #fb7299;
--bili-btn-main-hover: #fc8bab;
--bili-btn-main-disabled: #bfbfbf;
--bili-btn-stop: #ff4b4b;
--bili-btn-stop-hover: #d9363e;
--bili-btn-stop-disabled: #999;
--bili-btn-text: #fff;
--bili-title-color: #fb7299;
--bili-label-color: #aaa;
--bili-tip-yellow-bg: rgba(58, 45, 26, 0.9);
--bili-tip-yellow-border: #faad14;
--bili-tip-yellow-fg: #ffd666;
--bili-tip-green-bg: rgba(30, 43, 34, 0.9);
--bili-tip-green-border: #52c41a;
--bili-tip-green-fg: #b7eb8f;
}
#bili-stream-code-panel {
background-color: var(--bili-bg) !important;
color: var(--bili-fg) !important;
box-shadow: var(--bili-panel-shadow) !important;
border-radius: 12px;
padding: 12px;
font-family: "Microsoft YaHei", sans-serif;
max-width: 280px;
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--bili-border);
}
#bili-result {
background-color: var(--bili-bg) !important;
color: var(--bili-fg) !important;
border: 1px solid var(--bili-border) !important;
border-radius: 8px;
margin-top: 15px;
padding: 10px;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
.bili-input {
background: var(--bili-input-bg) !important;
color: var(--bili-input-fg) !important;
border: 1px solid var(--bili-input-border) !important;
border-radius: 4px;
padding: 6px 8px;
font-size: 13px;
width: 100%;
box-sizing: border-box;
}
.bili-select {
background: var(--bili-input-bg) !important;
color: var(--bili-input-fg) !important;
border: 1px solid var(--bili-input-border) !important;
border-radius: 4px;
padding: 6px 8px;
font-size: 13px;
width: 100%;
box-sizing: border-box;
}
#bili-room-id, #bili-title, #server-addr, #stream-code {
background: var(--bili-input-bg) !important;
color: var(--bili-input-fg) !important;
border: 1px solid var(--bili-input-border) !important;
border-radius: 4px;
padding: 8px;
font-size: 14px;
}
#bili-area-group, #bili-area {
background: var(--bili-input-bg) !important;
color: var(--bili-input-fg) !important;
border: 1px solid var(--bili-input-border) !important;
border-radius: 4px;
padding: 8px;
font-size: 14px;
}
.bili-important-tip {
background-color: var(--bili-tip-bg) !important;
color: var(--bili-tip-fg) !important;
border-left: 4px solid var(--bili-tip-border) !important;
border-radius: 6px;
margin-top: 8px;
padding: 8px;
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
}
.bili-tip-yellow {
background: var(--bili-tip-yellow-bg);
border-left: 4px solid var(--bili-tip-yellow-border);
color: var(--bili-tip-yellow-fg);
border-radius: 6px;
margin-top: 8px;
padding: 8px;
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
}
.bili-tip-green {
background: var(--bili-tip-green-bg);
border-left: 4px solid var(--bili-tip-green-border);
color: var(--bili-tip-green-fg);
border-radius: 6px;
margin-top: 8px;
padding: 8px;
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
}
#bili-qr-container {
background-color: var(--bili-bg) !important;
color: var(--bili-fg) !important;
border: 1px solid var(--bili-border) !important;
}
#bili-qr-container h3 {
color: var(--bili-title-color) !important;
}
#bili-qr-container p {
color: var(--bili-label-color) !important;
}
.bili-btn-main {
background: var(--bili-btn-main);
color: var(--bili-btn-text);
border: none;
border-radius: 4px;
padding: 10px;
cursor: pointer;
font-size: 14px;
transition: background 0.3s, opacity 0.3s;
}
.bili-btn-main:hover:not(:disabled) {
background: var(--bili-btn-main-hover);
}
.bili-btn-main:disabled {
background: var(--bili-btn-main-disabled);
opacity: 0.5;
cursor: not-allowed;
}
.bili-btn-stop {
background: var(--bili-btn-stop);
color: var(--bili-btn-text);
border: none;
border-radius: 4px;
padding: 10px;
cursor: pointer;
font-size: 14px;
transition: background 0.3s, opacity 0.3s;
}
.bili-btn-stop:hover:not(:disabled) {
background: var(--bili-btn-stop-hover);
}
.bili-btn-stop:disabled {
background: var(--bili-btn-stop-disabled);
opacity: 0.5;
cursor: not-allowed;
}
.bili-title {
color: var(--bili-title-color) !important;
font-size: 18px;
margin: 0;
}
.bili-title:hover {
opacity: 0.8;
text-decoration: underline !important;
}
.bili-label {
color: var(--bili-label-color);
font-size: 14px;
}
.bili-copy-btn {
margin-left: 5px;
background: var(--bili-btn-main);
color: var(--bili-btn-text);
border: none;
border-radius: 4px;
padding: 6px 8px;
cursor: pointer;
transition: background 0.3s;
font-size: 12px;
flex-shrink: 0;
}
.bili-copy-btn:disabled {
background: var(--bili-btn-main-disabled);
cursor: not-allowed;
}
.bili-copy-btn:hover:not(:disabled) {
background: var(--bili-btn-main-hover);
}
.bili-message {
color: var(--bili-fg);
font-size: 15px;
margin: 0;
}
.bili-message-error {
color: red;
}
.bili-status-success {
background: #d4edda !important;
color: #155724 !important;
border: 1px solid #c3e6cb !important;
}
.bili-status-error {
background: #f8d7da !important;
color: #721c24 !important;
border: 1px solid #f5c6cb !important;
}
`;
document.head.appendChild(style);
}
// 全局变量
let roomId = null; // 当前房间ID
let csrf = null; // CSRF令牌
let startLiveButton = null; // "开始直播"按钮引用
let stopLiveButton = null; // "结束直播"按钮引用
let editTitleButton = null; // "修改标题"按钮引用
let editAreaButton = null; // "修改分区"按钮引用
let isLiveStarted = GM_getValue(STORAGE_KEYS.IS_LIVE_STARTED, false); // 直播状态
let streamInfo = GM_getValue(STORAGE_KEYS.STREAM_INFO, null); // 推流信息缓存
// 请求头
const headers = {
accept: "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
origin: "https://link.bilibili.com",
referer: "https://link.bilibili.com/p/center/index",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
};
// 开始直播数据模板
const startData = {
room_id: "",
platform: "pc_link", //感谢bilibili Z-Lake提供
area_v2: "",
backup_stream: "0",
csrf_token: "",
csrf: "",
};
// 停止直播数据模板
const stopData = {
room_id: "",
platform: "pc_link", //感谢bilibili Z-Lake提供
csrf_token: "",
csrf: "",
};
// 修改直播标题数据模板
const titleData = {
room_id: "",
platform: "pc_link", //感谢bilibili Z-Lake提供
title: "",
csrf_token: "",
csrf: "",
};
// 修改直播分区数据模板
const areaData = {
room_id: "",
platform: "pc_link", //感谢bilibili Z-Lake提供
area_id: "",
csrf_token: "",
csrf: "",
};
// 人脸验证数据模板
const faceAuthData = {
room_id: "",
face_auth_code: "60024",
csrf_token: "",
csrf: "",
visit_id: "",
};
// 初始化入口
function init() {
try {
insertGlobalStyle(); // 插入全局样式
removeExistingComponents(); // 清理旧组件
createUI(); // 创建UI(只创建一次主面板)
restoreLiveState(); // 恢复直播状态
} catch (error) {
console.error("B站推流码获取工具初始化失败:", error);
}
}
// 移除已存在的组件
function removeExistingComponents() {
const existingPanel = document.getElementById("bili-stream-code-panel");
if (existingPanel) {
// 清理事件监听器
if (existingPanel._clickOutsideHandler) {
document.removeEventListener(
"click",
existingPanel._clickOutsideHandler,
true
);
}
existingPanel.remove();
}
const existingButton = document.getElementById("bili-stream-float-button");
if (existingButton) existingButton.remove();
// 清空按钮引用,防止旧引用干扰
startLiveButton = null;
stopLiveButton = null;
editTitleButton = null;
editAreaButton = null;
}
// 创建UI(只创建一次主面板)
function createUI() {
// 若主面板已存在则不再重复创建
if (!document.getElementById("bili-stream-code-panel")) {
const panel = createPanel();
panel.style.display = "none";
}
// 浮动按钮可重复创建(防止丢失)
createFloatButton();
}
// 创建面板
function createPanel() {
const panel = document.createElement("div");
panel.id = "bili-stream-code-panel";
panel.style.cssText = `
position: fixed;
top: 70px;
right: 10px;
width: 240px;
z-index: 10000;
display: none;
`;
// 头部区域
const header = createPanelHeader();
panel.appendChild(header);
// 表单区域
const form = createPanelForm();
panel.appendChild(form);
// 结果区域
const resultArea = document.createElement("div");
resultArea.id = "bili-result";
resultArea.style.cssText = `
margin-top: 15px;
padding: 10px;
border: 1px solid #eee;
border-radius: 4px;
background-color: #f9f9f9;
display: none;
`;
panel.appendChild(resultArea);
document.body.appendChild(panel);
// 新增:设置点击外部隐藏面板
setupClickOutsideHandler(panel);
return panel;
}
// 新增:设置点击外部隐藏面板的处理器
function setupClickOutsideHandler(panel) {
function handleClickOutside(event) {
// 只在面板可见时处理
if (panel.style.display === "none") return;
// 检查点击目标
const floatButton = document.getElementById("bili-stream-float-button");
const isClickInPanel = panel.contains(event.target);
const isClickOnFloatButton =
floatButton && floatButton.contains(event.target);
// 点击面板外且不是浮动按钮时隐藏面板
if (!isClickInPanel && !isClickOnFloatButton) {
panel.style.display = "none";
}
}
// 使用捕获阶段监听,确保在其他事件处理器之前执行
document.addEventListener("click", handleClickOutside, true);
// 存储处理器引用,便于清理
panel._clickOutsideHandler = handleClickOutside;
}
// 创建面板头部
function createPanelHeader() {
const header = document.createElement("div");
header.style.cssText =
"display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;";
// 标题 - 改为可点击的链接
const title = document.createElement("a");
title.textContent = "推流码获取";
title.className = "bili-title";
title.href = "https://github.com/smathsp/UserScript";
title.target = "_blank";
title.style.cssText = "text-decoration: none; cursor: pointer;";
title.title = "访问 GitHub 仓库";
// 亮暗模式切换按钮
const modeBtn = document.createElement("button");
modeBtn.id = "bili-mode-toggle";
modeBtn.style.cssText =
"width: 28px; height: 28px; border: none; background: transparent; cursor: pointer; display: flex; align-items: center; justify-content: center; margin-left: 8px;";
// SVG 图标
let isDarkMode = GM_getValue(STORAGE_KEYS.DARK_MODE, false);
modeBtn.innerHTML = isDarkMode ? MOON_SVG : SUN_SVG;
modeBtn.title = isDarkMode ? "切换为亮色模式" : "切换为暗色模式";
modeBtn.onclick = function () {
isDarkMode = !isDarkMode;
GM_setValue(STORAGE_KEYS.DARK_MODE, isDarkMode);
modeBtn.innerHTML = isDarkMode ? MOON_SVG : SUN_SVG;
modeBtn.title = isDarkMode ? "切换为亮色模式" : "切换为暗色模式";
applyColorMode(isDarkMode);
};
// 首次渲染时应用模式
setTimeout(() => applyColorMode(isDarkMode), 0);
// 关闭按钮
const closeButton = document.createElement("button");
closeButton.innerHTML = CLOSE_SVG;
closeButton.style.cssText =
"width: 24px; height: 24px; border: none; background: transparent; cursor: pointer; display: flex; align-items: center; justify-content: center;";
closeButton.onclick = () => {
document.getElementById("bili-stream-code-panel").style.display = "none";
};
// 头部右侧按钮组
const rightBtns = document.createElement("div");
rightBtns.style.cssText = "display: flex; align-items: center; gap: 4px;";
rightBtns.appendChild(modeBtn);
rightBtns.appendChild(closeButton);
header.appendChild(title);
header.appendChild(rightBtns);
return header;
}
// 亮暗模式应用函数
function applyColorMode(isDark) {
// 只切换 class,不再手动设置 style
const root = document.documentElement;
if (isDark) {
root.classList.add("bili-dark-mode");
} else {
root.classList.remove("bili-dark-mode");
}
}
// 创建面板表单
function createPanelForm() {
const form = document.createElement("div");
form.style.cssText = "display: flex; flex-direction: column; gap: 10px;";
// 房间ID输入
form.appendChild(createRoomIdInput());
// 分区选择
const areaSelectionElement = createAreaSelection();
form.appendChild(areaSelectionElement);
if (areaSelectionElement.loadAndBindAreaListPromise) {
areaSelectionElement.loadAndBindAreaListPromise
.then(() => {
autoFillRoomId();
})
.catch((error) => {
console.error(
"Error during area list loading, or in autoFillRoomId:",
error
);
setTimeout(autoFillRoomId, 300);
});
} else {
console.warn(
"loadAndBindAreaListPromise not found, falling back for autoFillRoomId."
);
setTimeout(autoFillRoomId, 300);
}
// 标题输入
form.appendChild(createTitleInput());
// 按钮组
form.appendChild(createButtonGroup());
return form;
}
// 创建房间ID输入
function createRoomIdInput() {
const container = document.createElement("div");
container.style.cssText =
"display: flex; flex-direction: column; gap: 5px;";
const label = document.createElement("label");
label.textContent = "房间ID:";
label.className = "bili-label";
const input = document.createElement("input");
input.type = "text";
input.id = "bili-room-id";
input.placeholder = "请输入你的房间ID";
input.className = "bili-input";
// 新增:输入时保存
input.addEventListener("blur", function () {
GM_setValue(STORAGE_KEYS.LAST_ROOM_ID, input.value.trim());
});
container.appendChild(label);
container.appendChild(input);
return container;
}
// 创建分区选择
function createAreaSelection() {
const container = document.createElement("div");
container.id = "bili-area-selection-container";
container.style.cssText =
"display: flex; flex-direction: column; gap: 5px;";
const label = document.createElement("label");
label.textContent = "直播分区:";
label.className = "bili-label";
// 加载指示器
const loading = document.createElement("div");
loading.id = "bili-area-loading";
loading.textContent = "正在加载分区列表...";
loading.style.cssText =
"padding: 8px; color: #666; font-size: 14px; text-align: center; cursor: pointer;";
// 分区组选择器
const groupSelect = document.createElement("select");
groupSelect.id = "bili-area-group";
groupSelect.className = "bili-select";
groupSelect.style.cssText = "margin-bottom: 8px; display: none;";
// 子分区选择器
const areaSelect = document.createElement("select");
areaSelect.id = "bili-area";
areaSelect.className = "bili-select";
areaSelect.style.cssText = "display: none;";
// 统一事件绑定
groupSelect.addEventListener("change", function () {
const areaList = getCachedAreaList() || [];
const selectedIndex = this.options[this.selectedIndex].dataset.index;
GM_setValue(STORAGE_KEYS.LAST_GROUP_ID, groupSelect.value);
updateAreaSelectors(
areaList,
Number(selectedIndex),
groupSelect,
areaSelect
);
});
areaSelect.addEventListener("change", function () {
GM_setValue(STORAGE_KEYS.LAST_AREA_ID, areaSelect.value);
GM_setValue(STORAGE_KEYS.LAST_GROUP_ID, groupSelect.value);
});
loading.onclick = function () {
if (
loading.style.color === "rgb(255, 75, 75)" ||
loading.style.color === "#ff4b4b"
) {
loadAndBindAreaList();
}
};
container.appendChild(label);
container.appendChild(loading);
container.appendChild(groupSelect);
container.appendChild(areaSelect);
// 合并后的分区刷新函数
function updateAreaSelectors(
areaList,
groupIdx = 0,
groupSel = groupSelect,
areaSel = areaSelect
) {
groupSel.innerHTML = "";
areaSel.innerHTML = "";
areaList.forEach((group, idx) => {
const option = document.createElement("option");
option.value = group.id;
option.textContent = group.name;
option.dataset.index = idx;
groupSel.appendChild(option);
});
// 恢复上次大类
const lastGroupId = GM_getValue(STORAGE_KEYS.LAST_GROUP_ID);
if (lastGroupId) {
for (let i = 0; i < groupSel.options.length; i++) {
if (groupSel.options[i].value == lastGroupId) {
groupSel.selectedIndex = i;
groupIdx = i;
break;
}
}
}
if (areaList[groupIdx] && areaList[groupIdx].list) {
areaList[groupIdx].list.forEach((area) => {
const option = document.createElement("option");
option.value = area.id;
option.textContent = area.name;
areaSel.appendChild(option);
});
}
// 恢复上次分区id
const lastAreaId = GM_getValue(STORAGE_KEYS.LAST_AREA_ID);
if (lastAreaId && areaSel.options.length > 0) {
for (let i = 0; i < areaSel.options.length; i++) {
if (areaSel.options[i].value == lastAreaId) {
areaSel.selectedIndex = i;
break;
}
}
}
// 显示选择器
loading.style.display = "none";
groupSel.style.display = "block";
areaSel.style.display = "block";
}
// 加载分区数据
function loadAndBindAreaList() {
return new Promise(async (resolve, reject) => {
// 返回 Promise
loading.style.display = "block";
groupSelect.style.display = "none";
areaSelect.style.display = "none";
loading.textContent = "正在加载分区列表...";
loading.style.color = "#666";
const cachedList = getCachedAreaList();
if (cachedList) {
updateAreaSelectors(cachedList, 0, groupSelect, areaSelect);
resolve(); // 解析 Promise
return;
}
try {
const response = await gmRequest({
method: "GET",
url: API_URL_AREA_LIST,
headers: headers,
});
const result = JSON.parse(response.responseText);
if (result.code === 0) {
cacheAreaList(result.data);
updateAreaSelectors(result.data, 0, groupSelect, areaSelect);
resolve(); // 解析 Promise
} else {
console.error("Area list API error:", result);
showAreaLoadError();
reject(new Error("Failed to load area list")); // 拒绝 Promise
}
} catch (errorResponse) {
console.error("Area list request error:", errorResponse);
showAreaLoadError();
reject(errorResponse); // 拒绝 Promise
}
});
}
// 将 Promise 附加到容器元素,以便在 createUI 中访问
container.loadAndBindAreaListPromise = loadAndBindAreaList();
return container;
}
// 创建标题输入
function createTitleInput() {
const container = document.createElement("div");
container.style.cssText =
"display: flex; flex-direction: column; gap: 5px;";
const label = document.createElement("label");
label.textContent = "直播标题:";
label.className = "bili-label";
const input = document.createElement("input");
input.type = "text";
input.id = "bili-title";
input.placeholder = "请输入直播标题";
input.className = "bili-input";
// 新增:输入时保存
input.addEventListener("blur", function () {
GM_setValue(STORAGE_KEYS.LAST_TITLE, input.value.trim());
});
container.appendChild(label);
container.appendChild(input);
return container;
}
// 创建按钮组
function createButtonGroup() {
const container = document.createElement("div");
container.style.cssText = "display: flex; flex-direction: column; gap: 8px; margin-top: 10px;";
// 修改按钮组(直播中显示)
const editButtonsContainer = document.createElement("div");
editButtonsContainer.id = "bili-edit-buttons";
editButtonsContainer.style.cssText = "display: none; gap: 8px;";
// 修改标题按钮
editTitleButton = document.createElement("button");
editTitleButton.textContent = "修改标题";
editTitleButton.className = "bili-btn-main";
editTitleButton.style.cssText = "flex: 1; font-size: 13px; padding: 8px;";
editTitleButton.onclick = editLiveTitle;
// 修改分区按钮
editAreaButton = document.createElement("button");
editAreaButton.textContent = "修改分区";
editAreaButton.className = "bili-btn-main";
editAreaButton.style.cssText = "flex: 1; font-size: 13px; padding: 8px;";
editAreaButton.onclick = editLiveArea;
editButtonsContainer.appendChild(editTitleButton);
editButtonsContainer.appendChild(editAreaButton);
// 主按钮组
const mainButtonsContainer = document.createElement("div");
mainButtonsContainer.style.cssText = "display: flex; gap: 10px;";
// 开始直播按钮
startLiveButton = document.createElement("button");
startLiveButton.textContent = "获取推流码并开始直播";
startLiveButton.className = "bili-btn-main";
startLiveButton.style.flex = "1";
startLiveButton.onclick = startLive;
// 结束直播按钮
stopLiveButton = document.createElement("button");
stopLiveButton.textContent = "结束直播";
stopLiveButton.className = "bili-btn-stop";
stopLiveButton.style.flex = "1";
stopLiveButton.disabled = true;
stopLiveButton.onclick = stopLive;
mainButtonsContainer.appendChild(startLiveButton);
mainButtonsContainer.appendChild(stopLiveButton);
container.appendChild(editButtonsContainer);
container.appendChild(mainButtonsContainer);
return container;
}
// 创建浮动按钮
function createFloatButton() {
const button = document.createElement("div");
button.id = "bili-stream-float-button";
button.innerHTML =
'<svg viewBox="0 0 1024 1024" width="24" height="24"><path d="M718.3 183.7H305.7c-122 0-221 99-221 221v214.6c0 122 99 221 221 221h412.6c122 0 221-99 221-221V404.7c0-122-99-221-221-221z m159.1 435.6c0 87.6-71.5 159.1-159.1 159.1H305.7c-87.6 0-159.1-71.5-159.1-159.1V404.7c0-87.6 71.5-159.1 159.1-159.1h412.6c87.6 0 159.1 71.5 159.1 159.1v214.6z" fill="#FFFFFF"></path><path d="M415.5 532.2v-131c0-7.1 3.8-13.6 10-17.1 6.2-3.5 13.7-3.5 19.9 0l131 75.1c6.2 3.5 10 10.1 10 17.1 0 7.1-3.8 13.6-10 17.1l-131 65.5c-6.2 3.5-13.7 3.5-19.9 0-6.2-3.5-10-10.1-10-17.1v-9.6z" fill="#FFFFFF"></path></svg>';
button.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
width: 48px;
height: 48px;
border-radius: 50%;
background-color: #fb7299;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
z-index: 10001;
transition: transform 0.3s;
`;
button.onmouseover = function () {
this.style.transform = "scale(1.1)";
};
button.onmouseout = function () {
this.style.transform = "scale(1)";
};
button.onclick = togglePanel;
document.body.appendChild(button);
return button;
}
// 检查实际直播状态
async function checkActualLiveStatus() {
if (!roomId || !csrf) return;
try {
const response = await gmRequest({
method: "POST",
url: "https://api.live.bilibili.com/room/v1/Room/get_info",
headers: headers,
data: new URLSearchParams({ room_id: roomId }).toString(),
});
const result = JSON.parse(response.responseText);
if (result.code === 0 && result.data) {
const actualLiveStatus = result.data.live_status; // 0=未开播,1=直播中,2=轮播中
// 检查本地状态与实际状态是否一致
if (isLiveStarted && actualLiveStatus !== 1) {
// 本地认为在直播,但实际未直播 - 状态不一致
console.log("检测到直播状态不一致:本地显示直播中,但服务器显示未直播");
// 更新本地状态
isLiveStarted = false;
streamInfo = null;
GM_setValue(STORAGE_KEYS.IS_LIVE_STARTED, false);
GM_setValue(STORAGE_KEYS.STREAM_INFO, null);
// 更新UI
updateButtonsForLive(false);
// 清除结果区域或显示提示
const resultArea = document.getElementById("bili-result");
if (resultArea) {
resultArea.innerHTML = `<div class="bili-tip-yellow"><span style="font-weight:bold;">提示:</span>检测到该房间直播已被结束,已自动更新状态</div>`;
resultArea.style.display = "block";
}
// 显示通知
GM_notification({
text: "检测到直播已结束,状态已同步",
title: "B站推流码获取工具",
timeout: 5000,
});
} else if (!isLiveStarted && actualLiveStatus === 1) {
// 本地认为未直播,但实际在直播 - 可能是其他地方开启的直播
console.log("检测到可能存在其他地方开启的直播");
const resultArea = document.getElementById("bili-result");
if (resultArea) {
resultArea.innerHTML = `<div class="bili-tip-yellow"><span style="font-weight:bold;">提示:</span>检测到该房间正在直播中,可能是通过其他方式开启的</div>`;
resultArea.style.display = "block";
}
}
}
} catch (error) {
console.error("检查直播状态失败:", error);
// 静默失败,不影响正常使用
}
}
// 显示/隐藏面板
async function togglePanel() {
const panel = document.getElementById("bili-stream-code-panel");
if (!panel) return; // 理论上不会发生
const isShowing = panel.style.display === "none" || !panel.style.display;
panel.style.display = isShowing ? "block" : "none";
// 如果是显示面板,检查直播状态
if (isShowing) {
// 延迟一点检查,确保面板已显示
setTimeout(() => {
checkActualLiveStatus();
}, 100);
}
}
// 检查浮动按钮
function checkFloatButton() {
if (!document.getElementById("bili-stream-float-button")) {
createFloatButton();
}
}
// 显示分区加载错误信息
function showAreaLoadError() {
const loading = document.getElementById("bili-area-loading");
if (loading) {
loading.textContent = "无法加载分区列表,请稍后刷新重试";
loading.style.color = "#ff4b4b";
}
// 显示通知
GM_notification({
text: "无法加载直播分区列表,请检查网络连接或登录状态",
title: "B站推流码获取工具",
timeout: 5000,
});
}
// 更新分区选择器
function updateAreaSelectors(areaList) {
const loading = document.getElementById("bili-area-loading");
const groupSelect = document.getElementById("bili-area-group");
const areaSelect = document.getElementById("bili-area");
// 防止 loading 取不到时报错