-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserScript.js
More file actions
992 lines (882 loc) · 41.8 KB
/
UserScript.js
File metadata and controls
992 lines (882 loc) · 41.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
// ==UserScript==
// @name Better Web Animation 网页动画优化
// @namespace http://tampermonkey.net/
// @version 6.11
// @description 为所有网页的新加载、变化、移动和消失的内容提供可配置的平滑显现和动画效果,包括图片和元素位置、尺寸变化的平滑过渡。
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @grant GM_notification
// @license CC BY-NC 4.0
// ==/UserScript==
(function() {
'use strict';
// 多语言支持
const translations = {
en: {
settingsTitle: 'Animation Effect Settings',
fadeInDuration: 'Fade-in Duration (seconds):',
fadeOutDuration: 'Fade-out Duration (seconds):',
transitionDuration: 'Transition Duration (seconds):',
positionTransitionDuration: 'Position Transition Duration (seconds):',
sizeTransitionDuration: 'Size Transition Duration (seconds):',
animationTypes: 'Animation Types:',
fade: 'Fade',
zoom: 'Zoom',
rotate: 'Rotate',
slide: 'Slide',
excludedTags: 'Excluded Tags (separated by commas):',
observeAttributes: 'Observe Attribute Changes',
observeCharacterData: 'Observe Text Changes',
enableInitialFadeIn: 'Enable Initial Fade-in Effect',
complexityThreshold: 'Complexity Threshold (elements):',
mutationThreshold: 'Mutation Threshold (mutations/sec):',
enableComplexityDetection: 'Enable Complexity Detection',
enableMutationDetection: 'Enable Mutation Rate Detection',
saveConfig: 'Save Settings',
cancelConfig: 'Cancel',
settings: 'Settings',
animations: 'Animations',
enabled: 'Enabled',
disabled: 'Disabled',
animationPresets: 'Animation Presets:',
defaultPreset: 'Default',
gentlePreset: 'Gentle',
dynamicPreset: 'Dynamic',
customPreset: 'Custom',
enablePositionTransition: 'Enable Position Transition',
enableSizeTransition: 'Enable Size Transition',
enableImageFadeIn: 'Enable Image Fade-in',
detectFrequentChanges: 'Detect Frequent Changes',
changeThreshold: 'Change Threshold:',
detectionDuration: 'Detection Duration (ms):',
complexityThresholdLabel: 'Complexity Threshold (elements):',
mutationThresholdLabel: 'Mutation Threshold (mutations/sec):',
highComplexityDetected: 'High complexity detected. Animations have been disabled.',
highMutationRateDetected: 'High mutation rate detected. Animations have been disabled.',
},
zh: {
settingsTitle: '动画效果设置',
fadeInDuration: '渐显持续时间(秒):',
fadeOutDuration: '渐隐持续时间(秒):',
transitionDuration: '属性过渡持续时间(秒):',
positionTransitionDuration: '位置过渡持续时间(秒):',
sizeTransitionDuration: '尺寸过渡持续时间(秒):',
animationTypes: '动画类型:',
fade: '淡入/淡出(Fade)',
zoom: '缩放(Zoom)',
rotate: '旋转(Rotate)',
slide: '滑动(Slide)',
excludedTags: '排除的标签(用逗号分隔):',
observeAttributes: '观察属性变化',
observeCharacterData: '观察文本变化',
enableInitialFadeIn: '启用加载时的渐入效果',
complexityThreshold: '复杂度阈值(元素数量):',
mutationThreshold: '突变阈值(突变/秒):',
enableComplexityDetection: '启用复杂度检测',
enableMutationDetection: '启用突变率检测',
saveConfig: '保存设置',
cancelConfig: '取消',
settings: '设置',
animations: '动画',
enabled: '已启用',
disabled: '已禁用',
animationPresets: '动画预设:',
defaultPreset: '默认',
gentlePreset: '柔和',
dynamicPreset: '动感',
customPreset: '自定义',
enablePositionTransition: '启用位置过渡',
enableSizeTransition: '启用尺寸过渡',
enableImageFadeIn: '启用图片渐入',
detectFrequentChanges: '检测频繁变化',
changeThreshold: '变化阈值:',
detectionDuration: '检测持续时间(毫秒):',
complexityThresholdLabel: '复杂度阈值(元素数量):',
mutationThresholdLabel: '突变阈值(突变/秒):',
highComplexityDetected: '检测到高复杂度。动画已被禁用。',
highMutationRateDetected: '检测到高突变率。动画已被禁用。',
}
};
const userLang = navigator.language.startsWith('zh') ? 'zh' : 'en';
const t = translations[userLang];
// 仅在这些选择器匹配的元素上应用动画,减少全页面扫描
const TARGET_SELECTORS = 'img, video, p, div, li, section, article, main, aside, header, footer';
// 默认配置,确保所有配置项都有默认值
const defaultConfig = {
fadeInDuration: 0.5, // 渐显持续时间(秒)
fadeOutDuration: 0.5, // 渐隐持续时间(秒)
transitionDuration: 0.5, // 属性过渡持续时间(秒)
positionTransitionDuration: 0.5, // 位置过渡持续时间(秒)
sizeTransitionDuration: 0.5, // 尺寸过渡持续时间(秒)
animationTypes: ['fade'], // 动画类型:'fade', 'zoom', 'rotate', 'slide'
excludedTags: ['script'], // 排除的标签
observeAttributes: true, // 观察属性变化
observeCharacterData: true, // 观察文本变化
enableInitialFadeIn: true, // 启用加载时的渐入效果
complexityThreshold: 5000, // 复杂度阈值(元素数量)
mutationThreshold: 100, // 突变阈值(突变/秒)
enableComplexityDetection: true, // 启用复杂度检测
enableMutationDetection: true, // 启用突变率检测
animationPreset: 'default', // 动画预设
enablePositionTransition: true, // 启用位置过渡
enableSizeTransition: true, // 启用尺寸过渡
enableImageFadeIn: true, // 启用图片渐入
detectFrequentChanges: false, // 检测频繁变化(默认关闭)
changeThreshold: 50, // 变化阈值(默认值)
detectionDuration: 500, // 检测持续时间(毫秒)(默认值)
};
// 加载用户配置,确保所有配置项都有值
let userConfig = Object.assign({}, defaultConfig, GM_getValue('userConfig', {}));
// 动画启用状态
let animationsEnabled = true;
// 存储菜单命令的ID
let animationToggleMenuId = null;
let settingsMenuId = null;
// 添加菜单命令
settingsMenuId = GM_registerMenuCommand(t.settings, showConfigPanel);
// 添加动画开关菜单
updateAnimationMenuCommand();
function updateAnimationMenuCommand() {
const label = `${t.animations}: ${animationsEnabled ? t.enabled : t.disabled}`;
// 移除之前的动画开关菜单命令
if (animationToggleMenuId !== null) {
GM_unregisterMenuCommand(animationToggleMenuId);
}
// 注册新的动画开关菜单命令,并保存其ID
animationToggleMenuId = GM_registerMenuCommand(label, toggleAnimations);
}
function toggleAnimations() {
animationsEnabled = !animationsEnabled;
if (animationsEnabled) {
observer.disconnect(); // 先断开观察器,防止重复注册
startObserving();
applyAnimationsToExistingElements();
} else {
observer.disconnect();
}
updateAnimationMenuCommand();
}
// 检测复杂网页
function checkComplexity() {
if (!userConfig.enableComplexityDetection) return;
const totalElements = document.getElementsByTagName('*').length;
if (totalElements > userConfig.complexityThreshold) {
animationsEnabled = false;
console.warn('Animations have been disabled due to high complexity of the webpage.');
GM_notification(t.highComplexityDetected, 'Better Web Animation');
updateAnimationMenuCommand();
}
}
checkComplexity();
// 添加全局样式
function addGlobalStyles() {
// 移除之前的样式
const existingStyle = document.getElementById('global-animation-styles');
if (existingStyle) existingStyle.remove();
// 动态生成动画样式
let animations = '';
// 根据预设设置动画参数
switch (userConfig.animationPreset) {
case 'gentle':
userConfig.fadeInDuration = 1;
userConfig.fadeOutDuration = 1;
userConfig.transitionDuration = 1;
userConfig.positionTransitionDuration = 1;
userConfig.sizeTransitionDuration = 1;
break;
case 'dynamic':
userConfig.fadeInDuration = 0.3;
userConfig.fadeOutDuration = 0.3;
userConfig.transitionDuration = 0.3;
userConfig.positionTransitionDuration = 0.3;
userConfig.sizeTransitionDuration = 0.3;
break;
case 'custom':
// 保持用户自定义设置
break;
default:
// 默认设置
userConfig.fadeInDuration = defaultConfig.fadeInDuration;
userConfig.fadeOutDuration = defaultConfig.fadeOutDuration;
userConfig.transitionDuration = defaultConfig.transitionDuration;
userConfig.positionTransitionDuration = defaultConfig.positionTransitionDuration;
userConfig.sizeTransitionDuration = defaultConfig.sizeTransitionDuration;
break;
}
// 渐显效果
if (userConfig.animationTypes.includes('fade')) {
animations += `
.fade-in-effect {
animation: fadeIn ${userConfig.fadeInDuration}s forwards;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: var(--original-opacity, 1); }
}
`;
}
// 缩放效果
if (userConfig.animationTypes.includes('zoom')) {
animations += `
.zoom-in-effect {
animation: zoomIn ${userConfig.fadeInDuration}s forwards;
}
@keyframes zoomIn {
from { transform: scale(0); }
to { transform: scale(1); }
}
`;
}
// 旋转效果
if (userConfig.animationTypes.includes('rotate')) {
animations += `
.rotate-in-effect {
animation: rotateIn ${userConfig.fadeInDuration}s forwards;
}
@keyframes rotateIn {
from { transform: rotate(-360deg); }
to { transform: rotate(0deg); }
}
`;
}
// 滑动效果
if (userConfig.animationTypes.includes('slide')) {
animations += `
.slide-in-effect {
animation: slideIn ${userConfig.fadeInDuration}s forwards;
}
@keyframes slideIn {
from { transform: translateY(100%); }
to { transform: translateY(0); }
}
`;
}
// 属性变化过渡效果
if (userConfig.transitionDuration > 0) {
animations += `
.property-change-effect {
transition: all ${userConfig.transitionDuration}s ease-in-out;
}
`;
}
// 渐隐效果
if (userConfig.fadeOutDuration > 0) {
animations += `
.fade-out-effect {
animation: fadeOut ${userConfig.fadeOutDuration}s forwards;
}
@keyframes fadeOut {
from { opacity: var(--original-opacity, 1); }
to { opacity: 0; }
}
`;
}
// 位置过渡效果
if (userConfig.enablePositionTransition && userConfig.positionTransitionDuration > 0) {
animations += `
.position-transition-effect {
transition: transform ${userConfig.positionTransitionDuration}s ease-in-out;
}
`;
}
// 尺寸过渡效果
if (userConfig.enableSizeTransition && userConfig.sizeTransitionDuration > 0) {
animations += `
.size-transition-effect {
transition: width ${userConfig.sizeTransitionDuration}s ease-in-out, height ${userConfig.sizeTransitionDuration}s ease-in-out;
}
`;
}
// 图片渐入效果
if (userConfig.enableImageFadeIn) {
animations += `
img.image-fade-in-effect {
opacity: 0;
transition: opacity ${userConfig.fadeInDuration}s ease-in-out;
}
img.image-fade-in-effect.loaded {
opacity: 1;
}
`;
}
// 添加样式到页面
const style = document.createElement('style');
style.id = 'global-animation-styles';
style.textContent = animations;
document.head.appendChild(style);
}
addGlobalStyles();
// 页面加载时,为整个页面应用平滑显现效果
function applyInitialFadeIn() {
if (!userConfig.enableInitialFadeIn) return;
document.body.style.opacity = '0';
document.body.style.transition = `opacity ${userConfig.fadeInDuration}s`;
window.addEventListener('load', () => {
document.body.style.opacity = '';
});
}
applyInitialFadeIn();
// 检查元素是否可见(在视口内)并非固定定位
function isElementInViewport(element) {
const rect = element.getBoundingClientRect();
const style = window.getComputedStyle(element);
const isVisible = (
rect.width > 0 &&
rect.height > 0 &&
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth) &&
style.visibility !== 'hidden' &&
style.display !== 'none'
);
// 排除固定定位和粘性定位元素
const position = style.position;
const isFixedOrSticky = position === 'fixed' || position === 'sticky';
return isVisible && !isFixedOrSticky;
}
// 检查是否为要排除的特定元素(如Bilibili播放器)
let bilibiliExcludedElement = null;
function isBilibiliVideoPage() {
return window.location.href.startsWith('https://www.bilibili.com/video/');
}
if (isBilibiliVideoPage()) {
const xpath = '//*[@id="bilibili-player"]/div/div/div[1]/div[1]/div[4]';
const result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
bilibiliExcludedElement = result.singleNodeValue;
}
// 使用 WeakMap 记录元素的初始位置和尺寸
const elementRects = new WeakMap();
// 使用 WeakSet 跟踪正在动画的元素,避免递归触发
const animatingElements = new WeakSet();
// 标志位,指示是否正在滚动
let isScrolling = false;
let scrollTimeout;
// 监听滚动事件
window.addEventListener('scroll', () => {
isScrolling = true;
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(() => {
isScrolling = false;
}, 100); // 在用户停止滚动100毫秒后,重置标志位
}, { passive: true });
// 应用进入动画效果
function applyEnterAnimations(element) {
if (!animationsEnabled) return;
// 检查是否在排除列表中
if (userConfig.excludedTags.includes(element.tagName.toLowerCase())) return;
// 检查元素是否在视口内
if (!isElementInViewport(element)) return;
// 检查是否为要排除的特定元素
if (element === bilibiliExcludedElement) return;
// 检查是否正在动画中
if (animatingElements.has(element)) return;
// 检查初始透明度
const computedStyle = window.getComputedStyle(element);
const initialOpacity = computedStyle.opacity;
// 保存原始透明度
element.style.setProperty('--original-opacity', initialOpacity);
// 清除之前的动画类
element.classList.remove('fade-in-effect', 'zoom-in-effect', 'rotate-in-effect', 'slide-in-effect');
// 添加动画类
if (userConfig.animationTypes.includes('fade')) {
element.classList.add('fade-in-effect');
}
if (userConfig.animationTypes.includes('zoom')) {
element.classList.add('zoom-in-effect');
}
if (userConfig.animationTypes.includes('rotate')) {
element.classList.add('rotate-in-effect');
}
if (userConfig.animationTypes.includes('slide')) {
element.classList.add('slide-in-effect');
}
// 标记元素正在动画中
animatingElements.add(element);
// 监听动画结束,移除动画类和标记
function handleAnimationEnd() {
element.classList.remove('fade-in-effect', 'zoom-in-effect', 'rotate-in-effect', 'slide-in-effect');
element.style.removeProperty('--original-opacity');
animatingElements.delete(element);
element.removeEventListener('animationend', handleAnimationEnd);
}
element.addEventListener('animationend', handleAnimationEnd);
}
// 应用属性变化过渡效果
function applyTransitionEffect(element) {
if (!animationsEnabled) return;
// 检查是否在排除列表中
if (userConfig.excludedTags.includes(element.tagName.toLowerCase())) return;
// 检查元素是否在视口内
if (!isElementInViewport(element)) return;
// 检查是否为要排除的特定元素
if (element === bilibiliExcludedElement) return;
// 检查是否正在动画中
if (animatingElements.has(element)) return;
if (!element.classList.contains('property-change-effect')) {
element.classList.add('property-change-effect');
// 标记元素正在过渡中
animatingElements.add(element);
// 监听过渡结束,移除过渡类和标记
const removeTransitionClass = () => {
element.classList.remove('property-change-effect');
animatingElements.delete(element);
element.removeEventListener('transitionend', removeTransitionClass);
};
element.addEventListener('transitionend', removeTransitionClass);
}
}
// 应用位置和尺寸变化过渡效果
function applyPositionAndSizeTransition(changedElements) {
if (!animationsEnabled) return;
if (isScrolling) return; // 如果正在滚动,跳过位置和尺寸过渡
// 过滤不可见元素
const elements = changedElements.filter(element => isElementInViewport(element));
if (elements.length === 0) return;
// 在当前帧记录元素的初始位置和尺寸
elements.forEach(element => {
const rect = element.getBoundingClientRect();
elementRects.set(element, rect);
});
// 在下一帧中应用过渡效果
requestAnimationFrame(() => {
elements.forEach(element => {
// 检查是否正在动画中
if (animatingElements.has(element)) return;
// 获取元素的新位置和尺寸
const firstRect = elementRects.get(element);
const lastRect = element.getBoundingClientRect();
// 计算位置和尺寸变化
const deltaX = firstRect.left - lastRect.left;
const deltaY = firstRect.top - lastRect.top;
const deltaW = firstRect.width / lastRect.width;
const deltaH = firstRect.height / lastRect.height;
// 如果位置或尺寸发生变化
if (deltaX !== 0 || deltaY !== 0 || deltaW !== 1 || deltaH !== 1) {
// 标记元素正在动画中
animatingElements.add(element);
// 设置初始变换
element.style.transformOrigin = 'top left';
element.style.transform = `translate(${deltaX}px, ${deltaY}px) scale(${deltaW}, ${deltaH})`;
// 强制重绘
element.getBoundingClientRect();
// 应用过渡效果
const transitions = [];
if (userConfig.enablePositionTransition && userConfig.positionTransitionDuration > 0) {
transitions.push(`transform ${userConfig.positionTransitionDuration}s ease-in-out`);
}
if (userConfig.enableSizeTransition && userConfig.sizeTransitionDuration > 0) {
transitions.push(`width ${userConfig.sizeTransitionDuration}s ease-in-out`, `height ${userConfig.sizeTransitionDuration}s ease-in-out`);
}
element.style.transition = transitions.join(', ');
// 在下一帧中移除变换,触发过渡
requestAnimationFrame(() => {
element.style.transform = '';
if (userConfig.enableSizeTransition && userConfig.sizeTransitionDuration > 0) {
element.style.width = '';
element.style.height = '';
}
});
// 监听过渡结束,清除样式和标记
const handleTransitionEnd = () => {
element.style.transition = '';
element.style.transformOrigin = '';
animatingElements.delete(element);
element.removeEventListener('transitionend', handleTransitionEnd);
};
element.addEventListener('transitionend', handleTransitionEnd);
} else {
// 如果没有变化,清除记录
elementRects.delete(element);
}
});
});
}
// 应用离开动画效果
function applyExitAnimations(element) {
if (!animationsEnabled) return;
// 检查是否在排除列表中
if (userConfig.excludedTags.includes(element.tagName.toLowerCase())) return;
// 检查元素是否在视口内
if (!isElementInViewport(element)) return;
// 检查是否为要排除的特定元素
if (element === bilibiliExcludedElement) return;
// 检查是否正在动画中
if (animatingElements.has(element)) return;
// 如果元素已经有离开动画,直接返回
if (element.classList.contains('fade-out-effect')) return;
// 获取元素的原始透明度
const computedStyle = window.getComputedStyle(element);
const initialOpacity = computedStyle.opacity;
element.style.setProperty('--original-opacity', initialOpacity);
// 添加渐隐类
element.classList.add('fade-out-effect');
// 标记元素正在动画中
animatingElements.add(element);
// 在动画结束后,从DOM中移除元素并清除标记
function handleAnimationEnd() {
element.removeEventListener('animationend', handleAnimationEnd);
animatingElements.delete(element);
if (element.parentNode) {
element.parentNode.removeChild(element);
}
}
element.addEventListener('animationend', handleAnimationEnd);
}
// 应用图片渐入效果
function applyImageFadeIn(img) {
if (!animationsEnabled || !userConfig.enableImageFadeIn) return;
// 检查是否在排除列表中
if (userConfig.excludedTags.includes('img')) return;
if (!img.classList.contains('image-fade-in-effect')) {
img.classList.add('image-fade-in-effect');
if (img.complete && img.naturalWidth !== 0) {
img.classList.add('loaded');
} else {
img.addEventListener('load', () => {
img.classList.add('loaded');
});
img.addEventListener('error', () => {
img.classList.add('loaded');
});
}
}
}
// 应用动画到现有的所有元素
function applyAnimationsToExistingElements() {
// 只遍历常见的元素,避免对整个页面的所有节点进行扫描
document.querySelectorAll(TARGET_SELECTORS).forEach(element => {
applyEnterAnimations(element);
// 对所有图片应用图片渐入效果
if (element.tagName.toLowerCase() === 'img') {
applyImageFadeIn(element);
} else {
// 对子元素中的图片应用图片渐入效果
element.querySelectorAll('img').forEach(img => {
applyImageFadeIn(img);
});
}
});
}
// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(mutations => {
if (!animationsEnabled) return;
if (userConfig.enableMutationDetection) {
// 统计突变次数
mutationCount += mutations.length;
const now = Date.now();
const elapsed = now - mutationStartTime;
if (elapsed >= 1000) { // 1秒
const mutationsPerSecond = mutationCount / (elapsed / 1000);
mutationCount = 0;
mutationStartTime = now;
if (mutationsPerSecond > userConfig.mutationThreshold) {
animationsEnabled = false;
console.warn('Animations have been disabled due to high mutation rate.');
GM_notification(t.highMutationRateDetected, 'Better Web Animation');
updateAnimationMenuCommand();
observer.disconnect();
return;
}
}
}
// 使用 requestAnimationFrame 优化回调
requestAnimationFrame(() => {
const changedElements = new Set();
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
// 在节点被添加时应用进入动画
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.matches(TARGET_SELECTORS)) {
applyEnterAnimations(node);
if (node.tagName.toLowerCase() === 'img') {
applyImageFadeIn(node);
}
}
node.querySelectorAll(TARGET_SELECTORS).forEach(el => {
applyEnterAnimations(el);
if (el.tagName.toLowerCase() === 'img') {
applyImageFadeIn(el);
}
});
}
});
// 在节点被移除前应用离开动画
mutation.removedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.matches(TARGET_SELECTORS)) {
applyExitAnimations(node);
}
node.querySelectorAll(TARGET_SELECTORS).forEach(el => {
applyExitAnimations(el);
});
}
});
// 添加父节点到变化集合
if (mutation.target && mutation.target.nodeType === Node.ELEMENT_NODE) {
if (mutation.target.matches(TARGET_SELECTORS)) {
changedElements.add(mutation.target);
}
}
} else if ((mutation.type === 'attributes' && userConfig.observeAttributes) ||
(mutation.type === 'characterData' && userConfig.observeCharacterData)) {
const target = mutation.target;
if (target.nodeType === Node.ELEMENT_NODE && target.matches(TARGET_SELECTORS)) {
applyTransitionEffect(target);
changedElements.add(target);
// 如果是图片的 src 属性发生变化,重新应用图片渐入效果
if (target.tagName.toLowerCase() === 'img' && mutation.attributeName === 'src') {
applyImageFadeIn(target);
}
}
}
});
// 应用位置和尺寸过渡效果
if ((userConfig.enablePositionTransition || userConfig.enableSizeTransition) && changedElements.size > 0) {
applyPositionAndSizeTransition(Array.from(changedElements));
}
});
});
// 突变计数器
let mutationCount = 0;
let mutationStartTime = Date.now();
// 开始观察
function startObserving() {
observer.observe(document.body, {
childList: true,
attributes: userConfig.observeAttributes,
characterData: userConfig.observeCharacterData,
subtree: true,
attributeFilter: ['src', 'style', 'class'], // 观察属性变化,尤其是图片的'src'变化
});
}
if (animationsEnabled) {
startObserving();
applyAnimationsToExistingElements(); // 应用动画到现有元素
}
// 配置面板
function showConfigPanel() {
// 检查是否已存在配置面板
if (document.getElementById('animation-config-panel')) return;
// 创建遮罩层
const overlay = document.createElement('div');
overlay.id = 'animation-config-overlay';
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0,0,0,0.5)';
overlay.style.zIndex = '9998';
document.body.appendChild(overlay);
// 创建配置面板的HTML结构
const panel = document.createElement('div');
panel.id = 'animation-config-panel';
panel.style.position = 'fixed';
panel.style.top = '50%';
panel.style.left = '50%';
panel.style.transform = 'translate(-50%, -50%)';
panel.style.backgroundColor = '#fff';
panel.style.border = '1px solid #ccc';
panel.style.padding = '20px';
panel.style.zIndex = '9999';
panel.style.maxWidth = '600px';
panel.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
panel.style.overflowY = 'auto';
panel.style.maxHeight = '80%';
panel.innerHTML = `
<h2>${t.settingsTitle}</h2>
<label>
${t.animationPresets}
<select id="animationPreset">
<option value="default" ${userConfig.animationPreset === 'default' ? 'selected' : ''}>${t.defaultPreset}</option>
<option value="gentle" ${userConfig.animationPreset === 'gentle' ? 'selected' : ''}>${t.gentlePreset}</option>
<option value="dynamic" ${userConfig.animationPreset === 'dynamic' ? 'selected' : ''}>${t.dynamicPreset}</option>
<option value="custom" ${userConfig.animationPreset === 'custom' ? 'selected' : ''}>${t.customPreset}</option>
</select>
</label>
<br><br>
<div id="customSettings" style="display: ${userConfig.animationPreset === 'custom' ? 'block' : 'none'};">
<label>
${t.fadeInDuration}
<input type="number" id="fadeInDuration" value="${userConfig.fadeInDuration}" step="0.1" min="0">
</label>
<br>
<label>
${t.fadeOutDuration}
<input type="number" id="fadeOutDuration" value="${userConfig.fadeOutDuration}" step="0.1" min="0">
</label>
<br>
<label>
${t.transitionDuration}
<input type="number" id="transitionDuration" value="${userConfig.transitionDuration}" step="0.1" min="0">
</label>
<br>
<label>
${t.positionTransitionDuration}
<input type="number" id="positionTransitionDuration" value="${userConfig.positionTransitionDuration}" step="0.1" min="0">
</label>
<br>
<label>
${t.sizeTransitionDuration}
<input type="number" id="sizeTransitionDuration" value="${userConfig.sizeTransitionDuration}" step="0.1" min="0">
</label>
<br>
<label>
${t.changeThreshold}
<input type="number" id="changeThresholdConfig" value="${userConfig.changeThreshold}" min="1">
</label>
<br>
<label>
${t.detectionDuration}
<input type="number" id="detectionDurationConfig" value="${userConfig.detectionDuration}" min="100">
</label>
<br>
</div>
<label>
${t.animationTypes}
<br>
<input type="checkbox" id="animationFade" ${userConfig.animationTypes.includes('fade') ? 'checked' : ''}> ${t.fade}
<br>
<input type="checkbox" id="animationZoom" ${userConfig.animationTypes.includes('zoom') ? 'checked' : ''}> ${t.zoom}
<br>
<input type="checkbox" id="animationRotate" ${userConfig.animationTypes.includes('rotate') ? 'checked' : ''}> ${t.rotate}
<br>
<input type="checkbox" id="animationSlide" ${userConfig.animationTypes.includes('slide') ? 'checked' : ''}> ${t.slide}
</label>
<br>
<label>
${t.excludedTags}
<input type="text" id="excludedTags" value="${userConfig.excludedTags.join(',')}">
</label>
<br><br>
<label>
<input type="checkbox" id="observeAttributes" ${userConfig.observeAttributes ? 'checked' : ''}> ${t.observeAttributes}
</label>
<br>
<label>
<input type="checkbox" id="observeCharacterData" ${userConfig.observeCharacterData ? 'checked' : ''}> ${t.observeCharacterData}
</label>
<br><br>
<label>
<input type="checkbox" id="detectFrequentChanges" ${userConfig.detectFrequentChanges ? 'checked' : ''}> ${t.detectFrequentChanges}
</label>
<br>
<label>
${t.changeThreshold}
<input type="number" id="changeThresholdConfig" value="${userConfig.changeThreshold}" min="1">
</label>
<br>
<label>
${t.detectionDuration}
<input type="number" id="detectionDurationConfig" value="${userConfig.detectionDuration}" min="100">
</label>
<br><br>
<label>
<input type="checkbox" id="enableInitialFadeIn" ${userConfig.enableInitialFadeIn ? 'checked' : ''}> ${t.enableInitialFadeIn}
</label>
<br><br>
<label>
<input type="checkbox" id="enablePositionTransition" ${userConfig.enablePositionTransition ? 'checked' : ''}> ${t.enablePositionTransition}
</label>
<br>
<label>
<input type="checkbox" id="enableSizeTransition" ${userConfig.enableSizeTransition ? 'checked' : ''}> ${t.enableSizeTransition}
</label>
<br>
<label>
<input type="checkbox" id="enableImageFadeIn" ${userConfig.enableImageFadeIn ? 'checked' : ''}> ${t.enableImageFadeIn}
</label>
<br><br>
<label>
<input type="checkbox" id="enableComplexityDetection" ${userConfig.enableComplexityDetection ? 'checked' : ''}> ${t.enableComplexityDetection}
</label>
<br>
<label>
${t.complexityThresholdLabel}
<input type="number" id="complexityThreshold" value="${userConfig.complexityThreshold}" min="0">
</label>
<br><br>
<label>
<input type="checkbox" id="enableMutationDetection" ${userConfig.enableMutationDetection ? 'checked' : ''}> ${t.enableMutationDetection}
</label>
<br>
<label>
${t.mutationThresholdLabel}
<input type="number" id="mutationThreshold" value="${userConfig.mutationThreshold}" min="0">
</label>
<br><br>
<button id="saveConfig">${t.saveConfig}</button>
<button id="cancelConfig">${t.cancelConfig}</button>
`;
document.body.appendChild(panel);
// 根据预设切换自定义设置的显示
document.getElementById('animationPreset').addEventListener('change', (e) => {
const customSettings = document.getElementById('customSettings');
if (e.target.value === 'custom') {
customSettings.style.display = 'block';
} else {
customSettings.style.display = 'none';
}
});
// 添加事件监听
document.getElementById('saveConfig').addEventListener('click', () => {
// 保存配置
userConfig.animationPreset = document.getElementById('animationPreset').value;
if (userConfig.animationPreset === 'custom') {
userConfig.fadeInDuration = parseFloat(document.getElementById('fadeInDuration').value) || defaultConfig.fadeInDuration;
userConfig.fadeOutDuration = parseFloat(document.getElementById('fadeOutDuration').value) || defaultConfig.fadeOutDuration;
userConfig.transitionDuration = parseFloat(document.getElementById('transitionDuration').value) || defaultConfig.transitionDuration;
userConfig.positionTransitionDuration = parseFloat(document.getElementById('positionTransitionDuration').value) || defaultConfig.positionTransitionDuration;
userConfig.sizeTransitionDuration = parseFloat(document.getElementById('sizeTransitionDuration').value) || defaultConfig.sizeTransitionDuration;
userConfig.changeThreshold = parseInt(document.getElementById('changeThresholdConfig').value) || defaultConfig.changeThreshold;
userConfig.detectionDuration = parseInt(document.getElementById('detectionDurationConfig').value) || defaultConfig.detectionDuration;
}
const animationTypes = [];
if (document.getElementById('animationFade').checked) animationTypes.push('fade');
if (document.getElementById('animationZoom').checked) animationTypes.push('zoom');
if (document.getElementById('animationRotate').checked) animationTypes.push('rotate');
if (document.getElementById('animationSlide').checked) animationTypes.push('slide');
userConfig.animationTypes = animationTypes.length > 0 ? animationTypes : defaultConfig.animationTypes;
const excludedTags = document.getElementById('excludedTags').value.split(',').map(tag => tag.trim().toLowerCase()).filter(tag => tag);
userConfig.excludedTags = excludedTags.length > 0 ? excludedTags : defaultConfig.excludedTags;
userConfig.observeAttributes = document.getElementById('observeAttributes').checked;
userConfig.observeCharacterData = document.getElementById('observeCharacterData').checked;
userConfig.detectFrequentChanges = document.getElementById('detectFrequentChanges').checked;
userConfig.enableInitialFadeIn = document.getElementById('enableInitialFadeIn').checked;
userConfig.enablePositionTransition = document.getElementById('enablePositionTransition').checked;
userConfig.enableSizeTransition = document.getElementById('enableSizeTransition').checked;
userConfig.enableImageFadeIn = document.getElementById('enableImageFadeIn').checked;
userConfig.enableComplexityDetection = document.getElementById('enableComplexityDetection').checked;
userConfig.complexityThreshold = parseInt(document.getElementById('complexityThreshold').value) || defaultConfig.complexityThreshold;
userConfig.enableMutationDetection = document.getElementById('enableMutationDetection').checked;
userConfig.mutationThreshold = parseInt(document.getElementById('mutationThreshold').value) || defaultConfig.mutationThreshold;
// 保存到本地存储
GM_setValue('userConfig', userConfig);
// 更新样式和观察器
addGlobalStyles();
observer.disconnect();
if (animationsEnabled) {
startObserving();
applyAnimationsToExistingElements();
}
// 移除配置面板
panel.remove();
overlay.remove();
});
document.getElementById('cancelConfig').addEventListener('click', () => {
// 移除配置面板
panel.remove();
overlay.remove();
});
overlay.addEventListener('click', () => {
panel.remove();
overlay.remove();
});
}
})();