-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscriptcat-douyin-fire-helper.user.js
More file actions
3379 lines (2942 loc) · 135 KB
/
scriptcat-douyin-fire-helper.user.js
File metadata and controls
3379 lines (2942 loc) · 135 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 抖音续火花自动发送助手-集成一言API和TXTAPI-支持多用户-增强版
// @namespace http://tampermonkey.net/
// @version 3.1.1
// @description 每天自动发送续火消息,支持自定义时间,集成一言API和TXTAPI,支持多目标用户,记录火花天数,专属一言,随机发送时间,用户列表解析,自动重试
// @author 飔梦 / 阚泥 / xiaohe123awa
// @match https://creator.douyin.com/creator-micro/data/following/chat
// @icon https://free.picui.cn/free/2025/11/23/69226264aca4e.png
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_notification
// @grant GM_listValues
// @grant GM_deleteValue
// @grant GM_xmlhttpRequest
// @connect hitokoto.cn
// ==/UserScript==
(function() {
'use strict';
// 默认配置
const DEFAULT_CONFIG = {
baseMessage: "续火",
sendTime: "00:01:00",
sendTimeRandom: false,
sendTimeRangeStart: "23:30:00",
sendTimeRangeEnd: "00:30:00",
checkInterval: 1000,
maxWaitTime: 30000,
maxRetryCount: 3,
hitokotoTimeout: 60000,
txtApiTimeout: 60000,
useHitokoto: true,
useTxtApi: true,
useSpecialHitokoto: true,
specialHitokotoMode: "random",
specialHitokotoRandom: true,
txtApiMode: "manual",
txtApiManualRandom: true,
customMessage: "—————每日续火—————\n\n[TXTAPI]\n\n—————每日一言—————\n\n[API]\n\n—————专属一言—————\n\n[专属一言]\n\n🔥 火花已续 [天数] 天",
hitokotoFormat: "{hitokoto}\n—— {from}{from_who}",
fromFormat: "{from}",
fromWhoFormat: "「{from_who}」",
txtApiUrl: "https://v1.hitokoto.cn/?encode=text",
txtApiManualText: "文本1\n文本2\n文本3",
enableTargetUser: false,
targetUsernames: "",
userSearchTimeout: 10000,
maxHistoryLogs: 200,
searchDebounceDelay: 500,
searchThrottleDelay: 1000,
clickMethod: "direct",
pageLoadWaitTime: 5000,
chatInputCheckInterval: 1000,
multiUserMode: "sequential",
multiUserRetrySame: false,
fireDays: 1,
lastFireDate: "",
specialHitokotoMonday: "周一专属文案1\n周一专属文案2",
specialHitokotoTuesday: "周二专属文案1\n周二专属文案2",
specialHitokotoWednesday: "周三专属文案1\n周三专属文案2",
specialHitokotoThursday: "周四专属文案1\n周四专属文案2",
specialHitokotoFriday: "周五专属文案1\n周五专属文案2",
specialHitokotoSaturday: "周六专属文案1\n周六专属文案2",
specialHitokotoSunday: "周日专属文案1\n周日专属文案2",
autoRetryEnabled: false, // 新增:自动重试开关
autoRetryInterval: 10, // 新增:自动重试间隔(分钟)
retryAfterMaxReached: true, // 新增:达到最大重试后是否继续重试
retryResetInterval: 10 // 新增:清除重试计数并重试的间隔(分钟)
};
// 状态变量
let isProcessing = false;
let retryCount = 0;
let countdownInterval = null;
let isScriptCat = false;
let userConfig = {};
let nextSendTime = null;
let currentState = "idle";
let chatObserver = null;
let searchTimeout = null;
let lastSearchTime = 0;
let searchDebounceTimer = null;
let chatInputCheckTimer = null;
// 多用户相关变量
let currentUserIndex = -1;
let sentUsersToday = [];
let allTargetUsers = [];
let currentRetryUser = null;
// 专属一言发送记录
let specialHitokotoSentIndexes = {
monday: [],
tuesday: [],
wednesday: [],
thursday: [],
friday: [],
saturday: [],
sunday: []
};
// 拖动相关变量
let isDragging = false;
let dragOffsetX = 0;
let dragOffsetY = 0;
let currentPanel = null;
// 新增:自动重试相关变量
let autoRetryTimer = null;
let lastRetryResetTime = 0;
let isMaxRetryReached = false;
// ==================== 核心功能函数 ====================
// 检测是否是ScriptCat
function detectScriptCat() {
return typeof ScriptCat !== 'undefined' ||
(typeof GM_info !== 'undefined' && GM_info.scriptHandler === 'ScriptCat');
}
// 初始化配置
function initConfig() {
const savedConfig = GM_getValue('userConfig');
userConfig = savedConfig ? {
...DEFAULT_CONFIG,
...savedConfig
} : {
...DEFAULT_CONFIG
};
for (const key in DEFAULT_CONFIG) {
if (userConfig[key] === undefined) {
userConfig[key] = DEFAULT_CONFIG[key];
}
}
if (!GM_getValue('txtApiManualSentIndexes')) {
GM_setValue('txtApiManualSentIndexes', []);
}
if (!GM_getValue('historyLogs')) {
GM_setValue('historyLogs', []);
}
// 初始化火花天数
if (!GM_getValue('fireDays')) {
GM_setValue('fireDays', userConfig.fireDays);
} else {
userConfig.fireDays = GM_getValue('fireDays');
}
// 初始化上次火花日期
if (!GM_getValue('lastFireDate')) {
const today = new Date().toISOString().split('T')[0];
GM_setValue('lastFireDate', today);
userConfig.lastFireDate = today;
} else {
userConfig.lastFireDate = GM_getValue('lastFireDate');
}
// 初始化专属一言发送记录
if (!GM_getValue('specialHitokotoSentIndexes')) {
GM_setValue('specialHitokotoSentIndexes', specialHitokotoSentIndexes);
} else {
specialHitokotoSentIndexes = GM_getValue('specialHitokotoSentIndexes', specialHitokotoSentIndexes);
}
// 初始化多用户数据
if (!GM_getValue('sentUsersToday')) {
GM_setValue('sentUsersToday', []);
}
sentUsersToday = GM_getValue('sentUsersToday', []);
if (!GM_getValue('currentUserIndex')) {
GM_setValue('currentUserIndex', -1);
}
currentUserIndex = GM_getValue('currentUserIndex', -1);
// 初始化重试计数
if (!GM_getValue('retryCount')) {
GM_setValue('retryCount', 0);
}
retryCount = GM_getValue('retryCount', 0);
// 初始化最大重试达到标志
if (!GM_getValue('isMaxRetryReached')) {
GM_setValue('isMaxRetryReached', false);
}
isMaxRetryReached = GM_getValue('isMaxRetryReached', false);
// 初始化上次重试重置时间
if (!GM_getValue('lastRetryResetTime')) {
GM_setValue('lastRetryResetTime', 0);
}
lastRetryResetTime = GM_getValue('lastRetryResetTime', 0);
// 解析目标用户列表
parseTargetUsers();
GM_setValue('userConfig', userConfig);
return userConfig;
}
// 解析目标用户列表
function parseTargetUsers() {
if (!userConfig.targetUsernames || !userConfig.targetUsernames.trim()) {
allTargetUsers = [];
userConfig.enableTargetUser = false; // 目标用户为空时自动关闭
return;
}
const rawText = userConfig.targetUsernames.trim();
allTargetUsers = rawText.split('\n')
.map(user => user.trim())
.filter(user => user.length > 0);
// 目标用户不为空时自动开启
if (allTargetUsers.length > 0) {
userConfig.enableTargetUser = true;
}
addHistoryLog(`解析到 ${allTargetUsers.length} 个目标用户: ${allTargetUsers.join(', ')}`, 'info');
}
// 获取下一个目标用户
function getNextTargetUser() {
if (allTargetUsers.length === 0) {
return null;
}
const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user));
if (unsentUsers.length === 0) {
addHistoryLog('所有目标用户今日都已发送', 'info');
return null;
}
let nextUser;
if (userConfig.multiUserMode === 'random') {
const randomIndex = Math.floor(Math.random() * unsentUsers.length);
nextUser = unsentUsers[randomIndex];
} else {
if (currentUserIndex < 0 || currentUserIndex >= allTargetUsers.length) {
currentUserIndex = 0;
}
let found = false;
for (let i = 0; i < allTargetUsers.length; i++) {
const index = (currentUserIndex + i) % allTargetUsers.length;
const user = allTargetUsers[index];
if (!sentUsersToday.includes(user)) {
nextUser = user;
currentUserIndex = index;
found = true;
break;
}
}
if (!found) {
return null;
}
}
return nextUser;
}
// 标记用户为已发送
function markUserAsSent(username) {
if (!sentUsersToday.includes(username)) {
sentUsersToday.push(username);
GM_setValue('sentUsersToday', sentUsersToday);
}
const index = allTargetUsers.indexOf(username);
if (index !== -1) {
currentUserIndex = (index + 1) % allTargetUsers.length;
GM_setValue('currentUserIndex', currentUserIndex);
}
addHistoryLog(`用户 ${username} 已标记为今日已发送`, 'success');
updateUserStatusDisplay();
}
// 保存配置
function saveConfig() {
GM_setValue('userConfig', userConfig);
GM_setValue('fireDays', userConfig.fireDays);
GM_setValue('lastFireDate', userConfig.lastFireDate);
GM_setValue('specialHitokotoSentIndexes', specialHitokotoSentIndexes);
GM_setValue('retryCount', retryCount);
GM_setValue('isMaxRetryReached', isMaxRetryReached);
GM_setValue('lastRetryResetTime', lastRetryResetTime);
}
// 添加历史日志
function addHistoryLog(message, type = 'info') {
const logs = GM_getValue('historyLogs', []);
const logEntry = {
timestamp: new Date().toISOString(),
message: message,
type: type
};
logs.unshift(logEntry);
if (logs.length > userConfig.maxHistoryLogs) {
logs.splice(userConfig.maxHistoryLogs);
}
GM_setValue('historyLogs', logs);
addLog(message, type);
}
// 获取历史日志
function getHistoryLogs() {
return GM_getValue('historyLogs', []);
}
// 清空历史日志
function clearHistoryLogs() {
GM_setValue('historyLogs', []);
addHistoryLog('历史日志已清空', 'info');
}
// 导出历史日志
function exportHistoryLogs() {
const logs = getHistoryLogs();
const logText = logs.map(log =>
`${new Date(log.timestamp).toLocaleString()} [${log.type.toUpperCase()}] ${log.message}`
).join('\n');
const blob = new Blob([logText], {
type: 'text/plain'
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `抖音续火助手日志_${new Date().toISOString().split('T')[0]}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
addHistoryLog('日志已导出', 'success');
}
// 添加实时日志
function addLog(message, type = 'info') {
const now = new Date();
const timeString = now.toLocaleTimeString();
const logEntry = document.createElement('div');
logEntry.style.color = type === 'success' ? '#00d8b8' : type === 'error' ? '#ff2c54' : '#ffc107';
logEntry.style.padding = '5px 0';
logEntry.style.borderBottom = '1px solid rgba(255,255,255,0.05)';
logEntry.textContent = `${timeString} - ${message}`;
const logContainer = document.getElementById('dy-fire-log');
if (logContainer) {
logContainer.prepend(logEntry);
if (logContainer.children.length > 8) {
logContainer.removeChild(logContainer.lastChild);
}
logContainer.scrollTop = 0;
}
}
// 更新重试计数显示
function updateRetryCount() {
const retryEl = document.getElementById('dy-fire-retry');
if (retryEl) {
retryEl.textContent = `${retryCount}/${userConfig.maxRetryCount}`;
}
}
// 更新一言状态显示
function updateHitokotoStatus(status, isSuccess = true) {
const statusEl = document.getElementById('dy-fire-hitokoto');
if (statusEl) {
statusEl.textContent = status;
statusEl.style.color = isSuccess ? '#00d8b8' : '#ff2c54';
}
}
// 更新TXTAPI状态显示
function updateTxtApiStatus(status, isSuccess = true) {
const statusEl = document.getElementById('dy-fire-txtapi');
if (statusEl) {
statusEl.textContent = status;
statusEl.style.color = isSuccess ? '#00d8b8' : '#ff2c54';
}
}
// 更新专属一言状态显示
function updateSpecialHitokotoStatus(status, isSuccess = true) {
const statusEl = document.getElementById('dy-fire-special-hitokoto');
if (statusEl) {
statusEl.textContent = status;
statusEl.style.color = isSuccess ? '#00d8b8' : '#ff2c54';
}
}
// 更新火花天数显示
function updateFireDaysStatus() {
const statusEl = document.getElementById('dy-fire-days');
if (statusEl) {
statusEl.textContent = userConfig.fireDays;
statusEl.style.color = '#00d8b8';
}
}
// 更新火花天数(每天第一次发送时调用)
function updateFireDays() {
const today = new Date().toISOString().split('T')[0];
const lastFireDate = userConfig.lastFireDate || '';
if (lastFireDate !== today) {
// 新的一天,增加天数
userConfig.fireDays++;
userConfig.lastFireDate = today;
GM_setValue('fireDays', userConfig.fireDays);
GM_setValue('lastFireDate', today);
addHistoryLog(`新的一天开始,火花天数增加为: ${userConfig.fireDays}`, 'success');
updateFireDaysStatus();
}
}
// 初始化聊天列表观察器
function initChatObserver() {
// 先停止旧的观察器,但不记录日志
if (chatObserver) {
stopChatObserver('观察器初始化', true); // 传递skipLog为true
}
if (!userConfig.enableTargetUser || currentState !== 'searching') {
addHistoryLog('观察器未启动: 目标用户功能未启用或当前状态非查找中', 'warn');
return;
}
// 获取当前目标用户信息
const currentTargetUser = currentRetryUser || getNextTargetUser();
const remainingUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user));
chatObserver = new MutationObserver(function(mutations) {
clearTimeout(searchDebounceTimer);
searchDebounceTimer = setTimeout(() => {
const now = Date.now();
if (now - lastSearchTime < userConfig.searchThrottleDelay) {
return;
}
lastSearchTime = now;
// 添加查找统计
const searchStartTime = GM_getValue('searchStartTime', now);
const searchDuration = now - searchStartTime;
// 记录详细查找信息
if (searchDuration > 1000 && searchDuration % 5000 < 100) { // 每5秒记录一次
addHistoryLog(`正在查找用户: ${currentTargetUser || '未知'}, 已查找 ${Math.floor(searchDuration/1000)} 秒, DOM变化 ${mutations.length} 处`, 'info');
}
findAndClickTargetUser();
}, userConfig.searchDebounceDelay);
});
const chatContainer = findChatContainer();
if (chatContainer) {
try {
chatObserver.observe(chatContainer, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
// 记录更详细的启动信息
const targetCount = allTargetUsers.length;
const sentCount = sentUsersToday.length;
const remaining = targetCount - sentCount;
GM_setValue('searchStartTime', Date.now());
addHistoryLog(`聊天列表观察器已启动 (目标: ${currentTargetUser || '获取中...'}, 待发送用户: ${remaining}/${targetCount}, 容器: ${chatContainer.tagName}.${chatContainer.className || 'no-class'})`, 'info');
} catch (error) {
addHistoryLog(`观察器启动失败: ${error.message}`, 'error');
chatObserver = null;
}
} else {
addHistoryLog('未找到聊天列表容器,将使用备用查找策略', 'warn');
try {
chatObserver.observe(document.body, {
childList: true,
subtree: false,
attributes: false,
characterData: false
});
addHistoryLog('聊天列表观察器已启动(备用策略,监控body变化)', 'info');
} catch (error) {
addHistoryLog(`备用观察器启动失败: ${error.message}`, 'error');
chatObserver = null;
}
}
}
// 查找聊天容器
function findChatContainer() {
const possibleSelectors = [
'.chat-list-container',
'.semi-list',
'[role="list"]',
'.conversation-list',
'.message-list'
];
for (const selector of possibleSelectors) {
const container = document.querySelector(selector);
if (container) {
return container;
}
}
const sampleUser = document.querySelector('.item-header-name-vL_79m');
if (sampleUser) {
let parent = sampleUser;
for (let i = 0; i < 10; i++) {
parent = parent.parentElement;
if (parent && parent.children.length > 5) {
return parent;
}
if (!parent) break;
}
}
return null;
}
// 停止聊天观察器
function stopChatObserver(reason = '未知原因', skipLog = false) {
if (chatObserver) {
try {
chatObserver.disconnect();
// 记录观察器详细信息
if (!skipLog) {
const targetCount = allTargetUsers.length;
const sentCount = sentUsersToday.length;
const currentTarget = currentRetryUser || getNextTargetUser();
addHistoryLog(`聊天列表观察器已停止 (原因: ${reason}, 目标用户: ${targetCount}个, 已发送: ${sentCount}/${targetCount}, 当前目标: ${currentTarget || '无'})`, 'info');
}
} catch (error) {
if (!skipLog) {
addHistoryLog(`停止观察器时出错: ${error.message} (原因: ${reason})`, 'error');
}
}
chatObserver = null;
} else {
if (!skipLog) {
addHistoryLog(`尝试停止聊天观察器但观察器不存在 (原因: ${reason})`, 'warn');
}
}
if (searchDebounceTimer) {
clearTimeout(searchDebounceTimer);
searchDebounceTimer = null;
}
if (searchTimeout) {
clearTimeout(searchTimeout);
searchTimeout = null;
}
if (!skipLog && reason !== '观察器初始化') {
// 添加状态信息
const stateInfo = {
'idle': '空闲',
'searching': '查找中',
'found': '已找到用户',
'sending': '发送中',
'processing': '处理中'
};
addHistoryLog(`当前状态: ${stateInfo[currentState] || currentState}, 重试次数: ${retryCount}/${userConfig.maxRetryCount}`, 'info');
}
}
// 安全地创建鼠标事件
function createSafeMouseEvent(type, options = {}) {
try {
const safeOptions = {
bubbles: true,
cancelable: true,
view: window,
...options
};
return new MouseEvent(type, safeOptions);
} catch (error) {
try {
const safeOptions = {
bubbles: true,
cancelable: true,
...options
};
delete safeOptions.view;
return new MouseEvent(type, safeOptions);
} catch (error2) {
addHistoryLog(`创建鼠标事件失败: ${error2.message}`, 'error');
return null;
}
}
}
// 添加查找开始时间记录
function startUserSearch() {
GM_setValue('searchStartTime', Date.now());
GM_setValue('searchMutationCount', 0);
GM_setValue('searchAttemptCount', 0);
}
// 查找并点击目标用户
function findAndClickTargetUser() {
if (!userConfig.enableTargetUser || allTargetUsers.length === 0) {
updateUserStatus('配置错误', false);
return false;
}
if (currentState !== 'searching') {
return false;
}
// 增加查找尝试计数
let searchAttemptCount = GM_getValue('searchAttemptCount', 0) + 1;
GM_setValue('searchAttemptCount', searchAttemptCount);
if (searchAttemptCount > 50) {
addHistoryLog(`查找尝试次数过多(${searchAttemptCount}),可能DOM结构已变化`, 'error');
stopChatObserver('查找尝试次数过多');
return false;
}
let currentTargetUser;
if (userConfig.multiUserRetrySame && retryCount > 1 && currentRetryUser) {
// 重试时使用同一用户
currentTargetUser = currentRetryUser;
addHistoryLog(`重试使用同一用户: ${currentTargetUser}`, 'info');
} else {
currentTargetUser = getNextTargetUser();
currentRetryUser = currentTargetUser;
}
if (!currentTargetUser) {
addHistoryLog('没有可发送的目标用户', 'info');
updateUserStatus('无目标用户', false);
stopChatObserver();
isProcessing = false;
currentRetryUser = null;
return false;
}
GM_setValue('lastTargetUser', currentTargetUser);
addHistoryLog(`查找目标用户: ${currentTargetUser}`, 'info');
updateUserStatus(`寻找: ${currentTargetUser}`, null);
const userElements = document.querySelectorAll('.item-header-name-vL_79m');
let targetElement = null;
for (let element of userElements) {
if (element.textContent.trim() === currentTargetUser) {
targetElement = element;
break;
}
}
if (targetElement) {
addHistoryLog(`找到目标用户: ${currentTargetUser}`, 'success');
updateUserStatus(`已找到: ${currentTargetUser}`, true);
stopChatObserver();
let clickSuccess = false;
if (userConfig.clickMethod === 'direct') {
try {
targetElement.click();
addHistoryLog('使用直接点击方法成功', 'success');
clickSuccess = true;
} catch (error) {
addHistoryLog(`直接点击失败: ${error.message}`, 'error');
}
} else {
try {
const clickEvent = createSafeMouseEvent('click');
if (clickEvent) {
targetElement.dispatchEvent(clickEvent);
addHistoryLog('使用事件触发方法成功', 'success');
clickSuccess = true;
} else {
targetElement.click();
addHistoryLog('事件创建失败,使用直接点击成功', 'success');
clickSuccess = true;
}
} catch (error) {
addHistoryLog(`事件触发失败: ${error.message}`, 'error');
}
}
if (clickSuccess) {
currentState = 'found';
stopChatObserver('成功找到目标用户');
waitForPageLoad().then(() => {
addHistoryLog('页面加载完成,开始查找聊天输入框', 'info');
tryFindChatInput();
}).catch(error => {
addHistoryLog(`等待页面加载超时: ${error.message}`, 'error');
tryFindChatInput();
});
return true;
} else {
try {
let clickableParent = targetElement;
for (let i = 0; i < 5; i++) {
clickableParent = clickableParent.parentElement;
if (!clickableParent) break;
const style = window.getComputedStyle(clickableParent);
if (style.cursor === 'pointer' || clickableParent.onclick) {
clickableParent.click();
addHistoryLog('通过父元素点击成功', 'success');
currentState = 'found';
waitForPageLoad().then(() => {
addHistoryLog('页面加载完成,开始查找聊天输入框', 'info');
tryFindChatInput();
}).catch(error => {
addHistoryLog(`等待页面加载超时: ${error.message}`, 'error');
tryFindChatInput();
});
return true;
}
}
} catch (error) {
addHistoryLog(`父元素点击也失败: ${error.message}`, 'error');
}
updateUserStatus('点击失败', false);
return false;
}
} else {
addHistoryLog(`未找到目标用户: ${currentTargetUser}`, 'warn');
updateUserStatus(`寻找: ${currentTargetUser}`, null);
return false;
}
}
// 等待页面加载完成
function waitForPageLoad() {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error(`页面加载等待超时 (${userConfig.pageLoadWaitTime}ms)`));
}, userConfig.pageLoadWaitTime);
if (document.readyState === 'complete') {
clearTimeout(timeout);
resolve();
return;
}
window.addEventListener('load', function onLoad() {
clearTimeout(timeout);
window.removeEventListener('load', onLoad);
resolve();
});
let checkCount = 0;
const maxChecks = userConfig.pageLoadWaitTime / 100;
const checkInterval = setInterval(() => {
checkCount++;
const chatInput = document.querySelector('.chat-input-dccKiL');
if (chatInput) {
clearTimeout(timeout);
clearInterval(checkInterval);
resolve();
return;
}
if (checkCount >= maxChecks) {
clearTimeout(timeout);
clearInterval(checkInterval);
reject(new Error('页面DOM变化检查超时'));
}
}, 100);
});
}
// 发送消息函数
async function sendMessage() {
if (isProcessing) {
addHistoryLog('已有任务正在进行中', 'error');
return;
}
// 重置最大重试达到标志
isMaxRetryReached = false;
GM_setValue('isMaxRetryReached', false);
if (userConfig.enableTargetUser && allTargetUsers.length > 0) {
const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user));
if (unsentUsers.length === 0) {
addHistoryLog('所有目标用户今日都已发送', 'info');
return;
}
} else {
const lastSentDate = GM_getValue('lastSentDate', '');
const today = new Date().toDateString();
if (lastSentDate === today) {
addHistoryLog('今天已经发送过消息', 'info');
return;
}
}
isProcessing = true;
retryCount = 0;
currentState = 'idle';
updateRetryCount();
addHistoryLog('开始发送流程...', 'info');
executeSendProcess();
}
// 执行发送流程
async function executeSendProcess() {
// 检查是否已达到最大重试次数
if (isMaxRetryReached && userConfig.retryAfterMaxReached) {
// 检查是否到了自动重试时间
const now = Date.now();
const intervalMs = userConfig.autoRetryInterval * 60 * 1000;
if (now - lastRetryResetTime < intervalMs) {
const remainingMinutes = Math.ceil((intervalMs - (now - lastRetryResetTime)) / 60000);
addHistoryLog(`已达到最大重试次数,${remainingMinutes}分钟后将自动重试`, 'info');
isProcessing = false;
return;
} else {
// 重置重试计数
retryCount = 0;
isMaxRetryReached = false;
GM_setValue('retryCount', retryCount);
GM_setValue('isMaxRetryReached', false);
addHistoryLog('自动重试时间已到,重置重试计数', 'info');
}
}
retryCount++;
GM_setValue('retryCount', retryCount);
updateRetryCount();
if (retryCount > userConfig.maxRetryCount) {
if (userConfig.retryAfterMaxReached) {
isMaxRetryReached = true;
lastRetryResetTime = Date.now();
GM_setValue('isMaxRetryReached', true);
GM_setValue('lastRetryResetTime', lastRetryResetTime);
addHistoryLog(`已达到最大重试次数 (${userConfig.maxRetryCount}),${userConfig.autoRetryInterval}分钟后将自动重试`, 'error');
// 启动自动重试计时器
startAutoRetryTimer();
} else {
addHistoryLog(`已达到最大重试次数 (${userConfig.maxRetryCount}),停止重试`, 'error');
}
isProcessing = false;
currentState = 'idle';
stopChatObserver('达到最大重试次数');
currentRetryUser = null;
return;
}
addHistoryLog(`尝试发送 (${retryCount}/${userConfig.maxRetryCount})`, 'info');
if (userConfig.enableTargetUser && allTargetUsers.length > 0) {
currentState = 'searching';
const searchTimeoutId = setTimeout(() => {
if (currentState === 'searching') {
addHistoryLog('用户查找超时', 'error');
updateUserStatus('查找超时', false);
stopChatObserver('用户查找超时'); // 这里记录日志
setTimeout(executeSendProcess, 2000);
}
}, userConfig.userSearchTimeout);
initChatObserver();
const found = findAndClickTargetUser();
if (!found) {
// 用户查找失败,观察器会继续工作
}
} else {
setTimeout(tryFindChatInput, 1000);
}
}
// 启动自动重试计时器
function startAutoRetryTimer() {
if (autoRetryTimer) {
clearTimeout(autoRetryTimer);
}
if (!userConfig.retryAfterMaxReached) {
return;
}
const intervalMs = userConfig.autoRetryInterval * 60 * 1000;
autoRetryTimer = setTimeout(() => {
if (isMaxRetryReached && !isProcessing) {
addHistoryLog('自动重试计时器触发,重置重试计数并重新发送', 'info');
// 重置重试计数
retryCount = 0;
isMaxRetryReached = false;
GM_setValue('retryCount', retryCount);
GM_setValue('isMaxRetryReached', false);
updateRetryCount();
// 重新发送
sendMessage();
}
}, intervalMs);
}
// 重置重试计数并发送(用于定时任务)
function resetRetryAndSend() {
if (isProcessing) {
addHistoryLog('已有任务正在进行中,跳过重置重试', 'info');
return;
}
// 重置重试计数
retryCount = 0;
isMaxRetryReached = false;
lastRetryResetTime = Date.now();
GM_setValue('retryCount', retryCount);
GM_setValue('isMaxRetryReached', false);
GM_setValue('lastRetryResetTime', lastRetryResetTime);
updateRetryCount();
addHistoryLog(`已重置重试计数,当前重试次数: ${retryCount}`, 'success');
// 检查是否需要发送
autoSendIfNeeded();
}
// 尝试查找聊天输入框并发送消息
let chatInputRetryCount = 0;
async function tryFindChatInput() {
if (chatInputCheckTimer) {
clearTimeout(chatInputCheckTimer);
}
const input = document.querySelector('.chat-input-dccKiL');
if (input) {
chatInputRetryCount = 0;
addHistoryLog('找到聊天输入框', 'info');
let messageToSend;
try {
messageToSend = await getMessageContent();
addHistoryLog('消息内容准备完成', 'success');
} catch (error) {
addHistoryLog(`消息获取失败: ${error.message}`, 'error');
messageToSend = `${userConfig.baseMessage} | 消息获取失败~`;
}
currentState = 'sending';
input.textContent = '';
input.focus();
const lines = messageToSend.split('\n');
for (let i = 0; i < lines.length; i++) {
document.execCommand('insertText', false, lines[i]);
if (i < lines.length - 1) {
document.execCommand('insertLineBreak');
}
}
input.dispatchEvent(new Event('input', {
bubbles: true
}));
setTimeout(() => {
const sendBtn = document.querySelector('.chat-btn');
if (sendBtn && !sendBtn.disabled) {
addHistoryLog('正在发送消息...', 'info');