-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfishpi-auto-red-packet.js
More file actions
1961 lines (1714 loc) · 67.8 KB
/
fishpi-auto-red-packet.js
File metadata and controls
1961 lines (1714 loc) · 67.8 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 FishPi 聊天室自动抢红包
// @namespace https://fishpi.cn/
// @version 0.4.4
// @description FishPi 聊天室自动抢红包脚本,支持经典/简约样式、悬浮拖拽设置面板、红包统计、自定义官方感谢文案与频控
// @author FishPi Offical
// @match https://fishpi.cn/cr*
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @run-at document-end
// @noframes
// ==/UserScript==
(function () {
'use strict';
if (window.location.pathname !== '/cr') {
return;
}
const pageWindow = resolvePageWindow();
const STORAGE_KEY = 'fishpi-auto-red-packet-settings-v1';
const STATS_STORAGE_KEY = 'fishpi-auto-red-packet-stats-v1';
const PANEL_ID = 'arp-panel';
const PANEL_BODY_ID = 'arp-panel-body';
const LAUNCHER_ID = 'arp-launcher';
const PAGE_CONTEXT_EVENT = 'arp:page-context';
const PAGE_MESSAGE_EVENT = 'arp:page-message';
const PAGE_CONTEXT_REQUEST_EVENT = 'arp:page-context-request';
const PAGE_BRIDGE_SCRIPT_ID = 'arp-page-bridge';
const DEFAULT_THANK_TEMPLATE = '我通过官方抢红包扩展抢到了{points}积分,谢谢老板~';
const OFFICIAL_EXTENSION_QUOTE = '> 来自官方抢红包扩展,下载地址:https://ext.adventext.fun/market';
const DAY_MS = 24 * 60 * 60 * 1000;
const DEFAULT_SETTINGS = {
enabled: true,
delaySeconds: 10,
autoReplyEnabled: true,
thankTemplate: DEFAULT_THANK_TEMPLATE,
thankLimitEnabled: true,
thankLimitWindowMinutes: 60,
thankLimitMaxPerUser: 1,
panelOpen: false,
panelPosition: null,
launcherPosition: null,
types: {
random: true,
average: true,
specify: true,
heartbeat: false,
rockPaperScissors: false
},
rockPaperScissors: {
randomGesture: true,
fixedGesture: '0'
}
};
const state = {
settings: loadSettings(),
stats: loadStats(),
currentUserId: '',
currentUserName: '',
servePath: '',
scheduled: new Map(),
messageCache: new Map(),
manualHandled: new Set(),
statusText: '等待初始化',
lastGrabText: '暂无',
renderPatched: false,
readyTimer: null,
initialScanDone: false,
servePathResolved: false,
thisClient: '',
bridgeListenersBound: false,
chatObserver: null,
chatObserverBootstrapper: null,
rescanTimer: null,
dragState: null,
launcherDragState: null,
launcherClickSuppress: false,
uiMounted: false
};
function resolvePageWindow() {
if (typeof unsafeWindow !== 'undefined') {
return unsafeWindow;
}
if (typeof window.wrappedJSObject !== 'undefined') {
return window.wrappedJSObject;
}
return window;
}
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
function decodeInlineString(value) {
return String(value || '')
.replace(/\\\\/g, '\\')
.replace(/\\'/g, '\'')
.replace(/\\"/g, '"');
}
function extractInlineScriptValue(pattern) {
const scripts = document.scripts || [];
for (const script of scripts) {
if (script.src) {
continue;
}
const match = (script.textContent || '').match(pattern);
if (match && match[1] !== undefined) {
return decodeInlineString(match[1]);
}
}
return null;
}
function syncContextFromLabel(label) {
if (!label || typeof label !== 'object') {
return;
}
if (Object.prototype.hasOwnProperty.call(label, 'servePath')) {
state.servePath = String(label.servePath || '');
state.servePathResolved = true;
}
const currentUserName = label.currentUser || label.currentUserName;
if (currentUserName) {
state.currentUserName = String(currentUserName);
}
if (label.currentUserId) {
state.currentUserId = String(label.currentUserId);
}
if (pageWindow.thisClient) {
state.thisClient = String(pageWindow.thisClient);
}
}
function hydrateContextFromInlineScripts() {
if (!state.servePathResolved) {
const servePath = extractInlineScriptValue(/servePath\s*:\s*["']([^"']*)["']/);
if (servePath !== null) {
state.servePath = servePath;
state.servePathResolved = true;
}
}
if (!state.currentUserName) {
const currentUserName = extractInlineScriptValue(/Label\.currentUser\s*=\s*'([^']*)'/)
|| extractInlineScriptValue(/currentUserName\s*:\s*'([^']*)'/);
if (currentUserName) {
state.currentUserName = currentUserName;
}
}
if (!state.currentUserId) {
const currentUserId = extractInlineScriptValue(/Label\.currentUserId\s*=\s*'([^']*)'/);
if (currentUserId) {
state.currentUserId = currentUserId;
}
}
}
function parseJSONSafe(text) {
if (!text || typeof text !== 'string') {
return null;
}
try {
return JSON.parse(text);
} catch (error) {
return null;
}
}
function buildUrl(path) {
const base = String(state.servePath || '');
const targetPath = String(path || '');
if (!base) {
return targetPath.startsWith('/') ? targetPath : `/${targetPath}`;
}
if (!targetPath) {
return base;
}
if (base.endsWith('/') && targetPath.startsWith('/')) {
return `${base.slice(0, -1)}${targetPath}`;
}
if (!base.endsWith('/') && !targetPath.startsWith('/')) {
return `${base}/${targetPath}`;
}
return `${base}${targetPath}`;
}
function mergeSettings(raw) {
const merged = clone(DEFAULT_SETTINGS);
if (!raw || typeof raw !== 'object') {
return merged;
}
merged.enabled = raw.enabled !== undefined ? !!raw.enabled : merged.enabled;
merged.delaySeconds = normalizeDelay(raw.delaySeconds);
merged.autoReplyEnabled = raw.autoReplyEnabled !== undefined ? !!raw.autoReplyEnabled : merged.autoReplyEnabled;
merged.thankTemplate = normalizeThankTemplate(raw.thankTemplate);
merged.thankLimitEnabled = raw.thankLimitEnabled !== undefined ? !!raw.thankLimitEnabled : merged.thankLimitEnabled;
merged.thankLimitWindowMinutes = normalizeThankLimitWindow(raw.thankLimitWindowMinutes);
merged.thankLimitMaxPerUser = normalizeThankLimitMax(raw.thankLimitMaxPerUser);
merged.panelOpen = raw.panelOpen !== undefined ? !!raw.panelOpen : merged.panelOpen;
merged.panelPosition = raw.panelPosition && typeof raw.panelPosition === 'object'
? {
left: Number(raw.panelPosition.left),
top: Number(raw.panelPosition.top)
}
: null;
merged.launcherPosition = raw.launcherPosition && typeof raw.launcherPosition === 'object'
? {
left: Number(raw.launcherPosition.left),
top: Number(raw.launcherPosition.top)
}
: null;
merged.types = Object.assign({}, merged.types, raw.types || {});
merged.types.random = !!merged.types.random;
merged.types.average = !!merged.types.average;
merged.types.specify = !!merged.types.specify;
merged.types.heartbeat = !!merged.types.heartbeat;
merged.types.rockPaperScissors = !!merged.types.rockPaperScissors;
merged.rockPaperScissors = Object.assign({}, merged.rockPaperScissors, raw.rockPaperScissors || {});
merged.rockPaperScissors.randomGesture = !!merged.rockPaperScissors.randomGesture;
merged.rockPaperScissors.fixedGesture = ['0', '1', '2'].includes(String(merged.rockPaperScissors.fixedGesture))
? String(merged.rockPaperScissors.fixedGesture)
: '0';
return merged;
}
function storageGet(key, fallback) {
const storageKey = typeof key === 'string' ? key : STORAGE_KEY;
const defaultValue = arguments.length > 1 ? fallback : null;
try {
if (typeof GM_getValue === 'function') {
return GM_getValue(storageKey, defaultValue);
}
} catch (error) {
console.warn('[ARP] GM_getValue 读取失败', error);
}
try {
const raw = window.localStorage.getItem(storageKey);
return raw ? JSON.parse(raw) : defaultValue;
} catch (error) {
console.warn('[ARP] localStorage 读取失败', error);
return defaultValue;
}
}
function storageSet(key, value) {
const storageKey = value === undefined ? STORAGE_KEY : key;
const data = value === undefined ? key : value;
try {
if (typeof GM_setValue === 'function') {
GM_setValue(storageKey, data);
return;
}
} catch (error) {
console.warn('[ARP] GM_setValue 写入失败', error);
}
try {
window.localStorage.setItem(storageKey, JSON.stringify(data));
} catch (error) {
console.warn('[ARP] localStorage 写入失败', error);
}
}
function loadSettings() {
return mergeSettings(storageGet(STORAGE_KEY, null));
}
function saveSettings() {
storageSet(STORAGE_KEY, state.settings);
}
function normalizeHistoryItem(item) {
if (!item || typeof item !== 'object' || !item.oId) {
return null;
}
return {
oId: String(item.oId),
time: Number(item.time) || 0,
points: Number(item.points) || 0,
senderId: String(item.senderId || ''),
senderName: String(item.senderName || ''),
type: String(item.type || '')
};
}
function normalizeThankItem(item) {
if (!item || typeof item !== 'object') {
return null;
}
return {
oId: String(item.oId || ''),
time: Number(item.time) || 0,
senderId: String(item.senderId || ''),
senderName: String(item.senderName || '')
};
}
function mergeStats(raw) {
const merged = {
totalPoints: 0,
totalCount: 0,
history: [],
thanksHistory: []
};
if (!raw || typeof raw !== 'object') {
return merged;
}
merged.totalPoints = Number.isFinite(Number(raw.totalPoints)) ? Number(raw.totalPoints) : 0;
merged.totalCount = Number.isFinite(Number(raw.totalCount)) ? Math.max(0, Math.floor(Number(raw.totalCount))) : 0;
merged.history = Array.isArray(raw.history) ? raw.history.map(normalizeHistoryItem).filter(Boolean) : [];
merged.thanksHistory = Array.isArray(raw.thanksHistory) ? raw.thanksHistory.map(normalizeThankItem).filter(Boolean) : [];
return merged;
}
function loadStats() {
return mergeStats(storageGet(STATS_STORAGE_KEY, null));
}
function saveStats() {
pruneStats();
storageSet(STATS_STORAGE_KEY, state.stats);
}
function normalizeDelay(value) {
const delay = Number(value);
if (!Number.isFinite(delay)) {
return DEFAULT_SETTINGS.delaySeconds;
}
return Math.min(600, Math.max(3, Math.round(delay)));
}
function normalizeThankLimitWindow(value) {
const minutes = Number(value);
if (!Number.isFinite(minutes)) {
return DEFAULT_SETTINGS.thankLimitWindowMinutes;
}
return Math.min(1440, Math.max(1, Math.round(minutes)));
}
function normalizeThankLimitMax(value) {
const count = Number(value);
if (!Number.isFinite(count)) {
return DEFAULT_SETTINGS.thankLimitMaxPerUser;
}
return Math.min(20, Math.max(1, Math.round(count)));
}
function normalizeThankTemplate(value) {
const template = String(value == null ? '' : value).trim();
if (!template) {
return DEFAULT_THANK_TEMPLATE;
}
return template.slice(0, 300);
}
function notify(type, message) {
if (pageWindow.Util && typeof pageWindow.Util.notice === 'function') {
pageWindow.Util.notice(type, 3000, message);
return;
}
console.log(`[ARP:${type}] ${message}`);
}
function updateStatus(message, lastGrabText) {
state.statusText = message;
if (lastGrabText !== undefined) {
state.lastGrabText = lastGrabText;
}
renderPanelBody();
}
function ensureUserContext() {
syncContextFromLabel(pageWindow.Label || {});
hydrateContextFromInlineScripts();
return !!state.currentUserName;
}
function injectStyles() {
const css = `
#${LAUNCHER_ID} {
position: fixed;
right: 24px;
bottom: 112px;
z-index: 99999;
border: 0;
border-radius: 999px;
padding: 10px 14px;
background: linear-gradient(135deg, #ff7a45, #ff4d4f);
color: #fff;
box-shadow: 0 10px 28px rgba(255, 77, 79, 0.28);
cursor: grab;
touch-action: none;
font-size: 13px;
font-weight: 600;
}
#${LAUNCHER_ID}:hover {
transform: translateY(-1px);
}
#${LAUNCHER_ID}:active {
cursor: grabbing;
}
#${PANEL_ID} {
position: fixed;
right: 24px;
bottom: 164px;
width: 340px;
z-index: 100000;
border-radius: 14px;
overflow: hidden;
background: rgba(255, 255, 255, 0.97);
color: #222;
box-shadow: 0 20px 48px rgba(0, 0, 0, 0.18);
border: 1px solid rgba(0, 0, 0, 0.08);
backdrop-filter: blur(10px);
}
#${PANEL_ID}.arp-hidden {
display: none;
}
#${PANEL_ID} * {
box-sizing: border-box;
}
#${PANEL_ID} .arp-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 14px;
background: linear-gradient(135deg, #fff7e8, #ffe7ba);
cursor: move;
user-select: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}
#${PANEL_ID} .arp-title {
font-size: 14px;
font-weight: 700;
}
#${PANEL_ID} .arp-header-actions {
display: flex;
align-items: center;
gap: 8px;
}
#${PANEL_ID} .arp-header-actions button,
#${PANEL_ID} .arp-inline-button {
border: 0;
border-radius: 8px;
padding: 6px 10px;
cursor: pointer;
font-size: 12px;
background: #fff;
color: #333;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}
#${PANEL_ID} .arp-close {
width: 28px;
height: 28px;
padding: 0;
font-size: 16px;
line-height: 28px;
text-align: center;
}
#${PANEL_ID} .arp-body {
padding: 14px;
max-height: min(72vh, 720px);
overflow: auto;
}
#${PANEL_ID} .arp-card {
margin-bottom: 12px;
padding: 12px;
border-radius: 12px;
background: #fafafa;
border: 1px solid rgba(0, 0, 0, 0.05);
}
#${PANEL_ID} .arp-card:last-child {
margin-bottom: 0;
}
#${PANEL_ID} .arp-card-title {
margin-bottom: 10px;
font-size: 13px;
font-weight: 700;
color: #111;
}
#${PANEL_ID} .arp-status-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 8px;
}
#${PANEL_ID} .arp-status-item {
padding: 8px 10px;
border-radius: 10px;
background: #fff;
}
#${PANEL_ID} .arp-status-label {
display: block;
margin-bottom: 4px;
font-size: 11px;
color: #888;
}
#${PANEL_ID} .arp-status-value {
display: block;
font-size: 12px;
color: #222;
word-break: break-word;
}
#${PANEL_ID} .arp-row,
#${PANEL_ID} .arp-option {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
#${PANEL_ID} .arp-row + .arp-row,
#${PANEL_ID} .arp-option + .arp-option,
#${PANEL_ID} .arp-note + .arp-option,
#${PANEL_ID} .arp-option + .arp-note,
#${PANEL_ID} .arp-inline-grid + .arp-note,
#${PANEL_ID} .arp-option + .arp-inline-grid {
margin-top: 10px;
}
#${PANEL_ID} .arp-option--stack {
align-items: stretch;
flex-direction: column;
}
#${PANEL_ID} .arp-option span,
#${PANEL_ID} .arp-row span {
font-size: 13px;
color: #222;
}
#${PANEL_ID} .arp-sub {
display: block;
margin-top: 3px;
font-size: 11px;
color: #888;
}
#${PANEL_ID} input[type="checkbox"] {
width: 18px;
height: 18px;
accent-color: #ff7a45;
cursor: pointer;
flex-shrink: 0;
}
#${PANEL_ID} input[type="number"],
#${PANEL_ID} select {
width: 110px;
padding: 6px 8px;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 8px;
background: #fff;
color: #222;
}
#${PANEL_ID} textarea {
width: 100%;
min-height: 82px;
padding: 8px;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 8px;
background: #fff;
color: #222;
resize: vertical;
font: inherit;
}
#${PANEL_ID} .arp-inline-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
#${PANEL_ID} .arp-inline-grid input[type="number"] {
width: 100%;
}
#${PANEL_ID} .arp-note {
font-size: 11px;
line-height: 1.5;
color: #666;
}
#${PANEL_ID} .arp-danger {
color: #cf1322;
}
#${PANEL_ID} .arp-actions-row {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
@media (max-width: 768px) {
#${LAUNCHER_ID} {
right: 12px;
bottom: 96px;
}
#${PANEL_ID} {
width: min(92vw, 340px);
right: 12px;
bottom: 144px;
}
}
`;
if (typeof GM_addStyle === 'function') {
GM_addStyle(css);
return;
}
const style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
}
function createUI() {
if (state.uiMounted || !document.body) {
return;
}
injectStyles();
const launcher = document.createElement('button');
launcher.id = LAUNCHER_ID;
launcher.type = 'button';
launcher.textContent = '🧧 抢红包设置';
launcher.addEventListener('pointerdown', startLauncherDrag);
launcher.addEventListener('click', function (event) {
if (state.launcherClickSuppress) {
event.preventDefault();
event.stopPropagation();
state.launcherClickSuppress = false;
return;
}
togglePanel();
});
//document.body.appendChild(launcher);
if (state.settings.launcherPosition && Number.isFinite(state.settings.launcherPosition.left) && Number.isFinite(state.settings.launcherPosition.top)) {
applyLauncherPosition(state.settings.launcherPosition.left, state.settings.launcherPosition.top);
}
const panel = document.createElement('div');
panel.id = PANEL_ID;
panel.className = state.settings.panelOpen ? '' : 'arp-hidden';
panel.innerHTML = `
<div class="arp-header">
<div class="arp-title">🧧 自动抢红包设置</div>
<div class="arp-header-actions">
<button type="button" class="arp-inline-button" data-action="scan">立即扫描</button>
<button type="button" class="arp-close" data-action="close">×</button>
</div>
</div>
<div class="arp-body" id="${PANEL_BODY_ID}"></div>
`;
document.body.appendChild(panel);
panel.querySelector('.arp-header').addEventListener('pointerdown', startDrag);
panel.addEventListener('click', handlePanelAction);
if (state.settings.panelPosition && Number.isFinite(state.settings.panelPosition.left) && Number.isFinite(state.settings.panelPosition.top)) {
applyPanelPosition(state.settings.panelPosition.left, state.settings.panelPosition.top);
}
state.uiMounted = true;
renderPanelBody();
registerMenuCommands();
}
function registerMenuCommands() {
if (typeof GM_registerMenuCommand !== 'function') {
return;
}
try {
GM_registerMenuCommand('打开抢红包设置', function () {
openPanel();
});
GM_registerMenuCommand('开/关自动抢红包功能', function () {
state.settings.enabled = !state.settings.enabled;
saveSettings();
applySettingsChange();
});
} catch (error) {
console.warn('[ARP] 注册菜单失败', error);
}
}
function renderPanelBody() {
const body = document.getElementById(PANEL_BODY_ID);
if (!body) {
return;
}
const settings = state.settings;
const statsSummary = getStatsSummary();
body.innerHTML = `
<div class="arp-card">
<div class="arp-card-title">运行状态</div>
<div class="arp-status-grid">
<div class="arp-status-item">
<span class="arp-status-label">功能状态</span>
<span class="arp-status-value">${settings.enabled ? '运行中' : '已关闭'}</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">排队数量</span>
<span class="arp-status-value">${state.scheduled.size}</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">当前状态</span>
<span class="arp-status-value">${escapeHtml(state.statusText)}</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">最近一次</span>
<span class="arp-status-value">${escapeHtml(state.lastGrabText)}</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">近7天积分</span>
<span class="arp-status-value">${formatPoints(statsSummary.last7DaysPoints, true)} 积分</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">近7天次数</span>
<span class="arp-status-value">${statsSummary.last7DaysCount} 次</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">累计积分</span>
<span class="arp-status-value">${formatPoints(statsSummary.totalPoints, true)} 积分</span>
</div>
<div class="arp-status-item">
<span class="arp-status-label">累计次数</span>
<span class="arp-status-value">${statsSummary.totalCount} 次</span>
</div>
</div>
</div>
<div class="arp-card">
<div class="arp-card-title">基础设置</div>
<label class="arp-option">
<span>启用自动抢红包</span>
<input type="checkbox" data-setting="enabled" ${settings.enabled ? 'checked' : ''}>
</label>
<label class="arp-option">
<span>
抢红包延迟
<span class="arp-sub">单位:秒,最小 3 秒</span>
</span>
<input type="number" min="3" max="600" step="1" data-setting="delaySeconds" value="${settings.delaySeconds}">
</label>
</div>
<div class="arp-card">
<div class="arp-card-title">红包类型</div>
<label class="arp-option">
<span>拼手气红包</span>
<input type="checkbox" data-type="random" ${settings.types.random ? 'checked' : ''}>
</label>
<label class="arp-option">
<span>普通红包</span>
<input type="checkbox" data-type="average" ${settings.types.average ? 'checked' : ''}>
</label>
<label class="arp-option">
<span>
专属红包
<span class="arp-sub">仅在红包指定了你时自动抢</span>
</span>
<input type="checkbox" data-type="specify" ${settings.types.specify ? 'checked' : ''}>
</label>
<label class="arp-option">
<span>
心跳红包
<span class="arp-sub arp-danger">可能抢到负积分,默认关闭</span>
</span>
<input type="checkbox" data-type="heartbeat" ${settings.types.heartbeat ? 'checked' : ''}>
</label>
<label class="arp-option">
<span>
猜拳红包
<span class="arp-sub arp-danger">猜错会扣积分,默认关闭</span>
</span>
<input type="checkbox" data-type="rockPaperScissors" ${settings.types.rockPaperScissors ? 'checked' : ''}>
</label>
<div class="arp-row">
<span>
猜拳出拳策略
<span class="arp-sub">只在开启“猜拳红包”后生效</span>
</span>
<select data-setting="rpsMode">
<option value="random" ${settings.rockPaperScissors.randomGesture ? 'selected' : ''}>随机出拳</option>
<option value="fixed" ${settings.rockPaperScissors.randomGesture ? '' : 'selected'}>固定出拳</option>
</select>
</div>
<div class="arp-row">
<span>固定出拳</span>
<select data-setting="fixedGesture" ${settings.rockPaperScissors.randomGesture ? 'disabled' : ''}>
<option value="0" ${settings.rockPaperScissors.fixedGesture === '0' ? 'selected' : ''}>石头</option>
<option value="1" ${settings.rockPaperScissors.fixedGesture === '1' ? 'selected' : ''}>剪刀</option>
<option value="2" ${settings.rockPaperScissors.fixedGesture === '2' ? 'selected' : ''}>布</option>
</select>
</div>
</div>
<div class="arp-card">
<div class="arp-card-title">感谢文案</div>
<label class="arp-option">
<span>
抢到后自动致谢
<span class="arp-sub">仅在抢到≥256积分时发送</span>
</span>
<input type="checkbox" data-setting="autoReplyEnabled" ${settings.autoReplyEnabled ? 'checked' : ''}>
</label>
<label class="arp-option arp-option--stack">
<span>
自定义感谢文案
<span class="arp-sub">支持占位符:{points}、{user}</span>
</span>
<textarea data-setting="thankTemplate" rows="3">${escapeHtml(settings.thankTemplate)}</textarea>
</label>
<div class="arp-note">发送格式:普通文字感谢文案 + 空行 + Markdown 引用。固定引用内容:${escapeHtml(OFFICIAL_EXTENSION_QUOTE)}</div>
<label class="arp-option">
<span>
同一人感谢频控
<span class="arp-sub">限制每个人在一段时间内只感谢前几次</span>
</span>
<input type="checkbox" data-setting="thankLimitEnabled" ${settings.thankLimitEnabled ? 'checked' : ''}>
</label>
<div class="arp-inline-grid">
<label class="arp-option">
<span>窗口分钟数</span>
<input type="number" min="1" max="1440" step="1" data-setting="thankLimitWindowMinutes" value="${settings.thankLimitWindowMinutes}" ${settings.thankLimitEnabled ? '' : 'disabled'}>
</label>
<label class="arp-option">
<span>每人前几次</span>
<input type="number" min="1" max="20" step="1" data-setting="thankLimitMaxPerUser" value="${settings.thankLimitMaxPerUser}" ${settings.thankLimitEnabled ? '' : 'disabled'}>
</label>
</div>
</div>
<div class="arp-card">
<div class="arp-card-title">快捷操作</div>
<div class="arp-actions-row">
<button type="button" class="arp-inline-button" data-action="scan">立即扫描</button>
<button type="button" class="arp-inline-button" data-action="stop-all">清空等待队列</button>
</div>
<div class="arp-note">支持聊天室“经典”和“简约”两种样式;右下角入口按钮和设置面板都支持拖拽。</div>
</div>
`;
bindPanelInputs(body);
}
function bindPanelInputs(body) {
body.querySelectorAll('[data-setting="enabled"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.enabled = node.checked;
saveSettings();
applySettingsChange();
});
});
body.querySelectorAll('[data-setting="delaySeconds"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.delaySeconds = normalizeDelay(node.value);
node.value = String(state.settings.delaySeconds);
saveSettings();
applySettingsChange();
});
});
body.querySelectorAll('[data-setting="autoReplyEnabled"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.autoReplyEnabled = node.checked;
saveSettings();
updateStatus('已更新感谢开关');
});
});
body.querySelectorAll('[data-setting="thankTemplate"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.thankTemplate = normalizeThankTemplate(node.value);
node.value = state.settings.thankTemplate;
saveSettings();
updateStatus('已更新感谢文案模板');
});
});
body.querySelectorAll('[data-setting="thankLimitEnabled"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.thankLimitEnabled = node.checked;
saveSettings();
updateStatus('已更新感谢频控开关');
renderPanelBody();
});
});
body.querySelectorAll('[data-setting="thankLimitWindowMinutes"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.thankLimitWindowMinutes = normalizeThankLimitWindow(node.value);
node.value = String(state.settings.thankLimitWindowMinutes);
saveSettings();
updateStatus('已更新感谢频控窗口');
renderPanelBody();
});
});
body.querySelectorAll('[data-setting="thankLimitMaxPerUser"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.thankLimitMaxPerUser = normalizeThankLimitMax(node.value);
node.value = String(state.settings.thankLimitMaxPerUser);
saveSettings();
updateStatus('已更新感谢频控次数');
renderPanelBody();
});
});
body.querySelectorAll('[data-type]').forEach((node) => {
node.addEventListener('change', function () {
const type = node.getAttribute('data-type');
state.settings.types[type] = node.checked;
saveSettings();
applySettingsChange();
});
});
body.querySelectorAll('[data-setting="rpsMode"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.rockPaperScissors.randomGesture = node.value === 'random';
saveSettings();
applySettingsChange();
});
});
body.querySelectorAll('[data-setting="fixedGesture"]').forEach((node) => {
node.addEventListener('change', function () {
state.settings.rockPaperScissors.fixedGesture = ['0', '1', '2'].includes(node.value) ? node.value : '0';
saveSettings();
applySettingsChange();
});
});
}
function handlePanelAction(event) {
const actionNode = event.target.closest('[data-action]');
if (!actionNode) {
return;
}
const action = actionNode.getAttribute('data-action');
if (action === 'close') {
closePanel();
return;
}
if (action === 'scan') {
scanRecentMessages(true);
return;
}