-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathclipboard-sender.user.js
More file actions
5193 lines (4617 loc) · 205 KB
/
clipboard-sender.user.js
File metadata and controls
5193 lines (4617 loc) · 205 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 通用网页脚本框架(重构版)
// @namespace https://github.com/hjdhnx/drpy-node
// @description 日志、右下角弹窗、按钮皮肤、可配置布局、按钮集合弹窗、按钮开关、定时任务等;结构化、可扩展。
// @version 2.0.2
// @author taoist (refactor by chatgpt)
// @match https://*.baidu.com/*
// @match https://www.baidu.com/*
// @match https://connect.huaweicloud.com/*
// @match https://*.huaweicloud.com/*
// @match https://*.iconfont.cn/*
// @match https://*.ziwierp.cn/*
// @run-at document-end
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_listValues
// ==/UserScript==
/**
* 本脚本对原有“智能剪切板推送”脚本进行结构化重构,目标:
* 1) 更易读:模块化、注释、类型提示(JSDoc)。
* 2) 易扩展:统一的 Action 注册与按钮渲染;主题与布局可配置;开关与定时任务可复用。
* 3) 零依赖:不再注入 jQuery。
*
* ——— 如何扩展(最重要)———
* 1. 在 ACTIONS 中新增一个 action:
* {
* id: 'hello', // 唯一 ID
* label: '打个招呼', // 按钮文字
* column: 1, // 放在第几列(1 ~ 5)
* handler() { alert('hi'); }
* }
*
* 2. 若想把多个按钮放到“按钮集合弹窗”,把它们的 group 指定为同一个名字:
* { id:'tf', label:'开逃犯', group:'开关集', handler(){ ... } }
* 点击“开关集”主按钮时会弹出集合弹窗,里面包含同组按钮。
*
* 3. 定时任务:Scheduler.registerDaily('08:30', () => { ... }, 'taskKey');
* 每天 08:30 触发;同一分钟只触发一次。
*
* 4. 主题:Theme.next() 可切换皮肤;Theme.apply() 应用主题到组件。
*
* 5. 日志:使用 Logger.log(...);点击“隐藏日志”可隐藏/显示。
*/
/*
变更说明(修复):
- GroupPopup.addButton 支持 isToggle(会显示 inset/outset)并把状态保存在 store。
- 主按钮 openGroup(name) 改为 toggleGroup(name),点击可切换显示/隐藏。
- 点击 overlay(弹窗外部)也会关闭弹窗。
- 点击组内按钮后(无论 toggle 还是普通)会收起弹窗(如需改为不收起可调整)。
*/
(function () {
'use strict';
/** *************************** 基础配置 ******************************** */
const META = Object.freeze({ version: '2.0.2', name: '通用网页脚本框架(重构版)' });
const CONFIG = {
buttonTop: 280,
popTop: 150,
baseLeft: 0,
columnWidth: 70,
columnGap: 70,
buttonHeight: 24,
layoutMode: 'fixed', // 'fixed' or 'auto'
layoutOffset: 10,
themes: [
{ name: '紫色起源', fg: '#E0EEEE', bg: '#9370DB' },
{ name: '淡绿生机', fg: '#BFEFFF', bg: '#BDB76B' },
{ name: '丰收时节', fg: '#E0EEE0', bg: '#CD661D' },
{ name: '粉色佳人', fg: '#FFFAFA', bg: '#FFB6C1' },
{ name: '黑白优雅', fg: '#111', bg: '#eee' },
// 新增渐变色皮肤
{ name: '清新蓝绿', fg: '#ffffff', bg: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)' },
{ name: '热情夕阳', fg: '#4a2f2f', bg: 'linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%)' },
{ name: '高级紫罗兰', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%)' },
{ name: '极光青绿', fg: '#083b2e', bg: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)' },
{ name: '科技未来蓝紫', fg: '#ffffff', bg: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)' },
// 新增更多渐变色皮肤
{ name: '日落金橙', fg: '#ffffff', bg: 'linear-gradient(135deg, #f6d365 0%, #fda085 100%)' },
{ name: '薄荷清凉', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%)' },
{ name: '浪漫粉紫', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff758c 0%, #ff7eb3 100%)' },
{ name: '深海蓝', fg: '#ffffff', bg: 'linear-gradient(135deg, #0c2b5b 0%, #204584 100%)' },
{ name: '森林绿意', fg: '#ffffff', bg: 'linear-gradient(135deg, #134e5e 0%, #71b280 100%)' },
{ name: '莓果甜心', fg: '#ffffff', bg: 'linear-gradient(135deg, #c71d6f 0%, #d09693 100%)' },
{ name: '柠檬青柚', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #96fbc4 0%, #f9f586 100%)' },
{ name: '星空紫', fg: '#ffffff', bg: 'linear-gradient(135deg, #231557 0%, #44107a 29%, #ff1361 67%, #fff800 100%)' },
{ name: '珊瑚橙红', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff9966 0%, #ff5e62 100%)' },
{ name: '冰川蓝白', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%)' },
// 新增现代感皮肤
{ name: '赛博朋克', fg: '#00ffff', bg: 'linear-gradient(135deg, #0f0f23 0%, #2d1b69 50%, #ff006e 100%)' },
{ name: '霓虹夜色', fg: '#ffffff', bg: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #e94560 100%)' },
{ name: '极简黑金', fg: '#ffd700', bg: 'linear-gradient(135deg, #000000 0%, #434343 100%)' },
{ name: '银河星尘', fg: '#ffffff', bg: 'linear-gradient(135deg, #2c3e50 0%, #3498db 50%, #9b59b6 100%)' },
{ name: '电光蓝紫', fg: '#ffffff', bg: 'linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%)' },
{ name: '炫彩极光', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff0844 0%, #ffb199 25%, #00d4ff 50%, #90e0ef 75%, #a8dadc 100%)' },
{ name: '暗黑科技', fg: '#00ff41', bg: 'linear-gradient(135deg, #0d1421 0%, #1a252f 50%, #2a3441 100%)' },
{ name: '彩虹渐变', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff0000 0%, #ff8000 16.66%, #ffff00 33.33%, #80ff00 50%, #00ff80 66.66%, #0080ff 83.33%, #8000ff 100%)' },
// 新增自然风皮肤
{ name: '樱花飞舞', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #ffecd2 0%, #fcb69f 50%, #ff9a9e 100%)' },
{ name: '秋叶满山', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff7e5f 0%, #feb47b 50%, #ff6b6b 100%)' },
{ name: '海洋深处', fg: '#ffffff', bg: 'linear-gradient(135deg, #667db6 0%, #0082c8 50%, #0052d4 100%)' },
{ name: '翡翠森林', fg: '#ffffff', bg: 'linear-gradient(135deg, #11998e 0%, #38ef7d 100%)' },
{ name: '薰衣草田', fg: '#ffffff', bg: 'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)' },
// 新增艺术感皮肤
{ name: '油画印象', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #ffecd2 0%, #fcb69f 30%, #ff9a9e 60%, #fecfef 100%)' },
{ name: '水彩渲染', fg: '#ffffff', bg: 'linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #4facfe 100%)' },
{ name: '抽象几何', fg: '#ffffff', bg: 'linear-gradient(45deg, #ff6b6b 0%, #4ecdc4 25%, #45b7d1 50%, #96ceb4 75%, #ffeaa7 100%)' },
{ name: '梦幻极光', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #a8edea 0%, #fed6e3 25%, #d299c2 50%, #fef9d7 75%, #dae2f8 100%)' },
{ name: '水墨丹青', fg: '#ffffff', bg: 'linear-gradient(135deg, #2c3e50 0%, #34495e 30%, #7f8c8d 60%, #95a5a6 100%)' },
{ name: '火焰燃烧', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff4e50 0%, #f9ca24 50%, #ff6348 100%)' },
{ name: '冰雪奇缘', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #e3f2fd 0%, #bbdefb 25%, #90caf9 50%, #64b5f6 75%, #42a5f5 100%)' },
{ name: '紫罗兰梦', fg: '#ffffff', bg: 'linear-gradient(135deg, #8e44ad 0%, #9b59b6 25%, #af7ac5 50%, #c39bd3 75%, #d7bde2 100%)' },
// 新增经典配色
{ name: '复古胶片', fg: '#f4f4f4', bg: 'linear-gradient(135deg, #8b5a3c 0%, #d4a574 50%, #f4e4bc 100%)' },
{ name: '工业风格', fg: '#ffffff', bg: 'linear-gradient(135deg, #2c3e50 0%, #34495e 50%, #95a5a6 100%)' },
{ name: '马卡龙色', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #ffeaa7 0%, #fab1a0 25%, #fd79a8 50%, #a29bfe 75%, #74b9ff 100%)' },
{ name: '暗夜精灵', fg: '#00d4aa', bg: 'linear-gradient(135deg, #0c0c0c 0%, #1a1a1a 50%, #2d2d2d 100%)' },
// 新增时尚潮流皮肤
{ name: '玫瑰金辉', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #f8cdda 0%, #1d2b64 100%)' },
{ name: '翡翠绿洲', fg: '#ffffff', bg: 'linear-gradient(135deg, #134e5e 0%, #71b280 100%)' },
{ name: '琥珀夕照', fg: '#ffffff', bg: 'linear-gradient(135deg, #fc4a1a 0%, #f7b733 100%)' },
{ name: '深邃蓝海', fg: '#ffffff', bg: 'linear-gradient(135deg, #1e3c72 0%, #2a5298 100%)' },
{ name: '紫晶魅惑', fg: '#ffffff', bg: 'linear-gradient(135deg, #8360c3 0%, #2ebf91 100%)' },
{ name: '橙红烈焰', fg: '#ffffff', bg: 'linear-gradient(135deg, #ff512f 0%, #dd2476 100%)' },
{ name: '青春活力', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #56ab2f 0%, #a8e6cf 100%)' },
{ name: '梦幻粉紫', fg: '#ffffff', bg: 'linear-gradient(135deg, #cc2b5e 0%, #753a88 100%)' },
{ name: '金属质感', fg: '#ffffff', bg: 'linear-gradient(135deg, #bdc3c7 0%, #2c3e50 100%)' },
{ name: '炫酷黑红', fg: '#ffffff', bg: 'linear-gradient(135deg, #000000 0%, #e74c3c 100%)' },
// 新增自然风光皮肤
{ name: '晨曦微光', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)' },
{ name: '暮色苍茫', fg: '#ffffff', bg: 'linear-gradient(135deg, #2c3e50 0%, #fd746c 100%)' },
{ name: '春意盎然', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #a8e6cf 0%, #dcedc1 100%)' },
{ name: '秋韵浓浓', fg: '#ffffff', bg: 'linear-gradient(135deg, #f77062 0%, #fe5196 100%)' },
{ name: '冬雪皑皑', fg: '#2d2d2d', bg: 'linear-gradient(135deg, #e6ddd4 0%, #d5def5 100%)' },
{ name: '夏日清凉', fg: '#ffffff', bg: 'linear-gradient(135deg, #00b4db 0%, #0083b0 100%)' },
// 新增科幻未来皮肤
{ name: '星际穿越', fg: '#ffffff', bg: 'linear-gradient(135deg, #0f0f23 0%, #8e44ad 50%, #3498db 100%)' },
{ name: '量子空间', fg: '#00ffff', bg: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%)' },
{ name: '机械战警', fg: '#ffffff', bg: 'linear-gradient(135deg, #434343 0%, #000000 50%, #ff6b6b 100%)' },
{ name: '虚拟现实', fg: '#ffffff', bg: 'linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%)' },
{ name: '时空隧道', fg: '#ffffff', bg: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 50%, #43e97b 100%)' },
// 新增奢华典雅皮肤
{ name: '皇室紫金', fg: '#ffd700', bg: 'linear-gradient(135deg, #2c1810 0%, #8e44ad 50%, #f39c12 100%)' },
{ name: '贵族蓝银', fg: '#ffffff', bg: 'linear-gradient(135deg, #2c3e50 0%, #3498db 50%, #ecf0f1 100%)' },
{ name: '典雅黑白', fg: '#ffffff', bg: 'linear-gradient(135deg, #000000 0%, #434343 50%, #ffffff 100%)' },
{ name: '奢华红金', fg: '#ffd700', bg: 'linear-gradient(135deg, #8b0000 0%, #dc143c 50%, #ffd700 100%)' },
{ name: '翡翠宝石', fg: '#ffffff', bg: 'linear-gradient(135deg, #134e5e 0%, #71b280 50%, #a8e6cf 100%)' },
],
defaultThemeIndex: 0,
storagePrefix: 'tmx.framework.'
};
function getLayoutOffset(defaultOffset = 10) {
if (CONFIG.layoutMode === 'auto') {
const isMobile = /Android|iPhone|SymbianOS|Windows Phone|iPad|iPod/i.test(navigator.userAgent);
if (isMobile) return 10;
const h = window.screen.height;
if (h === 1080) return 300;
if (h === 768) return 100;
if (h === 720) return 50;
if (h < 720) return 0;
if (h > 1080) return 500;
return defaultOffset;
}
return Number(CONFIG.layoutOffset) || defaultOffset;
}
/** *************************** 存储小工具 ******************************* */
class Store {
constructor(prefix) {
this.prefix = prefix;
this.ls = window.localStorage;
this.ss = window.sessionStorage;
// 检测是否支持GM存储API
this.hasGMStorage = typeof GM_setValue !== 'undefined' && typeof GM_getValue !== 'undefined';
}
key(k) {
return `${this.prefix}${k}`;
}
get(k, d = null) {
try {
if (this.hasGMStorage) {
// 使用GM全局存储
const v = GM_getValue(this.key(k), null);
return v == null ? d : JSON.parse(v);
} else {
// 降级到localStorage
const v = this.ls.getItem(this.key(k));
return v == null ? d : JSON.parse(v);
}
} catch (e) {
console.warn('存储读取失败:', e);
return d;
}
}
set(k, v) {
try {
if (this.hasGMStorage) {
// 使用GM全局存储
GM_setValue(this.key(k), JSON.stringify(v));
} else {
// 降级到localStorage
this.ls.setItem(this.key(k), JSON.stringify(v));
}
} catch (e) {
console.warn('存储写入失败:', e);
}
}
remove(k) {
try {
if (this.hasGMStorage) {
// 使用GM全局存储
if (typeof GM_deleteValue !== 'undefined') {
GM_deleteValue(this.key(k));
} else {
GM_setValue(this.key(k), null);
}
} else {
// 降级到localStorage
this.ls.removeItem(this.key(k));
}
} catch (e) {
console.warn('存储删除失败:', e);
}
}
// Session存储仍使用sessionStorage(因为GM不支持session级别存储)
sget(k, d = null) {
try {
const v = this.ss.getItem(this.key(k));
return v == null ? d : JSON.parse(v);
} catch (e) {
console.warn('Session存储读取失败:', e);
return d;
}
}
sset(k, v) {
try {
this.ss.setItem(this.key(k), JSON.stringify(v));
} catch (e) {
console.warn('Session存储写入失败:', e);
}
}
sremove(k) {
try {
this.ss.removeItem(this.key(k));
} catch (e) {
console.warn('Session存储删除失败:', e);
}
}
// 获取所有存储的键(仅GM模式支持)
getAllKeys() {
if (this.hasGMStorage && typeof GM_listValues !== 'undefined') {
try {
return GM_listValues().filter(key => key.startsWith(this.prefix));
} catch (e) {
console.warn('获取存储键列表失败:', e);
return [];
}
}
return [];
}
// 获取存储模式信息
getStorageInfo() {
return {
mode: this.hasGMStorage ? 'GM全局存储' : 'localStorage',
crossDomain: this.hasGMStorage,
prefix: this.prefix
};
}
}
const store = new Store(CONFIG.storagePrefix);
/** *************************** DOM 创建助手 ****************************** */
const h = (tag, attrs = {}, children = []) => {
const el = document.createElement(tag);
for (const [k, v] of Object.entries(attrs)) {
if (k === 'style' && typeof v === 'object') Object.assign(el.style, v);
else if (k.startsWith('on') && typeof v === 'function') el.addEventListener(k.slice(2), v);
else if (v != null) el.setAttribute(k, String(v));
}
for (const child of [].concat(children)) {
if (child == null) continue;
el.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
}
return el;
};
/** *************************** 主题 ************************************* */
const Theme = {
index: store.get('theme.index', CONFIG.defaultThemeIndex),
get current() {
return CONFIG.themes[this.index % CONFIG.themes.length];
},
next() {
this.setIndex((this.index + 1) % CONFIG.themes.length);
},
setIndex(i) {
this.index = i;
store.set('theme.index', i);
this.apply();
},
apply() {
document.documentElement.style.setProperty('--tmx-fg', this.current.fg);
document.documentElement.style.setProperty('--tmx-bg', this.current.bg);
document.documentElement.style.setProperty('--tmx-btn-h', CONFIG.buttonHeight + 'px');
}
};
/** *************************** 日志(简化) ***************************** */
const Logger = (() => {
let el, hooked = false, orig = { log: console.log, clear: console.clear };
function ensure() {
if (el) return;
// 检测是否为移动端设备
const isMobile = /Android|iPhone|SymbianOS|Windows Phone|iPad|iPod/i.test(navigator.userAgent);
// 计算日志窗口的最大宽度:按钮宽度 * 总列数(5)
const loggerMaxWidth = CONFIG.columnWidth * 5; // 70 * 5 = 350px
let loggerStyle;
if (isMobile) {
// 移动端:日志窗体在隐藏日志按钮上方
const hideLogBtn = buttonMap.get('toggle-log');
let left = CONFIG.baseLeft + getLayoutOffset();
// 计算合适的日志窗口高度,确保不超出屏幕顶部
const viewportHeight = window.innerHeight;
let maxLoggerHeight = Math.min(285, viewportHeight * 0.4); // 最大不超过视窗高度的40%
let top = CONFIG.buttonTop - maxLoggerHeight - 10; // 日志窗体高度 + 10px间距
// 如果隐藏日志按钮已存在,根据其位置动态调整
if (hideLogBtn) {
const btnRect = hideLogBtn.getBoundingClientRect();
left = btnRect.left;
top = btnRect.top - maxLoggerHeight - 10;
}
// 确保不超出视窗顶部,留出至少10px边距
if (top < 10) {
top = 10;
// 如果顶部空间不足,重新计算高度
const availableHeight = (hideLogBtn ? hideLogBtn.getBoundingClientRect().top : CONFIG.buttonTop) - 20;
if (availableHeight > 100) {
maxLoggerHeight = Math.min(maxLoggerHeight, availableHeight);
}
}
loggerStyle = {
position: 'fixed',
left: left + 'px',
top: top + 'px',
minWidth: '220px',
maxWidth: Math.min(loggerMaxWidth, window.innerWidth - 10) + 'px', // 移动端:5个按钮宽度或屏幕宽度-10px
maxHeight: maxLoggerHeight + 'px',
overflow: 'auto',
fontFamily: 'Helvetica,Arial,sans-serif',
fontSize: '12px',
fontWeight: 'bold',
padding: '6px',
background: 'var(--tmx-bg)',
color: 'var(--tmx-fg)',
border: '1px solid #aaa',
zIndex: 2147483640, // 降低层级,确保GroupPopup在上方
opacity: 0.9,
wordWrap: 'break-word',
whiteSpace: 'pre-wrap'
};
} else {
// PC端:保持原有位置(最后一列按钮右边)
loggerStyle = {
position: 'fixed',
left: (CONFIG.baseLeft + getLayoutOffset() + loggerMaxWidth) + 'px',
top: (CONFIG.buttonTop + 3) + 'px',
minWidth: '220px',
maxWidth: loggerMaxWidth + 'px', // PC端使用计算出的最大宽度
maxHeight: '285px',
overflow: 'auto',
fontFamily: 'Helvetica,Arial,sans-serif',
fontSize: '12px',
fontWeight: 'bold',
padding: '6px',
background: 'var(--tmx-bg)',
color: 'var(--tmx-fg)',
border: '1px solid #aaa',
zIndex: 2147483646,
opacity: 0.9,
wordWrap: 'break-word',
whiteSpace: 'pre-wrap'
};
}
el = h('div', {
id: 'tmx-logger',
style: loggerStyle
});
document.body.appendChild(el);
}
function hook() {
if (hooked) return;
ensure();
console.log = (...args) => {
append(args.join(' '));
orig.log.apply(console, args);
};
console.clear = () => {
clear();
orig.clear.apply(console);
};
hooked = true;
}
function append(text) {
ensure();
const row = h('div', {
style: {
lineHeight: '18px',
background: el.children.length % 2 ? 'rgba(255,255,255,0.2)' : ''
}
}, text);
el.appendChild(row);
el.scrollTop = el.scrollHeight - el.clientHeight;
}
function clear() {
if (el) el.innerHTML = '';
}
function hide() {
if (el) el.style.display = 'none';
}
function show() {
ensure();
el.style.display = '';
}
function applyTheme() {
if (el) {
el.style.background = 'var(--tmx-bg)';
el.style.color = 'var(--tmx-fg)';
}
}
return { hook, append, clear, hide, show, applyTheme };
})();
/** *************************** 右下角弹窗 ******************************** */
const Toast = (() => {
let root, content, titleEl, minBtn;
function ensure() {
if (root) return;
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
root = h('div', {
id: 'tmx-toast',
style: {
position: 'fixed',
right: '10px',
bottom: '10px',
minWidth: isMobile ? '200px' : '250px',
maxWidth: isMobile ? '90vw' : '400px',
width: 'auto',
border: '1px solid #aaa',
background: '#fff',
zIndex: 2147483645,
display: 'none'
}
});
const header = h('div', {
style: {
height: '36px',
lineHeight: '36px',
padding: '0 8px',
color: 'var(--tmx-fg)',
background: 'var(--tmx-bg)',
borderBottom: '1px solid #aaa',
fontWeight: 'bold',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center'
}
});
titleEl = h('span', {}, '通知');
const btns = h('div', {
style: {
display: 'flex',
gap: '5px'
}
});
minBtn = h('button', {
style: {
background: 'none',
border: 'none',
color: 'var(--tmx-fg)',
fontSize: '16px',
cursor: 'pointer',
padding: '2px 6px',
borderRadius: '2px',
lineHeight: '1'
}
}, '−');
const closeBtn = h('button', {
style: {
background: 'none',
border: 'none',
color: 'var(--tmx-fg)',
fontSize: '16px',
cursor: 'pointer',
padding: '2px 6px',
borderRadius: '2px',
lineHeight: '1'
}
}, '×');
btns.append(minBtn, closeBtn);
header.append(titleEl, btns);
content = h('div', {
style: {
minHeight: '60px',
maxHeight: isMobile ? '40vh' : '300px',
width: '100%',
overflow: 'auto',
fontSize: '13px',
fontWeight: 'normal', // 确保文字不加粗
padding: '8px',
textAlign: 'left',
background: '#fff', // 设置内容区域背景色
borderTop: '1px solid #eee', // 添加顶部边框分隔线
borderRight: '1px solid #eee' // 添加右边框线条
}
});
root.append(header, content);
document.body.appendChild(root);
let expanded = true;
minBtn.addEventListener('click', () => {
expanded = !expanded;
if (expanded) {
// 展开状态:恢复到右下角
content.style.display = '';
header.style.display = '';
// 重置header的flex布局样式
header.style.display = 'flex';
header.style.justifyContent = 'space-between';
header.style.alignItems = 'center';
root.style.right = '10px';
root.style.bottom = '10px';
root.style.minWidth = isMobile ? '200px' : '250px';
root.style.maxWidth = isMobile ? '90vw' : '400px';
root.style.width = 'auto';
root.style.height = '';
root.style.padding = '';
root.style.borderRadius = '';
root.style.boxShadow = '';
root.style.fontSize = '';
root.style.display = ''; // 恢复默认display
root.style.justifyContent = ''; // 清除flex属性
root.style.alignItems = ''; // 清除flex属性
root.style.boxSizing = '';
root.style.background = ''; // 清除背景色
root.style.color = ''; // 清除文字颜色
root.style.cursor = ''; // 清除鼠标样式
content.style.background = '#fff'; // 重置内容区域背景色
content.style.borderTop = '1px solid #eee'; // 重置顶部边框
content.style.borderRight = '1px solid #eee'; // 重置右边框
content.style.fontWeight = 'normal'; // 重置字体粗细
// 清空最小化内容 - 移除所有直接添加到root的子元素(除了header和content)
const childrenToRemove = [];
for (let child of root.children) {
if (child !== header && child !== content) {
childrenToRemove.push(child);
}
}
childrenToRemove.forEach(child => child.remove());
// 弹窗还原后重新计算调试代码容器位置
if (window.DebugWindow && window.DebugWindow.updateMinimizedContainerPosition) {
window.DebugWindow.updateMinimizedContainerPosition();
}
} else {
// 最小化状态:固定在最右下角,样式与调试窗口一致
content.style.display = 'none';
header.style.display = 'none';
// 弹窗提示固定在底部
root.style.right = '10px';
root.style.bottom = '10px';
root.style.width = '120px'; // 设置固定宽度
root.style.minWidth = ''; // 清除最小宽度限制
root.style.maxWidth = ''; // 清除最大宽度限制
root.style.height = '32px'; // 设置固定高度
root.style.padding = '8px 12px'; // 与调试窗口最小化项一致
root.style.borderRadius = '4px'; // 与调试窗口最小化项一致
root.style.boxShadow = '0 2px 8px rgba(0,0,0,0.2)'; // 与调试窗口最小化项一致
root.style.fontSize = '12px'; // 与调试窗口最小化项一致
root.style.background = 'var(--tmx-bg)'; // 添加背景颜色,与全局皮肤色保持一致
root.style.color = 'var(--tmx-fg)'; // 添加文字颜色
root.style.display = 'flex'; // 使用flex布局,与调试窗口一致
root.style.justifyContent = 'space-between'; // 与调试窗口布局一致
root.style.alignItems = 'center'; // 垂直居中
root.style.boxSizing = 'border-box'; // 确保padding包含在尺寸内
root.style.cursor = 'pointer';
// 创建最小化内容
const minimizedTitle = h('span', {
style: {
fontWeight: 'normal' // 确保最小化标题文字不加粗
}
}, titleEl.textContent);
const minimizedCloseBtn = h('span', {
style: {
cursor: 'pointer',
fontSize: '14px',
fontWeight: 'bold'
},
onclick: (e) => {
e.stopPropagation();
root.remove();
}
}, '×');
// 先清理可能存在的旧的最小化元素
const childrenToRemove = [];
for (let child of root.children) {
if (child !== header && child !== content) {
childrenToRemove.push(child);
}
}
childrenToRemove.forEach(child => child.remove());
root.appendChild(minimizedTitle);
root.appendChild(minimizedCloseBtn);
// 弹窗最小化后重新计算调试代码容器位置
if (window.DebugWindow && window.DebugWindow.updateMinimizedContainerPosition) {
window.DebugWindow.updateMinimizedContainerPosition();
}
}
});
// 点击最小化状态时展开
root.addEventListener('click', (e) => {
if (!expanded && (e.target === root || e.target === root.minimizedContent || e.target.tagName === 'SPAN' && e.target.textContent !== '×')) {
minBtn.click();
}
});
closeBtn.addEventListener('click', () => root.remove());
}
function show(title, html) {
ensure();
titleEl.textContent = title || '通知';
if (typeof html === 'string') content.innerHTML = html; else {
content.innerHTML = '';
content.append(html);
}
root.style.display = '';
}
function resize(hh, ww) {
ensure();
content.style.height = Math.max(60, hh) + 'px';
root.style.width = Math.max(220, ww) + 'px';
}
function hide() {
if (root) {
root.remove();
root = null;
content = null;
titleEl = null;
minBtn = null;
}
}
function applyTheme() {
ensure();
}
return { show, resize, hide, applyTheme };
})();
/** *************************** 按钮列 *********************************** */
class Columns {
constructor() {
this.columns = new Map();
for (let i = 1; i <= 5; i++) this.ensure(i);
}
ensure(index) {
if (this.columns.has(index)) return this.columns.get(index);
const offset = getLayoutOffset();
const left = CONFIG.baseLeft + offset + (index - 1) * CONFIG.columnGap;
const box = h('div', {
style: {
position: 'fixed',
top: CONFIG.buttonTop + 'px',
left: left + 'px',
width: CONFIG.columnWidth + 'px',
zIndex: 2147483646
}
});
document.body.appendChild(box);
this.columns.set(index, box);
return box;
}
addButton(index, label, onClick) {
const box = this.ensure(index);
const btn = h('button', { style: btnStyle(), title: label }, label);
btn.addEventListener('click', onClick);
box.appendChild(btn);
return btn;
}
}
function btnStyle() {
return {
display: 'block',
width: '100%',
height: 'var(--tmx-btn-h)',
marginTop: '6px',
color: 'var(--tmx-fg)',
background: 'var(--tmx-bg)',
border: '1px solid #999',
cursor: 'pointer'
};
}
/** *************************** Z-Index管理器 ******************************* */
const ZIndexManager = {
baseZIndex: 2147483647, // 最高基础层级
currentZIndex: 2147483647,
getNextZIndex() {
return ++this.currentZIndex;
},
// 确保元素在最上层
bringToTop(element) {
element.style.zIndex = this.getNextZIndex();
}
};
/** *************************** 组弹窗(支持 toggle 按钮) **************** */
class GroupPopup {
constructor(title) {
this.title = title;
// overlay covers full screen to allow click-outside-to-close
this.overlay = h('div', {
style: {
position: 'fixed',
inset: '0',
zIndex: 2147483645,
display: 'none',
background: 'rgba(0,0,0,0)',
pointerEvents: 'none' // 允许点击穿透到下层
}
});
// 添加关闭按钮到panel
const closeBtn = h('button', {
style: {
position: 'absolute',
top: '5px',
right: '5px',
width: '20px',
height: '20px',
border: 'none',
background: '#ff6b6b',
color: 'white',
borderRadius: '50%',
cursor: 'pointer',
fontSize: '12px',
lineHeight: '1'
}
}, '×');
closeBtn.addEventListener('click', () => this.hide());
// 为panel单独设置pointer-events
this.panelClickHandler = (e) => {
e.stopPropagation();
};
// 创建固定定位的wrapper
this.panelWrapper = h('div', {
style: {
position: 'fixed',
top: CONFIG.popTop + 'px',
left: getLayoutOffset() + 'px',
pointerEvents: 'auto'
}
});
this.panel = h('div', {
style: {
position: 'relative',
width: 'min(480px, calc(100vw - 20px))', // 5列按钮宽度,移动端不超出
padding: '10px 8px',
background: '#B2DFEE',
color: 'green',
textAlign: 'center',
border: '2px solid #ccc',
boxSizing: 'border-box'
}
});
this.panel.addEventListener('click', this.panelClickHandler);
// 添加关闭按钮到panel
this.panel.appendChild(closeBtn);
const titleBar = h('div', { style: { marginBottom: '6px', fontWeight: 'bold' } }, title);
this.btnWrap = h('div', {
style: {
display: 'flex',
flexWrap: 'wrap',
gap: '4px',
justifyContent: 'flex-start',
alignItems: 'flex-start',
minHeight: '40px'
}
});
this.panel.append(titleBar, this.btnWrap);
this.panelWrapper.appendChild(this.panel);
this.overlay.append(this.panelWrapper);
document.body.appendChild(this.overlay);
this.visible = false;
}
/**
* 添加按钮
* @param {string} label
* @param {Function} handler // will be called either as handler(btn) for toggles or handler() for normal
* @param {Object} options { isToggle:boolean, storeKey:string }
*/
addButton(label, handler, options = {}) {
const btn = h('button', {
style: Object.assign({}, btnStyle(), {
width: 'calc(20% - 3.2px)', // 每行5列,减去gap间距
minWidth: '60px',
maxWidth: '80px',
flex: '0 0 auto',
padding: '3px 4px',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
fontSize: '12px'
}),
title: label
}, label);
// apply current theme colors
btn.style.color = getComputedStyle(document.documentElement).getPropertyValue('--tmx-fg') || CONFIG.themes[0].fg;
btn.style.background = getComputedStyle(document.documentElement).getPropertyValue('--tmx-bg') || CONFIG.themes[0].bg;
if (options.isToggle && options.storeKey) {
// read initial state from store
let active = store.get(options.storeKey, 0) === 1;
btn.style.borderStyle = active ? 'inset' : 'outset';
// click toggles state, calls handler with (active, btn)
btn.addEventListener('click', (e) => {
e.stopPropagation();
active = !active;
btn.style.borderStyle = active ? 'inset' : 'outset';
try {
handler(active, btn);
} catch (err) {
console.error(err);
}
this.hide(); // collapse after click (保持原版体验)
});
} else {
btn.addEventListener('click', (e) => {
e.stopPropagation();
try {
handler(btn);
} catch (err) {
console.error(err);
}
this.hide();
});
}
this.btnWrap.appendChild(btn);
return btn;
}
show() {
this.overlay.style.display = '';
// 确保当前弹窗在最上层
ZIndexManager.bringToTop(this.overlay);
this.visible = true;
}
hide() {
this.overlay.style.display = 'none';
this.visible = false;
}
toggle() {
this.visible ? this.hide() : this.show();
}
}
/** *************************** 定时任务存储系统 ************************** */
const ScheduledTaskStorage = {
STORAGE_KEY: 'scheduled_tasks',
// 获取所有定时任务
getAll() {
try {
return store.get(this.STORAGE_KEY, []);
} catch (e) {
console.error('获取定时任务失败:', e);
return [];
}
},
// 保存定时任务
save(tasks) {
try {
store.set(this.STORAGE_KEY, tasks);
return true;
} catch (e) {
console.error('保存定时任务失败:', e);
return false;
}
},
// 添加定时任务
add(taskData) {
const tasks = this.getAll();
if (typeof taskData === 'string') {
// 兼容旧的调用方式
const [name, commandId, schedule] = arguments;
taskData = {
id: Date.now().toString(),
name: name,
commandId: commandId,
schedule: schedule,
enabled: true,
createTime: Date.now(),
lastRun: null,
nextRun: this.calculateNextRun(schedule)
};
} else {
// 新的调用方式,传入完整的任务对象
if (!taskData.nextRun) {
taskData.nextRun = this.calculateNextRun(taskData.schedule);
}
}
tasks.push(taskData);
return this.save(tasks) ? taskData : null;
},
// 删除定时任务
remove(id) {
const tasks = this.getAll();
const filtered = tasks.filter(task => task.id !== id);
return this.save(filtered);
},
// 更新定时任务
update(id, updates) {
const tasks = this.getAll();
const taskIndex = tasks.findIndex(task => task.id === id);
if (taskIndex !== -1) {
tasks[taskIndex] = { ...tasks[taskIndex], ...updates };
if (updates.schedule) {
tasks[taskIndex].nextRun = this.calculateNextRun(updates.schedule);
}
return this.save(tasks);
}
return false;
},
// 计算下次执行时间
calculateNextRun(schedule) {
const now = new Date();
const next = new Date(now);
switch (schedule.type) {
case 'interval':
next.setMinutes(next.getMinutes() + schedule.minutes);
break;
case 'daily':