forked from SeaLoong/BLRHH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bilibili直播间挂机助手.user.js
2438 lines (2401 loc) · 141 KB
/
Bilibili直播间挂机助手.user.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
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 Bilibili直播间挂机助手
// @namespace SeaLoong
// @version 2.4.11
// @description Bilibili直播间自动签到,领瓜子,参加抽奖,完成任务,送礼等
// @author SeaLoong
// @homepageURL https://github.com/SeaLoong/Bilibili-LRHH
// @supportURL https://github.com/SeaLoong/Bilibili-LRHH/issues
// @updateURL https://raw.githubusercontent.com/SeaLoong/Bilibili-LRHH/master/Bilibili%E7%9B%B4%E6%92%AD%E9%97%B4%E6%8C%82%E6%9C%BA%E5%8A%A9%E6%89%8B.user.js
// @downloadURL https://raw.githubusercontent.com/SeaLoong/Bilibili-LRHH/master/Bilibili%E7%9B%B4%E6%92%AD%E9%97%B4%E6%8C%82%E6%9C%BA%E5%8A%A9%E6%89%8B.user.js
// @include /^https?:\/\/live\.bilibili\.com\/[^?]*?\d+\??[^?]*$/
// @include /^https?:\/\/api\.live\.bilibili\.com\/_.*$/
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @require https://js-1258131272.file.myqcloud.com/BilibiliAPI.js
// @require https://js-1258131272.file.myqcloud.com/OCRAD.min.js
// @grant none
// @run-at document-start
// @license MIT License
// ==/UserScript==
/*
[greasyfork源]
// @require https://greasyfork.org/scripts/38140-bilibiliapi/code/BilibiliAPI.js
// @require https://greasyfork.org/scripts/44866-ocrad/code/OCRAD.js
[github源]
// @require https://raw.githubusercontent.com/SeaLoong/Bilibili-LRHH/master/BilibiliAPI.js
// @require https://raw.githubusercontent.com/SeaLoong/Bilibili-LRHH/master/OCRAD.min.js
[gitee源]
// @require https://gitee.com/SeaLoong/Bilibili-LRHH/raw/master/BilibiliAPI.js
// @require https://gitee.com/SeaLoong/Bilibili-LRHH/raw/master/OCRAD.min.js
[腾讯云源]
// @require https://js-1258131272.file.myqcloud.com/BilibiliAPI.js
// @require https://js-1258131272.file.myqcloud.com/OCRAD.min.js
[jsDelivr源]
// @require https://cdn.jsdelivr.net/gh/SeaLoong/Bilibili-LRHH/BilibiliAPI.js
// @require https://cdn.jsdelivr.net/gh/SeaLoong/Bilibili-LRHH/OCRAD.min.js
*/
(function BLRHH() {
'use strict';
const NAME = 'BLRHH';
const VERSION = '2.4.10';
document.domain = 'bilibili.com';
let API;
try {
API = BilibiliAPI;
} catch (err) {
window.toast('BilibiliAPI初始化失败,脚本已停用!', 'error');
console.error(`[${NAME}]`, err);
return;
}
const isSubScript = () => window.frameElement && window.parent[NAME] && window.frameElement[NAME];
const DEBUGMODE = false || window.top.localStorage.getItem('BLRHH-DEBUG');
const DEBUG = (sign, ...data) => {
if (!DEBUGMODE) return;
let d = new Date();
d = `[${NAME}]${(isSubScript() ? 'SubScript:' : '')}[${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}:${d.getMilliseconds()}]`;
if (data.length === 1) console.debug(d, `${sign}:`, data[0]);
else console.debug(d, `${sign}:`, data);
};
let CONFIG;
let CACHE;
let Info = {
short_id: undefined,
roomid: undefined,
uid: undefined,
ruid: undefined,
rnd: undefined,
csrf_token: undefined,
visit_id: undefined,
silver: undefined,
gold: undefined,
mobile_verify: undefined,
identification: undefined,
gift_list: undefined,
gift_list_str: '礼物对照表',
blocked: false
};
const tz_offset = new Date().getTimezoneOffset() + 480;
const ts_s = () => Math.round(ts_ms() / 1000);
const ts_ms = () => Date.now();
const delayCall = (callback, delay = 10e3) => {
const p = $.Deferred();
setTimeout(() => {
const t = callback();
if (t && t.then) t.then((arg1, arg2, arg3, arg4, arg5, arg6) => p.resolve(arg1, arg2, arg3, arg4, arg5, arg6));
else p.resolve();
}, delay);
return p;
};
const checkNewDay = (ts) => {
// 检查是否为新的一天,以UTC+8为准
const t = new Date(ts);
t.setMinutes(t.getMinutes() + tz_offset);
t.setHours(0, 0, 0, 0);
const d = new Date();
d.setMinutes(t.getMinutes() + tz_offset);
return (d - t > 86400e3);
};
const runTomorrow = (callback) => {
const t = new Date();
t.setMinutes(t.getMinutes() + tz_offset);
t.setDate(t.getDate() + 1);
t.setHours(0, 1, 0, 0);
t.setMinutes(t.getMinutes() - tz_offset);
setTimeout(callback, t - ts_ms());
DEBUG('runTomorrow', t.toString());
};
if (isSubScript()) {
try {
let host_server_list = [];
try {
// 拦截弹幕服务器连接
const webSocketConstructor = WebSocket.prototype.constructor;
WebSocket.prototype.constructor = (url, protocols) => {
if (host_server_list.some(v => url.indexOf(v) > -1)) return webSocketConstructor(url, protocols);
throw new Error();
};
} catch (err) {};
try {
// 拦截直播流
window.fetch = () => new Promise(() => {
throw new Error();
});
} catch (err) {};
try {
// 清空页面元素和节点
$('html').remove();
} catch (err) {};
DEBUG('initing', window.frameElement[NAME]);
// 读取父脚本数据
window.toast = window.parent.toast;
Info = window.parent[NAME].Info;
CONFIG = window.parent[NAME].CONFIG;
CACHE = window.parent[NAME].CACHE;
API.setCommonArgs(Info.csrf_token, '');
const down = () => {
Info = window.parent[NAME].Info;
CONFIG = window.parent[NAME].CONFIG;
CACHE = window.parent[NAME].CACHE;
const pDown = $.Deferred();
pDown.then(down);
window.frameElement[NAME].promise.down = pDown;
};
window.frameElement[NAME].promise.down.then(down);
const up = () => {
window.parent[NAME].Info = Info;
window.parent[NAME].CACHE = CACHE;
window.frameElement[NAME].promise.up.resolve();
};
window.frameElement[NAME].promise.init.resolve();
// 正式执行子脚本
if (window.frameElement[NAME].type === 'LOTTERY') {
let timer_next;
const finish = () => {
if (timer_next) clearTimeout(timer_next);
if (Lottery.ws.readyState === WebSocket.OPEN) Lottery.ws.close();
Lottery.ws = undefined;
window.frameElement[NAME].promise.finish.resolve();
};
const readyFinish = (t = 9e3) => {
if (timer_next) clearTimeout(timer_next);
if (t < 9e3) t = 9e3;
timer_next = setTimeout(finish, t);
};
const Lottery = {
ws: undefined,
raffleIdSet: new Set(),
guardIdSet: new Set(),
Gift: {
fishingCheck: (roomid) => {
const p = $.Deferred();
API.room.room_init(roomid).then((response) => {
DEBUG('Lottery.Gift.fishingCheck: API.room.room_init', response);
if (response.code === 0) {
if (response.data.is_hidden || response.data.is_locked || response.data.encrypted || response.data.pwd_verified) return p.resolve(true);
return p.resolve(false);
}
p.reject();
}, () => {
p.reject();
}).always(() => {
API.room.room_entry_action(roomid);
});
return p;
},
run: (roomid) => {
// 全部参加完成返回resolve,任意一个失败返回reject
try {
if (!CONFIG.AUTO_LOTTERY_CONFIG.GIFT_LOTTERY || Info.blocked) return $.Deferred().resolve();
return Lottery.Gift.fishingCheck(roomid).then((fishing) => {
if (!fishing) {
return API.Lottery.Gift.check(roomid).then((response) => {
DEBUG('Lottery.Gift.run: API.Lottery.Gift.check', response);
if (response.code === 0) {
if (response.data.list) return Lottery.Gift.join(roomid, response.data.list);
} else if (response.code === -400) {
// 没有需要提示的小电视
} else {
window.toast(`[自动抽奖][礼物抽奖]${response.msg}`, 'caution');
}
}, () => {
window.toast(`[自动抽奖][礼物抽奖]检查直播间(${roomid})失败,请检查网络`, 'error');
return delayCall(() => Lottery.Gift.run(roomid));
});
}
});
} catch (err) {
window.toast('[自动抽奖][礼物抽奖]运行时出现异常', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
},
join: (roomid, raffleList, i = 0) => {
if (Info.blocked) return $.Deferred().resolve();
if (i >= raffleList.length) return $.Deferred().resolve();
const obj = raffleList[i];
if (obj.status === 1) { // 可以参加
return Lottery.Gift._join(roomid, obj.raffleId, obj.type, obj.time_wait).then(() => Lottery.Gift.join(roomid, raffleList, i + 1));
} else if (obj.status === 2 && obj.time > 0) { // 已参加且未开奖
}
return Lottery.Gift.join(roomid, raffleList, i + 1);
},
_join: (roomid, raffleId, type, time_wait = 0) => {
if (Info.blocked) return $.Deferred().resolve();
roomid = parseInt(roomid, 10);
raffleId = parseInt(raffleId, 10);
if (isNaN(roomid) || isNaN(raffleId)) return $.Deferred().reject();
// raffleId过滤,防止重复参加
if (Lottery.raffleIdSet.has(raffleId)) return $.Deferred().resolve();
Lottery.raffleIdSet.add(raffleId); // 加入raffleId记录列表
readyFinish(time_wait * 1e3 + 9e3);
return delayCall(() => API.Lottery.Gift.join(roomid, raffleId, type).then((response) => {
DEBUG('Lottery.Gift._join: API.Lottery.Gift.join', response);
switch (response.code) {
case 0:
window.toast(`[自动抽奖][礼物抽奖]已参加抽奖(roomid=${roomid},id=${raffleId},type=${type})`, 'success');
break;
case 402:
// 抽奖已过期,下次再来吧
break;
case 65531:
// 65531: 非当前直播间或短ID直播间试图参加抽奖
Info.blocked = true;
up();
window.toast(`[自动抽奖][礼物抽奖]参加抽奖(roomid=${roomid},id=${raffleId},type=${type})失败,已停止`, 'error');
break;
default:
if (response.msg.indexOf('拒绝') > -1) {
Info.blocked = true;
up();
window.toast('[自动抽奖][礼物抽奖]访问被拒绝,您的帐号可能已经被关小黑屋,已停止', 'error');
} else if (response.msg.indexOf('快') > -1) {
return delayCall(() => Lottery.Gift._join(roomid, raffleId));
} else {
window.toast(`[自动抽奖][礼物抽奖](roomid=${roomid},id=${raffleId},type=${type})${response.msg}`, 'caution');
}
}
}, () => {
window.toast(`[自动抽奖][礼物抽奖]参加抽奖(roomid=${roomid},id=${raffleId},type=${type})失败,请检查网络`, 'error');
return delayCall(() => Lottery.Gift._join(roomid, raffleId));
}), time_wait * 1e3 + 5e3);
}
},
Guard: {
run: (roomid) => {
try {
if (!CONFIG.AUTO_LOTTERY_CONFIG.GUARD_AWARD || Info.blocked) return $.Deferred().resolve();
return API.xlive.lottery.check(roomid).then((response) => {
DEBUG('Lottery.Guard.run: API.xlive.lottery.check', response);
if (response.code === 0) {
return Lottery.Guard.join(roomid, response.data.guard);
} else {
window.toast(`[自动抽奖][舰队领奖](roomid=${roomid})${response.msg}`, 'caution');
}
}, () => {
window.toast(`[自动抽奖][舰队领奖]检查直播间(${roomid})失败,请检查网络`, 'error');
return delayCall(() => Lottery.Guard.run(roomid));
});
} catch (err) {
window.toast('[自动抽奖][舰队领奖]运行时出现异常', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
},
join: (roomid, guard, i = 0) => {
if (Info.blocked) return $.Deferred().resolve();
if (i >= guard.length) return $.Deferred().resolve();
const obj = guard[i];
if (obj.status === 1) {
return Lottery.Guard._join(roomid, obj.id).then(() => Lottery.Guard.join(roomid, guard, i + 1));
}
return Lottery.Guard.join(roomid, guard, i + 1);
},
_join: (roomid, id) => {
if (Info.blocked) return $.Deferred().resolve();
roomid = parseInt(roomid, 10);
id = parseInt(id, 10);
if (isNaN(roomid) || isNaN(id)) return $.Deferred().reject();
// id过滤,防止重复参加
if (Lottery.guardIdSet.has(id)) return $.Deferred().resolve();
Lottery.guardIdSet.add(id); // 加入id记录列表
return API.Lottery.Guard.join(roomid, id).then((response) => {
DEBUG('Lottery.Guard._join: API.Lottery.Guard.join', response);
if (response.code === 0) {
window.toast(`[自动抽奖][舰队领奖]领取(roomid=${roomid},id=${id})成功`, 'success');
} else if (response.msg.indexOf('拒绝') > -1) {
Info.blocked = true;
up();
window.toast('[自动抽奖][舰队领奖]访问被拒绝,您的帐号可能已经被关小黑屋,已停止', 'error');
} else if (response.msg.indexOf('快') > -1) {
return delayCall(() => Lottery.Guard._join(roomid, id));
} else if (response.msg.indexOf('过期') > -1) {
} else {
window.toast(`[自动抽奖][舰队领奖](roomid=${roomid},id=${id})${response.msg}`, 'caution');
}
}, () => {
window.toast(`[自动抽奖][舰队领奖]领取(roomid=${roomid},id=${id})失败,请检查网络`, 'error');
return delayCall(() => Lottery.Guard._join(roomid, id));
});
}
}
};
const listen = () => {
return API.room.getConf(window.frameElement[NAME].roomid).then((response) => {
DEBUG('listen: API.room.getConf', response);
host_server_list = response.data.host_server_list;
Lottery.ws = new API.DanmuWebSocket(Info.uid, window.frameElement[NAME].roomid, host_server_list, response.data.token);
Lottery.ws.bind((ws) => {
Lottery.ws = ws;
}, () => {
if (Info.blocked) {
finish();
return;
}
if (CONFIG.AUTO_LOTTERY_CONFIG.GIFT_LOTTERY) Lottery.Gift.run(window.frameElement[NAME].roomid).always(readyFinish);
if (CONFIG.AUTO_LOTTERY_CONFIG.GUARD_AWARD) Lottery.Guard.run(window.frameElement[NAME].roomid).always(readyFinish);
}, undefined, (obj) => {
if (Info.blocked) {
finish();
return;
}
switch (obj.cmd) {
case 'GUARD_LOTTERY_START':
if (CONFIG.AUTO_LOTTERY_CONFIG.GUARD_AWARD) {
if (obj.data.roomid === window.frameElement[NAME].roomid && obj.data.lottery.id) Lottery.Guard._join(window.frameElement[NAME].roomid, obj.data.lottery.id);
}
break;
case 'RAFFLE_START':
case 'TV_START':
if (CONFIG.AUTO_LOTTERY_CONFIG.GIFT_LOTTERY) {
if (obj.data.msg.real_roomid === window.frameElement[NAME].roomid && obj.data.raffleId) Lottery.Gift._join(window.frameElement[NAME].roomid, obj.data.raffleId, obj.data.type, obj.data.time_wait);
}
break;
case 'SPECIAL_GIFT':
if (obj.data['39'] !== undefined) {
switch (obj.data['39'].action) {
case 'start':
// 节奏风暴开始
case 'end':
// 节奏风暴结束
}
};
break;
default:
return;
}
readyFinish();
});
}, () => delayCall(() => listen()));
};
listen();
} else if (window.frameElement[NAME].type === 'GROUPSIGN|DAILYREWARD') {
const GroupSign = {
getGroups: () => {
return API.Group.my_groups().then((response) => {
DEBUG('GroupSign.getGroups: API.Group.my_groups', response);
if (response.code === 0) return $.Deferred().resolve(response.data.list);
window.toast(`[自动应援团签到]'${response.msg}`, 'caution');
return $.Deferred().reject();
}, () => {
window.toast('[自动应援团签到]获取应援团列表失败,请检查网络', 'error');
return delayCall(() => GroupSign.getGroups());
});
},
signInList: (list, i = 0) => {
if (i >= list.length) return $.Deferred().resolve();
const obj = list[i];
//自己不能给自己的应援团应援
if(obj.owner_uid==Info.uid) return GroupSign.signInList(list, i + 1);
return API.Group.sign_in(obj.group_id, obj.owner_uid).then((response) => {
DEBUG('GroupSign.signInList: API.Group.sign_in', response);
const p = $.Deferred();
if (response.code === 0) {
if (response.data.add_num > 0) {
window.toast(`[自动应援团签到]应援团(group_id=${obj.group_id},owner_uid=${obj.owner_uid})签到成功,当前勋章亲密度+${response.data.add_num}`, 'success');
p.resolve();
} else if (response.data.status === 1) {
p.resolve();
} else {
p.reject();
}
} else {
window.toast(`[自动应援团签到]'${response.msg}`, 'caution');
return GroupSign.signInList(list, i);
}
return $.when(GroupSign.signInList(list, i + 1), p);
}, () => {
window.toast(`[自动应援团签到]应援团(group_id=${obj.group_id},owner_uid=${obj.owner_uid})签到失败,请检查网络`, 'error');
return delayCall(() => GroupSign.signInList(list, i));
});
},
run: () => {
try {
if (!CONFIG.AUTO_GROUP_SIGN) return $.Deferred().resolve();
if (CACHE.group_sign_ts && !checkNewDay(CACHE.group_sign_ts)) {
// 同一天,不再检查应援团签到
runTomorrow(GroupSign.run);
return $.Deferred().resolve();
}
return GroupSign.getGroups().then((list) => {
return GroupSign.signInList(list).then(() => {
CACHE.group_sign_ts = ts_ms();
up();
runTomorrow(GroupSign.run);
}, () => delayCall(() => GroupSign.run()));
}, () => delayCall(() => GroupSign.run()));
} catch (err) {
window.toast('[自动应援团签到]运行时出现异常,已停止', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
}
}; // Once Run every day "api.live.bilibili.com"
const DailyReward = {
coin_exp: 0,
login: () => {
return API.DailyReward.login().then(() => {
DEBUG('DailyReward.login: API.DailyReward.login');
window.toast('[自动每日奖励][每日登录]完成', 'success');
}, () => {
window.toast('[自动每日奖励][每日登录]完成失败,请检查网络', 'error');
return delayCall(() => DailyReward.login());
});
},
watch: (aid, cid) => {
if (!CONFIG.AUTO_DAILYREWARD_CONFIG.WATCH) return $.Deferred().resolve();
return API.DailyReward.watch(aid, cid, Info.uid, ts_s()).then((response) => {
DEBUG('DailyReward.watch: API.DailyReward.watch', response);
if (response.code === 0) {
window.toast(`[自动每日奖励][每日观看]完成(av=${aid})`, 'success');
} else {
window.toast(`[自动每日奖励][每日观看]'${response.msg}`, 'caution');
}
}, () => {
window.toast('[自动每日奖励][每日观看]完成失败,请检查网络', 'error');
return delayCall(() => DailyReward.watch(aid, cid));
});
},
coin: (cards, n, i = 0, one = false) => {
if (!CONFIG.AUTO_DAILYREWARD_CONFIG.COIN) return $.Deferred().resolve();
if (DailyReward.coin_exp >= CONFIG.AUTO_DAILYREWARD_CONFIG.COIN_CONFIG.NUMBER * 10) {
window.toast('[自动每日奖励][每日投币]今日投币已完成', 'info');
return $.Deferred().resolve();
}
if (i >= cards.length) {
window.toast('[自动每日奖励][每日投币]动态里可投币的视频不足', 'caution');
return $.Deferred().resolve();
}
const obj = JSON.parse(cards[i].card);
let num = Math.min(2, n);
if (one) num = 1;
return API.DailyReward.coin(obj.aid, num).then((response) => {
DEBUG('DailyReward.coin: API.DailyReward.coin', response);
if (response.code === 0) {
DailyReward.coin_exp += num * 10;
window.toast(`[自动每日奖励][每日投币]投币成功(av=${obj.aid},num=${num})`, 'success');
return DailyReward.coin(cards, n - num, i + 1);
} else if (response.code === -110) {
window.toast('[自动每日奖励][每日投币]未绑定手机,已停止', 'error');
return $.Deferred().reject();
} else if (response.code === 34003) {
// 非法的投币数量
if (one) return DailyReward.coin(cards, n, i + 1);
return DailyReward.coin(cards, n, i, true);
} else if (response.code === 34005) {
// 塞满啦!先看看库存吧~
return DailyReward.coin(cards, n, i + 1);
}
window.toast(`[自动每日奖励][每日投币]'${response.msg}`, 'caution');
return DailyReward.coin(cards, n, i + 1);
}, () => delayCall(() => DailyReward.coin(cards, n, i)));
},
share: (aid) => {
if (!CONFIG.AUTO_DAILYREWARD_CONFIG.SHARE) return $.Deferred().resolve();
return API.DailyReward.share(aid).then((response) => {
DEBUG('DailyReward.share: API.DailyReward.share', response);
if (response.code === 0) {
window.toast(`[自动每日奖励][每日分享]分享成功(av=${aid})`, 'success');
} else if (response.code === 71000) {
// 重复分享
window.toast('[自动每日奖励][每日分享]今日分享已完成', 'info');
} else {
window.toast(`[自动每日奖励][每日分享]'${response.msg}`, 'caution');
}
}, () => {
window.toast('[自动每日奖励][每日分享]分享失败,请检查网络', 'error');
return delayCall(() => DailyReward.share(aid));
});
},
dynamic: () => {
return API.dynamic_svr.dynamic_new(Info.uid, 8).then((response) => {
DEBUG('DailyReward.dynamic: API.dynamic_svr.dynamic_new', response);
if (response.code === 0) {
if (response.data.cards[0]) {
const obj = JSON.parse(response.data.cards[0].card);
const p1 = DailyReward.watch(obj.aid, obj.cid);
const p2 = DailyReward.coin(response.data.cards, Math.max(CONFIG.AUTO_DAILYREWARD_CONFIG.COIN_CONFIG.NUMBER - DailyReward.coin_exp / 10, 0));
const p3 = DailyReward.share(obj.aid);
return $.when(p1, p2, p3);
} else {
window.toast('[自动每日奖励]"动态-投稿视频"中暂无动态', 'info');
}
} else {
window.toast(`[自动每日奖励]获取"动态-投稿视频"'${response.msg}`, 'caution');
}
}, () => {
window.toast('[自动每日奖励]获取"动态-投稿视频"失败,请检查网络', 'error');
return delayCall(() => DailyReward.dynamic());
});
},
run: () => {
try {
if (!CONFIG.AUTO_DAILYREWARD) return $.Deferred().resolve();
if (CACHE.dailyreward_ts && !checkNewDay(CACHE.dailyreward_ts)) {
// 同一天,不执行每日任务
runTomorrow(DailyReward.run);
return $.Deferred().resolve();
}
return API.DailyReward.exp().then((response) => {
DEBUG('DailyReward.run: API.DailyReward.exp', response);
if (response.code === 0) {
DailyReward.coin_exp = response.number;
DailyReward.login();
return DailyReward.dynamic().then(() => {
CACHE.dailyreward_ts = ts_ms();
up();
runTomorrow(DailyReward.run);
});
} else {
window.toast(`[自动每日奖励]${response.message}`, 'caution');
}
}, () => {
window.toast('[自动每日奖励]获取每日奖励信息失败,请检查网络', 'error');
return delayCall(() => DailyReward.run());
});
} catch (err) {
window.toast('[自动每日奖励]运行时出现异常', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
}
}; // Once Run every day "api.live.bilibili.com"
if (CONFIG.AUTO_GROUP_SIGN) GroupSign.run();
if (CONFIG.AUTO_DAILYREWARD) DailyReward.run();
} else {
window.frameElement[NAME].promise.finish.resolve();
}
} catch (err) {
console.error(`[${NAME}]子脚本运行时出现异常,已停止并关闭子页面`);
console.error(`[${NAME}]`, err);
window.frameElement[NAME].promise.finish.resolve();
}
} else {
const runUntilSucceed = (callback, delay = 0, period = 100) => {
setTimeout(() => {
if (!callback()) runUntilSucceed(callback, period, period);
}, delay);
};
const addCSS = (context) => {
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = context;
document.getElementsByTagName('head')[0].appendChild(style);
};
const Essential = {
init: () => {
return Essential.Toast.init().then(() => {
return Essential.AlertDialog.init().then(() => {
return Essential.Config.init().then(() => {
Essential.DataSync.init();
Essential.Cache.load();
Essential.Config.load();
});
});
});
},
Toast: {
init: () => {
try {
const list = [];
window.toast = (msg, type = 'info', timeout = 3e3) => {
let d = new Date().toLocaleTimeString();
switch (type) {
case 'success':
case 'info':
console.info(`[${NAME}][${d}]${msg}`);
break;
case 'caution':
console.warn(`[${NAME}][${d}]${msg}`);
break;
case 'error':
console.error(`[${NAME}][${d}]${msg}`);
break;
default:
type = 'info';
console.log(`[${NAME}][${d}]${msg}`);
}
if (CONFIG && !CONFIG.SHOW_TOAST) return;
const a = $(`<div class="link-toast ${type} fixed"><span class="toast-text">${msg}</span></div>`)[0];
document.body.appendChild(a);
a.style.top = (document.body.scrollTop + list.length * 40 + 10) + 'px';
a.style.left = (document.body.offsetWidth + document.body.scrollLeft - a.offsetWidth - 5) + 'px';
list.push(a);
setTimeout(() => {
a.className += ' out';
setTimeout(() => {
list.shift();
list.forEach((v) => {
v.style.top = (parseInt(v.style.top, 10) - 40) + 'px';
});
$(a).remove();
}, 200);
}, timeout);
};
return $.Deferred().resolve();
} catch (err) {
console.error(`[${NAME}]初始化浮动提示时出现异常`);
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
}
}, // Need Init
AlertDialog: {
init: () => {
try {
const div_background = $(`<div id="${NAME}_alertdialog"/>`);
div_background[0].style = 'display: table;position: fixed;height: 100%;width: 100%;top: 0;left: 0;font-size: 12px;z-index: 10000;background-color: rgba(0,0,0,.5);';
const div_position = $('<div/>');
div_position[0].style = 'display: table-cell;vertical-align: middle;';
const div_style = $('<div/>');
div_style[0].style = 'position: relative;top: 50%;width: 40%;padding: 16px;border-radius: 5px;background-color: #fff;margin: 0 auto;';
div_position.append(div_style);
div_background.append(div_position);
const div_title = $('<div/>');
div_title[0].style = 'position: relative;padding-bottom: 12px;';
const div_title_span = $('<span>提示</span>');
div_title_span[0].style = 'margin: 0;color: #23ade5;font-size: 16px;';
div_title.append(div_title_span);
div_style.append(div_title);
const div_content = $('<div/>');
div_content[0].style = 'display: inline-block;vertical-align: top;font-size: 14px;';
div_style.append(div_content);
const div_button = $('<div/>');
div_button[0].style = 'position: relative;height: 32px;margin-top: 12px;';
div_style.append(div_button);
const button_ok = $('<button><span>确定</span></button>');
button_ok[0].style = 'position: absolute;height: 100%;min-width: 68px;right: 0;background-color: #23ade5;color: #fff;border-radius: 4px;font-size: 14px;border: 0;cursor: pointer;';
div_button.append(button_ok);
window.alertdialog = (title, content) => {
div_title_span.html(title);
div_content.html(content);
button_ok.click(() => {
$(`#${NAME}_alertdialog`).remove();
});
$('body > .link-popup-ctnr').first().append(div_background);
};
return $.Deferred().resolve();
} catch (err) {
window.toast('初始化帮助界面时出现异常', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
}
}, // Need Init After Toast.init
Config: {
CONFIG_DEFAULT: {
AUTO_SIGN: true,
AUTO_TREASUREBOX: true,
AUTO_GROUP_SIGN: true,
MOBILE_HEARTBEAT: true,
AUTO_LOTTERY: false,
AUTO_LOTTERY_CONFIG: {
GIFT_LOTTERY: true,
GIFT_LOTTERY_CONFIG: {
REFRESH_INTERVAL: 0
},
GUARD_AWARD: true,
GUARD_AWARD_CONFIG: {
LISTEN_NUMBER: 1,
CHANGE_ROOM_INTERVAL: 60
},
MATERIAL_OBJECT_LOTTERY: true,
MATERIAL_OBJECT_LOTTERY_CONFIG: {
CHECK_INTERVAL: 10,
IGNORE_QUESTIONABLE_LOTTERY: true
},
HIDE_POPUP: true
},
AUTO_TASK: true,
AUTO_GIFT: false,
AUTO_GIFT_CONFIG: {
ROOMID: 0,
GIFT_DEFAULT: [1],
GIFT_ALLOWED: [1],
SEND_TODAY: false,
IGNORE_FEED: false
},
SILVER2COIN: false,
AUTO_DAILYREWARD: true,
AUTO_DAILYREWARD_CONFIG: {
LOGIN: true,
WATCH: true,
COIN: false,
COIN_CONFIG: {
NUMBER: 5
},
SHARE: true
},
SHOW_TOAST: true
},
NAME: {
AUTO_SIGN: '自动签到',
AUTO_TREASUREBOX: '自动领取银瓜子',
AUTO_GROUP_SIGN: '自动应援团签到',
MOBILE_HEARTBEAT: '移动端心跳',
AUTO_LOTTERY: '自动抽奖',
AUTO_LOTTERY_CONFIG: {
GIFT_LOTTERY: '礼物抽奖',
GIFT_LOTTERY_CONFIG: {
REFRESH_INTERVAL: '刷新间隔'
},
GUARD_AWARD: '舰队领奖',
GUARD_AWARD_CONFIG: {
LISTEN_NUMBER: '监听倍数',
CHANGE_ROOM_INTERVAL: '换房间隔'
},
MATERIAL_OBJECT_LOTTERY: '实物抽奖',
MATERIAL_OBJECT_LOTTERY_CONFIG: {
CHECK_INTERVAL: '检查间隔',
IGNORE_QUESTIONABLE_LOTTERY: '忽略存疑的抽奖'
},
HIDE_POPUP: '隐藏抽奖提示框'
},
AUTO_TASK: '自动完成任务',
AUTO_GIFT: '自动送礼物',
AUTO_GIFT_CONFIG: {
ROOMID: '房间号',
GIFT_DEFAULT: '默认礼物类型',
GIFT_ALLOWED: '允许礼物类型',
SEND_TODAY: '送出包裹中今天到期的礼物',
IGNORE_FEED: '忽略今日亲密度上限'
},
SILVER2COIN: '银瓜子换硬币',
AUTO_DAILYREWARD: '自动每日奖励',
AUTO_DAILYREWARD_CONFIG: {
LOGIN: '登录',
WATCH: '观看',
COIN: '投币',
COIN_CONFIG: {
NUMBER: '数量'
},
SHARE: '分享'
},
SHOW_TOAST: '显示浮动提示'
},
PLACEHOLDER: {
AUTO_LOTTERY_CONFIG: {
GIFT_LOTTERY_CONFIG: {
REFRESH_INTERVAL: '单位(分钟)'
},
GUARD_AWARD_CONFIG: {
LISTEN_NUMBER: '1~5,默认1',
CHANGE_ROOM_INTERVAL: '单位(分钟)'
},
MATERIAL_OBJECT_LOTTERY_CONFIG: {
CHECK_INTERVAL: '单位(分钟)'
}
},
AUTO_GIFT_CONFIG: {
ROOMID: '为0不送礼',
GIFT_DEFAULT: '为空默认不送',
GIFT_ALLOWED: '为空允许所有'
}
},
HELP: {
MOBILE_HEARTBEAT: '发送移动端心跳数据包,可以完成双端观看任务',
AUTO_LOTTERY: '设置是否自动参加抽奖功能,包括礼物抽奖、活动抽奖、实物抽奖<br>会占用更多资源并可能导致卡顿,且有封号风险',
AUTO_LOTTERY_CONFIG: {
GIFT_LOTTERY: '包括小电视、摩天大楼、C位光环及其他可以通过送礼触发广播的抽奖<br>内置几秒钟的延迟',
GIFT_LOTTERY_CONFIG: {
REFRESH_INTERVAL: '设置页面自动刷新的时间间隔,设置为0则不启用,单位为分钟<br>太久导致页面崩溃将无法正常运行脚本'
},
GUARD_AWARD_CONFIG: {
LISTEN_NUMBER: '设置在各大分区中的每一个分区监听的直播间的数量,1~5之间的一个整数<br>可能导致占用大量内存或导致卡顿',
CHANGE_ROOM_INTERVAL: '设置在多久之后改变监听的房间,单位为分钟,0表示不改变'
},
MATERIAL_OBJECT_LOTTERY: '部分房间设有实物奖励抽奖,脚本使用穷举的方式检查是否有实物抽奖<br>请注意中奖后记得及时填写相关信息领取实物奖励',
MATERIAL_OBJECT_LOTTERY_CONFIG: {
CHECK_INTERVAL: '每次穷举实物抽奖活动ID的时间间隔,单位为分钟',
IGNORE_QUESTIONABLE_LOTTERY: '对部分实物抽奖的标题存在疑问,勾选后不参加这部分抽奖'
},
HIDE_POPUP: '隐藏位于聊天框下方的抽奖提示框<br>注意:脚本参加抽奖后,部分抽奖仍然可以手动点击参加,为避免小黑屋,不建议点击'
},
AUTO_GIFT_CONFIG: {
ROOMID: '送礼物的直播间ID(即地址中live.bilibili.com/后面的数字), 设置为0则不送礼,小于0也视为0<br>只有在当前直播间和设置的直播间相同时才会送礼',
GIFT_DEFAULT: () => (`设置默认送的礼物类型编号,多个请用英文逗号(,)隔开,为空则表示默认不送出礼物<br>${Info.gift_list_str}`),
GIFT_ALLOWED: () => (`设置允许送的礼物类型编号(任何未在此列表的礼物一定不会被送出!),多个请用英文逗号(,)隔开,为空则表示允许送出所有类型的礼物<br><br>${Info.gift_list_str}`),
SEND_TODAY: '送出包裹中今天到期的礼物(会送出"默认礼物类型"之外的礼物,若今日亲密度已满则不送)'
},
SILVER2COIN: '用银瓜子兑换硬币,每天只能兑换一次<br>700银瓜子兑换1个硬币',
AUTO_DAILYREWARD: '自动完成每日经验的任务',
AUTO_DAILYREWARD_CONFIG: {
LOGIN: '自动完成登录任务(凌晨的时候不一定能完成)',
WATCH: '自动完成观看任务(凌晨的时候不一定能完成)',
COIN: '对你关注的动态中最新几期的视频投币,直到投完设定的数量',
SHARE: '自动分享你关注的动态中最新一期的视频(可以完成任务,但实际上不会出现这条动态)'
},
SHOW_TOAST: '选择是否显示浮动提示,但提示信息依旧会在控制台显示'
},
showed: false,
init: () => {
try {
const p = $.Deferred();
const getConst = (itemname, obj) => {
if (itemname.indexOf('-') > -1) {
const objname = itemname.match(/(.+?)-/)[1];
if (objname && obj[objname]) return getConst(itemname.replace(`${objname}-`, ''), obj[objname]);
else return undefined;
}
if (typeof obj[itemname] === 'function') return obj[itemname]();
return obj[itemname];
};
const recur = (cfg, element, parentname = undefined) => {
for (const item in cfg) {
let itemname;
if (parentname) itemname = `${parentname}-${item}`;
else itemname = item;
const id = `${NAME}_config_${itemname}`;
const name = getConst(itemname, Essential.Config.NAME);
const placeholder = getConst(itemname, Essential.Config.PLACEHOLDER);
let e;
let h;
if (getConst(itemname, Essential.Config.HELP)) h = $(`<div class="${NAME}_help" id="${id}_help" style="display: inline;"><span class="${NAME}_clickable">?</span></div>`);
switch ($.type(cfg[item])) {
case 'number':
case 'string':
e = $(`<div class="${NAME}_setting_item"></div>`);
e.html(`<label style="display: inline;" title="${name}">${name}<input id="${id}" type="text" class="${NAME}_input_text" placeholder="${placeholder}"></label>`);
if (h) e.append(h);
element.append(e);
break;
case 'boolean':
e = $(`<div class="${NAME}_setting_item"></div>`);
e.html(`<label style="display: inline;" title="${name}"><input id="${id}" type="checkbox" class="${NAME}_input_checkbox">${name}</label>`);
if (h) e.append(h);
element.append(e);
if (getConst(`${itemname}_CONFIG`, Essential.Config.NAME)) $(`#${id}`).addClass(`${NAME}_control`);
break;
case 'array':
e = $(`<div class="${NAME}_setting_item"></div>`);
e.html(`<label style="display: inline;" title="${name}">${name}<input id="${id}" type="text" class="${NAME}_input_text" placeholder="${placeholder}"></label>`);
if (h) e.append(h);
element.append(e);
break;
case 'object':
e = $(`<div id="${id}" style="margin: 0px 0px 8px 12px;"/>`);
element.append(e);
recur(cfg[item], e, itemname);
break;
}
}
};
runUntilSucceed(() => {
try {
if (!$('#sidebar-vm div.side-bar-cntr')[0]) return false;
// 加载css
addCSS(`.${NAME}_clickable {font-size: 12px;color: #0080c6;cursor: pointer;text-decoration: underline;}
.${NAME}_setting_item {margin: 6px 0px;}
.${NAME}_input_checkbox {vertical-align: bottom;}
.${NAME}_input_text {margin: -2px 0 -2px 4px;padding: 0;}`);
// 绘制右下角按钮
const div_button_span = $('<span>挂机助手设置</span>');
div_button_span[0].style = 'font-size: 12px;line-height: 16px;color: #0080c6;';
const div_button = $('<div/>');
div_button[0].style = 'cursor: pointer;text-align: center;padding: 0px;';
const div_side_bar = $('<div/>');
div_side_bar[0].style = 'width: 56px;height: 32px;overflow: hidden;position: fixed;right: 0px;bottom: 10%;padding: 4px 4px;background-color: rgb(255, 255, 255);z-index: 10001;border-radius: 8px 0px 0px 8px;box-shadow: rgba(0, 85, 255, 0.0980392) 0px 0px 20px 0px;border: 1px solid rgb(233, 234, 236);';
div_button.append(div_button_span);
div_side_bar.append(div_button);
$('#sidebar-vm div.side-bar-cntr').first().after(div_side_bar);
// 绘制设置界面
const div_position = $('<div/>');
div_position[0].style = 'display: none;position: fixed;height: 300px;width: 300px;bottom: 5%;z-index: 9999;';
const div_style = $('<div/>');
div_style[0].style = 'display: block;overflow: hidden;height: 300px;width: 300px;border-radius: 8px;box-shadow: rgba(106, 115, 133, 0.219608) 0px 6px 12px 0px;border: 1px solid rgb(233, 234, 236);background-color: rgb(255, 255, 255);';
div_position.append(div_style);
document.body.appendChild(div_position[0]);
// 绘制标题栏及按钮
const div_title = $('<div/>');
div_title[0].style = 'display: block;border-bottom: 1px solid #E6E6E6;height: 35px;line-height: 35px;margin: 0;padding: 0;overflow: hidden;';
const div_title_span = $('<span style="float: left;display: inline;padding-left: 8px;font: 700 14px/35px SimSun;">Bilibili直播间挂机助手</span>');
const div_title_button = $('<div/>');
div_title_button[0].style = 'float: right;display: inline;padding-right: 8px;';
const div_button_clear = $(`<div style="display: inline;"><span class="${NAME}_clickable">清除缓存</span></div>`);
div_title_button.append(div_button_clear);
div_title.append(div_title_span);
div_title.append(div_title_button);
div_style.append(div_title);
// 绘制设置项内容
const div_context_position = $('<div/>');
div_context_position[0].style = 'display: block;position: absolute;top: 36px;width: 100%;height: calc(100% - 36px);';
const div_context = $('<div/>');
div_context[0].style = 'height: 100%;overflow: auto;padding: 0 12px;margin: 0px;';
div_context_position.append(div_context);
div_style.append(div_context_position);
recur(Essential.Config.CONFIG_DEFAULT, div_context);
// 设置事件
div_button.click(() => {
if (!Essential.Config.showed) {
Essential.Config.load();
div_position.css('right', div_side_bar[0].clientWidth + 'px');
div_position.show();
div_button_span.text('点击保存设置');
div_button_span.css('color', '#ff8e29');
} else {
Essential.Config.save();
div_position.hide();
div_button_span.text('挂机助手设置');
div_button_span.css('color', '#0080c6');
}
Essential.Config.showed = !Essential.Config.showed;
});
div_button_clear.click(() => {
Essential.Cache.clear();
location.reload();
});
const getItemByElement = (element) => element.id.replace(`${NAME}_config_`, '');
const getItemByHelpElement = (element) => element.id.replace(`${NAME}_config_`, '').replace('_help', '');
$(`.${NAME}_help`).click(function () {
window.alertdialog('说明', getConst(getItemByHelpElement(this), Essential.Config.HELP));
});
$(`.${NAME}_control`).click(function () {
if ($(this).is(':checked')) {
$(`#${NAME}_config_${getItemByElement(this)}_CONFIG`).show();
} else {
$(`#${NAME}_config_${getItemByElement(this)}_CONFIG`).hide();
}
});
p.resolve();
return true;
} catch (err) {
window.toast('初始化设置界面时出现异常', 'error');
console.error(`[${NAME}]`, err);
p.reject();
return true;
}
});
return p;
} catch (err) {
window.toast('初始化设置时出现异常', 'error');
console.error(`[${NAME}]`, err);
return $.Deferred().reject();
}
},
recurLoad: (cfg, parentname = undefined, cfg_default = Essential.Config.CONFIG_DEFAULT) => {
for (const item in cfg_default) {
let itemname;
if (parentname) itemname = `${parentname}-${item}`;
else itemname = item;
const e = $(`#${NAME}_config_${itemname}`);