-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
1119 lines (1031 loc) · 39.8 KB
/
Copy pathcontent.js
File metadata and controls
1119 lines (1031 loc) · 39.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let words = [];
let currentWordData = null;
let clickCount = 0;
let showBoth = false;
let playAudioEnabled = true;
let fadeTime = 2; // 默认2秒
let wordLibrary = 'CET4.json'; // 默认词库
let currentWordIndex = 0; // 当前单词索引,用于顺序获取单词
let audioApi = 'https://dict.youdao.com/dictvoice?type=0&audio='; // 默认音频API
let wordHistory = []; // 单词历史记录
let historyIndex = -1; // 当前历史记录索引
let nextKey = ''; // 下一个单词按键
let prevKey = ''; // 上一个单词按键
let popwordEnabled = true; // 弹词功能是否开启(功能菜单中可关闭)
// 开发时设为 true 可输出日志;关闭可减少卡顿
const DEBUG = false;
function dbg(...args) { if (DEBUG) console.log(...args); }
// 防抖:合并频繁的 storage 写入,减轻卡顿
let savePositionTimer = null;
let saveWordMemoryQueue = [];
let saveWordMemoryTimer = null;
// Fisher–Yates 洗牌,原地打乱数组顺序
function shuffle(array) {
const arr = Array.isArray(array) ? array : [];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
// 加载单词库(加载后对词库随机打乱顺序)
async function loadWords() {
try {
// 默认使用word文件夹作为词库路径
const response = await fetch(chrome.runtime.getURL(`word/${wordLibrary}`));
words = await response.json();
shuffle(words); // 随机顺序
console.log('词库加载成功:', wordLibrary, '共', words.length, '个单词(已随机顺序)');
// 加载历史记录;随机顺序下不从存储恢复遍历位置,从打乱后的第 0 个开始
await loadHistory();
currentWordIndex = 0;
} catch (error) {
console.error('加载单词库失败:', error);
// 尝试加载可用的词库文件
try {
// 尝试加载CET4.json
const response = await fetch(chrome.runtime.getURL('word/CET4.json'));
words = await response.json();
shuffle(words);
currentWordIndex = 0;
wordLibrary = 'CET4.json'; // 更新词库名称
console.log('加载CET4.json成功(已随机顺序)');
// 加载历史记录,获取上次的遍历位置
await loadHistory();
} catch (error2) {
console.error('加载词库失败:', error2);
// 如果所有词库都加载失败,使用默认单词列表
words = [
{"word":"hello","translations":[{"translation":"你好","type":"int"}]},
{"word":"world","translations":[{"translation":"世界","type":"n"}]},
{"word":"apple","translations":[{"translation":"苹果","type":"n"}]},
{"word":"banana","translations":[{"translation":"香蕉","type":"n"}]},
{"word":"cat","translations":[{"translation":"猫","type":"n"}]}
];
shuffle(words);
currentWordIndex = 0;
console.log('使用默认单词列表');
}
}
}
// 加载历史记录
async function loadHistory() {
try {
// 获取当前词库名称(去掉.json后缀)
const libraryName = wordLibrary.replace('.json', '');
const storagePositionKey = `wordPosition_${libraryName}`;
const storageHistoryKey = `wordHistory_${libraryName}`;
console.log('尝试加载历史记录,词库:', libraryName);
// 从Chrome存储中获取遍历位置和历史记录
return new Promise((resolve) => {
chrome.storage.local.get([storagePositionKey, storageHistoryKey], function(result) {
if (result[storagePositionKey] !== undefined) {
currentWordIndex = result[storagePositionKey];
console.log('从存储中加载遍历位置:', currentWordIndex);
} else {
console.log('没有找到历史遍历位置,从0开始');
currentWordIndex = 0;
}
if (result[storageHistoryKey] !== undefined) {
// 从存储中加载历史记录,并转换为完整的单词对象结构
wordHistory = result[storageHistoryKey].map(record => {
// 如果记录已经是完整的单词对象(包含translations),则直接使用
if (record.translations) {
return record;
}
// 否则,将简化记录转换为完整的单词对象结构
return {
word: record.word,
translations: [{
translation: record.meaning || '',
type: record.type || ''
}],
// 保留原始日期
date: record.date
};
});
historyIndex = wordHistory.length - 1;
console.log('从存储中加载历史记录,长度:', wordHistory.length);
} else {
console.log('没有找到历史记录,初始化空数组');
wordHistory = [];
historyIndex = -1;
}
resolve();
});
});
} catch (error) {
console.error('加载历史记录失败:', error);
currentWordIndex = 0;
wordHistory = [];
historyIndex = -1;
return Promise.resolve();
}
}
// 防抖保存遍历位置(约 500ms 内多次只写一次)
function scheduleSavePosition() {
if (savePositionTimer) clearTimeout(savePositionTimer);
savePositionTimer = setTimeout(() => {
savePositionTimer = null;
savePosition();
}, 500);
}
// 保存遍历位置和历史记录
function savePosition() {
try {
// 获取当前词库名称(去掉.json后缀)
const libraryName = wordLibrary.replace('.json', '');
const storagePositionKey = `wordPosition_${libraryName}`;
const storageHistoryKey = `wordHistory_${libraryName}`;
// 保存当前遍历位置和历史记录到Chrome存储
chrome.storage.local.set({
[storagePositionKey]: currentWordIndex,
[storageHistoryKey]: wordHistory
}, function() {
console.log('遍历位置保存成功:', currentWordIndex);
console.log('历史记录保存成功,长度:', wordHistory.length);
});
} catch (error) {
console.error('保存遍历位置和历史记录失败:', error);
}
}
// 加载设置
function loadSettings() {
return new Promise((resolve) => {
chrome.storage.sync.get(['showBoth', 'playAudio', 'fadeTime', 'wordLibrary', 'audioApi', 'nextKey', 'prevKey', 'popwordEnabled'], function(data) {
showBoth = data.showBoth || false;
playAudioEnabled = data.playAudio !== false; // 默认开启
fadeTime = data.fadeTime || 2; // 默认2秒
wordLibrary = data.wordLibrary || 'CET4.json'; // 默认词库
audioApi = data.audioApi || 'https://dict.youdao.com/dictvoice?type=0&audio='; // 默认音频API
nextKey = data.nextKey || '鼠标左键'; // 下一个单词按键,默认鼠标左键
prevKey = data.prevKey || '鼠标右键'; // 上一个单词按键,默认鼠标右键
popwordEnabled = data.popwordEnabled !== false; // 默认开启弹词
resolve();
});
});
}
// 顺序获取一个单词
function getRandomWord() {
if (words.length === 0) return null;
const word = words[currentWordIndex];
console.log('获取单词:', word.word, '当前索引:', currentWordIndex);
// 递增索引,循环使用
currentWordIndex = (currentWordIndex + 1) % words.length;
console.log('下一个索引:', currentWordIndex);
// 保存单词记忆记录(防抖,避免每次点击都写 storage)
saveWordMemory(word);
// 保存遍历位置(防抖)
scheduleSavePosition();
return word;
}
// 保存单词记忆记录
function saveWordMemory(wordData) {
if (!wordData || !wordData.word) {
console.log('单词数据为空,无法保存记忆记录');
return;
}
try {
// 获取当前词库名称(去掉.json后缀)
const libraryName = wordLibrary.replace('.json', '');
// 从translations中获取中文意思和词性
const meaning = wordData.translations && wordData.translations.length > 0 ? wordData.translations[0].translation : '';
const type = wordData.translations && wordData.translations.length > 0 ? wordData.translations[0].type : '';
// 创建记忆记录
const memoryRecord = {
word: wordData.word,
type: type,
meaning: meaning,
date: new Date().toISOString()
};
console.log('准备保存记忆记录:', memoryRecord);
// 入队后防抖批量写入,减少 storage 写入次数
saveWordMemoryQueue.push({ storageKey: `wordHistory_${libraryName}`, record: memoryRecord });
if (saveWordMemoryTimer) clearTimeout(saveWordMemoryTimer);
saveWordMemoryTimer = setTimeout(flushWordMemoryQueue, 800);
} catch (error) {
console.error('保存单词记忆记录时发生错误:', error);
}
}
// 防抖批量写入记忆记录,减少卡顿
function flushWordMemoryQueue() {
saveWordMemoryTimer = null;
if (saveWordMemoryQueue.length === 0) return;
const byKey = new Map();
for (const { storageKey, record } of saveWordMemoryQueue) {
if (!byKey.has(storageKey)) byKey.set(storageKey, []);
byKey.get(storageKey).push(record);
}
saveWordMemoryQueue = [];
chrome.storage.local.get(Array.from(byKey.keys()), (result) => {
const toSet = {};
for (const [storageKey, records] of byKey) {
const history = (result[storageKey] || []).concat(records);
toSet[storageKey] = history.length > 1000 ? history.slice(-1000) : history;
}
chrome.storage.local.set(toSet, () => { dbg('单词记忆记录批量保存成功'); });
});
}
// 页面卸载前务必落盘,避免丢失进度
window.addEventListener('beforeunload', () => {
if (savePositionTimer) { clearTimeout(savePositionTimer); savePosition(); }
flushWordMemoryQueue();
});
// 显示单词或中文意思
function showWordEffect(x, y, direction = 'next') {
console.log('点击事件触发,playAudioEnabled:', playAudioEnabled, 'showBoth:', showBoth, 'direction:', direction);
console.log('点击位置:', x, y);
console.log('单词库大小:', words.length);
console.log('当前历史记录索引:', historyIndex);
console.log('历史记录长度:', wordHistory.length);
// 移除旧的元素
const oldElements = document.querySelectorAll('.word-effect');
console.log('移除旧元素数量:', oldElements.length);
oldElements.forEach(el => el.remove());
if (showBoth) {
// 如果设置为同时显示单词和中文意思
if (direction === 'prev') {
// 右键点击,显示上一个单词
if (historyIndex > 0) {
historyIndex--;
currentWordData = wordHistory[historyIndex];
console.log('获取上一个单词:', currentWordData.word);
// 播放音频(如果开启)
if (playAudioEnabled) {
console.log('准备播放音频');
playAudio(null, currentWordData.word);
} else {
console.log('音频播放已禁用');
}
// 处理不同结构的单词数据
let meaning, type;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
meaning = currentWordData.translations[0].translation;
type = currentWordData.translations[0].type;
} else {
// 简化的记录,直接使用type和meaning属性
meaning = currentWordData.meaning || '';
type = currentWordData.type || '';
}
const wordWithType = type ? `${currentWordData.word} (${type})` : currentWordData.word;
const text = `${wordWithType}\n${meaning}`;
console.log('创建浮动元素,文本:', text);
createFloatingElement(text, x, y, 'word-with-meaning');
} else {
console.log('已经是第一个单词');
}
} else {
// 左键点击,显示下一个单词
if (historyIndex < wordHistory.length - 1) {
// 如果不是在历史记录的末尾,显示历史记录中的下一个单词
historyIndex++;
currentWordData = wordHistory[historyIndex];
console.log('获取历史记录中的下一个单词:', currentWordData.word);
} else {
// 如果在历史记录的末尾,生成新单词
currentWordData = getRandomWord();
if (currentWordData) {
console.log('获取到新单词:', currentWordData.word);
// 添加到历史记录
wordHistory.push(currentWordData);
historyIndex++;
// 限制历史记录长度,避免内存占用过大
if (wordHistory.length > 100) {
wordHistory.shift();
historyIndex--;
}
// 保存单词记忆记录
saveWordMemory(currentWordData);
}
}
if (currentWordData) {
// 播放音频(如果开启)
if (playAudioEnabled) {
console.log('准备播放音频');
playAudio(null, currentWordData.word);
} else {
console.log('音频播放已禁用');
}
// 处理不同结构的单词数据
let meaning, type;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
meaning = currentWordData.translations[0].translation;
type = currentWordData.translations[0].type;
} else {
// 简化的记录,直接使用type和meaning属性
meaning = currentWordData.meaning || '';
type = currentWordData.type || '';
}
const wordWithType = type ? `${currentWordData.word} (${type})` : currentWordData.word;
const text = `${wordWithType}\n${meaning}`;
console.log('创建浮动元素,文本:', text);
createFloatingElement(text, x, y, 'word-with-meaning');
} else {
console.log('未获取到单词数据');
}
}
} else {
// 如果设置为分开显示单词和中文意思
clickCount++;
console.log('当前点击次数:', clickCount);
if (direction === 'prev') {
// 右键点击,显示上一个单词
if (historyIndex > 0) {
historyIndex--;
currentWordData = wordHistory[historyIndex];
console.log('获取上一个单词:', currentWordData.word);
if (clickCount % 2 === 1) {
// 右键第一下,显示上一个英文
// 播放音频(如果开启)
if (playAudioEnabled) {
console.log('准备播放音频');
playAudio(null, currentWordData.word);
} else {
console.log('音频播放已禁用');
}
// 处理不同结构的单词数据
let type;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
type = currentWordData.translations[0].type;
} else {
// 简化的记录,直接使用type属性
type = currentWordData.type || '';
}
const wordWithType = type ? `${currentWordData.word} (${type})` : currentWordData.word;
console.log('创建浮动元素,文本:', wordWithType);
createFloatingElement(wordWithType, x, y, 'word');
} else {
// 右键第二下,显示上一个的中文
// 处理不同结构的单词数据
let meaning;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
meaning = currentWordData.translations[0].translation;
} else {
// 简化的记录,直接使用meaning属性
meaning = currentWordData.meaning || '';
}
console.log('显示中文意思:', meaning);
console.log('创建浮动元素,文本:', meaning);
createFloatingElement(meaning, x, y, 'meaning');
}
} else {
console.log('已经是第一个单词');
}
} else {
// 左键点击
if (clickCount % 2 === 1) {
// 左键第一下,显示英文
if (historyIndex < wordHistory.length - 1) {
// 如果不是在历史记录的末尾,显示历史记录中的下一个单词
historyIndex++;
currentWordData = wordHistory[historyIndex];
console.log('获取历史记录中的下一个单词:', currentWordData.word);
} else {
// 如果在历史记录的末尾,生成新单词
currentWordData = getRandomWord();
if (currentWordData) {
console.log('获取到新单词:', currentWordData.word);
// 添加到历史记录
wordHistory.push(currentWordData);
historyIndex++;
// 限制历史记录长度,避免内存占用过大
if (wordHistory.length > 100) {
wordHistory.shift();
historyIndex--;
}
// 保存单词记忆记录
saveWordMemory(currentWordData);
}
}
if (currentWordData) {
// 播放音频(如果开启)
if (playAudioEnabled) {
console.log('准备播放音频');
playAudio(null, currentWordData.word);
} else {
console.log('音频播放已禁用');
}
// 处理不同结构的单词数据
let type;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
type = currentWordData.translations[0].type;
} else {
// 简化的记录,直接使用type属性
type = currentWordData.type || '';
}
const wordWithType = type ? `${currentWordData.word} (${type})` : currentWordData.word;
console.log('创建浮动元素,文本:', wordWithType);
createFloatingElement(wordWithType, x, y, 'word');
} else {
console.log('未获取到单词数据');
}
} else {
// 左键第二下,显示中文
if (currentWordData) {
// 处理不同结构的单词数据
let meaning;
if (currentWordData.translations && currentWordData.translations.length > 0) {
// 完整的单词对象,从translations中获取
meaning = currentWordData.translations[0].translation;
} else {
// 简化的记录,直接使用meaning属性
meaning = currentWordData.meaning || '';
}
console.log('显示中文意思:', meaning);
console.log('创建浮动元素,文本:', meaning);
createFloatingElement(meaning, x, y, 'meaning');
} else {
console.log('当前没有单词数据');
}
}
}
}
}
// 创建浮动元素
function createFloatingElement(text, x, y, type) {
console.log('开始创建浮动元素');
console.log('文本:', text);
console.log('位置:', x, y);
console.log('类型:', type);
const element = document.createElement('div');
element.className = 'word-effect';
element.textContent = text;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.classList.add(type);
// 确保元素显示在最上层,使用!important覆盖所有样式
element.style.zIndex = '999999 !important';
element.style.position = 'fixed !important';
element.style.pointerEvents = 'none !important';
element.style.fontSize = '16px !important';
element.style.fontWeight = 'bold !important';
element.style.color = '#333 !important';
element.style.backgroundColor = 'rgba(255, 255, 255, 0.9) !important';
element.style.padding = '8px 12px !important';
element.style.borderRadius = '4px !important';
element.style.boxShadow = '0 2px 8px rgba(0, 0, 0, 0.1) !important';
element.style.opacity = '1 !important'; // 确保初始状态是可见的
element.style.transform = 'translateY(0) !important'; // 确保初始位置正确
element.style.fontFamily = 'Arial, sans-serif !important';
element.style.whiteSpace = 'pre-line !important';
element.style.textAlign = 'center !important';
element.style.margin = '0 !important';
element.style.border = 'none !important';
element.style.outline = 'none !important';
element.style.textDecoration = 'none !important';
element.style.overflow = 'visible !important';
element.style.display = 'block !important';
element.style.width = 'auto !important';
element.style.height = 'auto !important';
// 动态设置过渡动画时间,与fadeTime相匹配
const transitionTime = (fadeTime - 0.2).toFixed(1); // 留出0.2秒的启动延迟
element.style.transition = `opacity ${transitionTime}s ease, transform ${transitionTime}s ease !important`;
console.log('元素创建完成,准备添加到DOM');
console.log('元素样式:', element.style.cssText);
try {
document.body.appendChild(element);
console.log('元素已添加到DOM');
console.log('DOM中.word-effect元素数量:', document.querySelectorAll('.word-effect').length);
// 强制重排,确保元素立即显示
element.offsetHeight;
console.log('元素重排完成');
} catch (error) {
console.error('添加元素到DOM失败:', error);
}
// 添加动画效果
setTimeout(() => {
console.log('开始动画效果');
try {
element.style.opacity = '0 !important';
element.style.transform = 'translateY(-100px) !important';
} catch (error) {
console.error('设置动画效果失败:', error);
}
}, 100);
// 动画结束后移除元素
setTimeout(() => {
console.log('移除元素');
try {
element.remove();
console.log('元素已移除,DOM中.word-effect元素数量:', document.querySelectorAll('.word-effect').length);
} catch (error) {
console.error('移除元素失败:', error);
}
}, fadeTime * 1000);
}
// 音频缓存配置
const CACHE_SIZE = 15; // 最大缓存数量
const CACHE_THRESHOLD = 5; // 触发缓存的阈值
const INITIAL_CACHE_COUNT = 10; // 初始缓存数量
const audioCache = new Map();
let cachedWordIndices = new Set(); // 已缓存的单词索引
let usedWordCount = 0; // 已使用的单词数量
let usedWords = []; // 已使用的单词
// 播放音频
function playAudio(audioUrl, word) {
console.log('尝试播放音频:', audioUrl, '单词:', word);
console.log('当前audioApi:', audioApi);
// 如果提供了完整的音频URL,直接使用
if (audioUrl) {
console.log('使用提供的音频URL:', audioUrl);
playAudioWithYoudao(audioUrl, word);
} else if (word) {
// 使用用户指定的音频API构建URL
const constructedUrl = audioApi + encodeURIComponent(word);
console.log('构建的音频URL:', constructedUrl);
playAudioWithYoudao(constructedUrl, word);
} else {
console.error('播放音频失败:单词为空');
}
}
// 使用用户指定的音频API播放音频
function playAudioWithYoudao(audioUrl, word) {
console.log('尝试播放音频:', audioUrl, '单词:', word);
// 检查缓存中是否已有音频
const cacheKey = `audio_${word}`;
if (audioCache.has(cacheKey)) {
const audio = audioCache.get(cacheKey);
audio.currentTime = 0;
audio.play().then(() => {
console.log('音频播放成功(缓存)');
// 增加使用计数
usedWordCount++;
// 记录已使用的单词
usedWords.push(word);
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
}).catch(error => {
console.error('播放音频失败(缓存):', error);
// 缓存失败时尝试重新加载
loadAndPlayYoudaoAudio(audioUrl, word);
});
} else {
// 首次加载音频
loadAndPlayYoudaoAudio(audioUrl, word);
}
}
// 加载并播放音频
function loadAndPlayYoudaoAudio(audioUrl, word) {
console.log('加载并播放音频:', audioUrl, '单词:', word);
try {
// 验证音频URL
if (!audioUrl) {
console.error('音频URL为空');
return;
}
const audio = new Audio(audioUrl);
audio.preload = 'auto';
audio.volume = 1.0;
// 添加到缓存
const cacheKey = `audio_${word}`;
audioCache.set(cacheKey, audio);
// 尝试播放音频
audio.play().then(() => {
console.log('音频播放成功:', word);
// 增加使用计数
usedWordCount++;
// 记录已使用的单词
usedWords.push(word);
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
}).catch(error => {
console.error('音频播放失败:', error);
// 失败时尝试使用浏览器内置的Web Speech API
if ('speechSynthesis' in window) {
try {
const speech = new SpeechSynthesisUtterance(word);
speech.lang = 'en-US';
speech.rate = 1.0;
speech.pitch = 1.0;
speech.volume = 1.0;
// 播放TTS
window.speechSynthesis.speak(speech);
console.log('Web Speech API播放成功:', word);
// 增加使用计数
usedWordCount++;
// 记录已使用的单词
usedWords.push(word);
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
} catch (error) {
console.error('Web Speech API播放失败:', error);
}
} else {
console.log('浏览器不支持Web Speech API');
}
});
} catch (error) {
console.error('加载音频失败:', error);
// 尝试使用备用TTS方法
if (word) {
playAudioWithTTS(word);
}
}
}
// 使用国内可用的TTS服务播放音频(备用)
function playAudioWithTTS(word) {
console.log('尝试使用TTS播放单词:', word);
// 首先尝试使用浏览器内置的Web Speech API
if ('speechSynthesis' in window) {
try {
// 检查是否有可用的语音合成器
if (window.speechSynthesis.getVoices().length === 0) {
// 如果没有可用的语音,等待voiceschanged事件
window.speechSynthesis.onvoiceschanged = function() {
if (window.speechSynthesis.getVoices().length > 0) {
playSpeechSynthesis(word);
} else {
console.error('没有可用的语音合成器');
// 尝试使用百度TTS
tryBaiduTTS(word);
}
};
} else {
// 直接播放
playSpeechSynthesis(word);
}
} catch (error) {
console.error('Web Speech API播放失败:', error);
// 尝试使用百度TTS
tryBaiduTTS(word);
}
} else {
console.log('浏览器不支持Web Speech API');
// 尝试使用百度TTS
tryBaiduTTS(word);
}
// 播放Web Speech API
function playSpeechSynthesis(word) {
try {
const speech = new SpeechSynthesisUtterance(word);
speech.lang = 'en-US';
speech.rate = 1.0;
speech.pitch = 1.0;
speech.volume = 1.0;
// 播放TTS
window.speechSynthesis.speak(speech);
console.log('Web Speech API播放成功:', word);
// 增加使用计数
usedWordCount++;
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
} catch (error) {
console.error('Web Speech API播放失败:', error);
// 尝试使用百度TTS
tryBaiduTTS(word);
}
}
// 尝试使用百度TTS
function tryBaiduTTS(word) {
try {
// 使用百度TTS API(无需API key的公共接口)
const ttsUrl = `https://tts.baidu.com/text2audio?lan=en&ie=UTF-8&spd=5&text=${encodeURIComponent(word)}`;
const audio = new Audio(ttsUrl);
audio.play().then(() => {
console.log('百度TTS播放成功:', word);
// 增加使用计数
usedWordCount++;
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
}).catch(error => {
console.error('百度TTS播放失败:', error);
});
} catch (error) {
console.error('TTS播放失败:', error);
}
}
}
// 加载并播放音频(兼容旧接口)
function loadAndPlayAudio(audioUrl) {
// 创建音频对象
const audio = new Audio(audioUrl);
audio.preload = 'auto';
audio.volume = 1.0;
// 添加到缓存
audioCache.set(audioUrl, audio);
// 尝试播放音频
audio.play().then(() => {
console.log('音频播放成功(新加载)');
// 增加使用计数
usedWordCount++;
// 检查是否需要缓存更多音频
checkAndCacheMoreAudio();
}).catch(error => {
console.error('播放音频失败(新加载):', error);
// 失败时使用TTS
const word = audioUrl.split('/').pop().split('-')[0];
playAudioWithTTS(word);
});
}
// 发送缓存更新消息
function sendCacheUpdate() {
// 更新缓存大小到存储
chrome.storage.local.set({ cacheSize: audioCache.size }, function() {
console.log('缓存大小已更新到存储:', audioCache.size);
});
}
// 检查并缓存更多音频
function checkAndCacheMoreAudio() {
// 当使用了CACHE_THRESHOLD个单词后,缓存更多
if (usedWordCount % CACHE_THRESHOLD === 0) {
console.log(`已使用${usedWordCount}个单词,开始缓存更多音频`);
// 清除已使用的单词缓存
if (usedWords.length >= CACHE_THRESHOLD) {
const wordsToRemove = usedWords.splice(0, CACHE_THRESHOLD);
wordsToRemove.forEach(word => {
const cacheKey = `audio_${word}`;
if (audioCache.has(cacheKey)) {
audioCache.delete(cacheKey);
console.log(`清除缓存: ${word}`);
}
// 从cachedWordIndices中移除对应的索引
// 查找单词对应的索引并移除
const wordIndex = words.findIndex(w => w.word === word);
if (wordIndex !== -1) {
cachedWordIndices.delete(wordIndex);
}
});
// 发送缓存更新消息
sendCacheUpdate();
}
// 缓存更多音频
cacheMoreAudio();
}
}
// 缓存更多音频
function cacheMoreAudio() {
// 最多缓存CACHE_SIZE个单词
if (cachedWordIndices.size >= CACHE_SIZE) {
console.log('缓存已达到最大容量');
return;
}
// 计算需要缓存的数量
const needToCache = Math.min(CACHE_THRESHOLD, CACHE_SIZE - cachedWordIndices.size);
// 错峰创建 Audio,避免一帧内大量创建导致卡顿
setTimeout(() => {
let cachedCount = 0;
const STAGGER_MS = 40;
const tryCacheOne = () => {
if (cachedCount >= needToCache || cachedWordIndices.size >= words.length) {
dbg(`已缓存${cachedWordIndices.size}/${CACHE_SIZE}个单词的音频`);
sendCacheUpdate();
return;
}
const randomIndex = Math.floor(Math.random() * words.length);
if (!cachedWordIndices.has(randomIndex)) {
const wordData = words[randomIndex];
if (wordData && wordData.word) {
const audioUrl = audioApi + encodeURIComponent(wordData.word);
const audio = new Audio(audioUrl);
audio.preload = 'auto';
audioCache.set(`audio_${wordData.word}`, audio);
cachedWordIndices.add(randomIndex);
cachedCount++;
dbg(`预缓存单词: ${wordData.word}`);
}
}
setTimeout(tryCacheOne, STAGGER_MS);
};
tryCacheOne();
}, 50);
}
// 初始化缓存(错峰创建 Audio,避免一帧内大量创建导致卡顿)
function initAudioCache() {
dbg('开始初始化音频缓存');
const STAGGER_MS = 40; // 每个音频间隔,避免主线程阻塞
setTimeout(() => {
let cachedCount = 0;
const tryCacheOne = () => {
if (cachedCount >= INITIAL_CACHE_COUNT || cachedWordIndices.size >= words.length) {
dbg(`初始化完成,已缓存${cachedWordIndices.size}个单词的音频`);
sendCacheUpdate();
return;
}
const randomIndex = Math.floor(Math.random() * words.length);
if (!cachedWordIndices.has(randomIndex)) {
const wordData = words[randomIndex];
if (wordData && wordData.word) {
const audioUrl = audioApi + encodeURIComponent(wordData.word);
const audio = new Audio(audioUrl);
audio.preload = 'auto';
audioCache.set(`audio_${wordData.word}`, audio);
cachedWordIndices.add(randomIndex);
cachedCount++;
dbg(`预缓存单词: ${wordData.word}`);
}
}
setTimeout(tryCacheOne, STAGGER_MS);
};
tryCacheOne();
}, 100);
}
// 监听鼠标点击事件
function setupClickListener() {
console.log('设置点击事件监听器');
// 使用捕获阶段的事件监听,确保在B站首页也能捕获到点击事件
document.addEventListener('click', (e) => {
if (!popwordEnabled) return;
// 只有当nextKey设置为"鼠标左键"时,才启用鼠标左键触发
if (nextKey !== '鼠标左键') {
return;
}
console.log('点击事件捕获,目标:', e.target.tagName);
console.log('目标类名:', e.target.className);
console.log('目标ID:', e.target.id);
// 避免在输入框、按钮等元素上触发
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'BUTTON') {
console.log('点击目标是输入框/文本域/按钮,跳过');
return;
}
// 避免在B站的特殊元素上触发,如播放器、导航栏等
if (e.target.closest('.bilibili-player') || e.target.closest('.nav-menu') || e.target.closest('.header')) {
console.log('点击目标是B站特殊元素,跳过');
return;
}
console.log('触发showWordEffect函数(左键,下一个单词)');
showWordEffect(e.clientX, e.clientY, 'next');
}, true); // 使用捕获阶段
// 添加右键点击事件监听
document.addEventListener('contextmenu', (e) => {
if (!popwordEnabled) return;
// 只有当prevKey设置为"鼠标右键"时,才启用鼠标右键触发
if (prevKey !== '鼠标右键') {
return;
}
console.log('右键点击事件捕获,目标:', e.target.tagName);
// 避免在输入框、按钮等元素上触发
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'BUTTON') {
console.log('点击目标是输入框/文本域/按钮,跳过');
return;
}
// 避免在B站的特殊元素上触发,如播放器、导航栏等
if (e.target.closest('.bilibili-player') || e.target.closest('.nav-menu') || e.target.closest('.header')) {
console.log('点击目标是B站特殊元素,跳过');
return;
}
// 阻止默认右键菜单
e.preventDefault();
console.log('触发showWordEffect函数(右键,上一个单词)');
showWordEffect(e.clientX, e.clientY, 'prev');
}, true); // 使用捕获阶段
// 添加键盘事件监听
document.addEventListener('keydown', (e) => {
if (!popwordEnabled) return;
// 避免在输入框中触发
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
return;
}
// 检查是否按下了自定义的下一个单词按键
if (nextKey && e.key === nextKey) {
console.log('按下了下一个单词按键:', nextKey);
// 获取鼠标当前位置作为显示位置
const x = window.innerWidth / 2;
const y = window.innerHeight / 2;
showWordEffect(x, y, 'next');
}
// 检查是否按下了自定义的上一个单词按键
if (prevKey && e.key === prevKey) {
console.log('按下了上一个单词按键:', prevKey);
// 获取鼠标当前位置作为显示位置
const x = window.innerWidth / 2;
const y = window.innerHeight / 2;
showWordEffect(x, y, 'prev');
}
}, true); // 使用捕获阶段
console.log('点击事件监听器设置完成(使用捕获阶段)');
}
// 初始化
async function init() {
console.log('开始初始化插件');
console.log('加载单词库');
await loadWords();
console.log('单词库加载完成,大小:', words.length);
console.log('加载设置');
await loadSettings();
console.log('设置加载完成,showBoth:', showBoth, 'playAudioEnabled:', playAudioEnabled, 'fadeTime:', fadeTime);
console.log('设置点击事件监听器');
setupClickListener();
// 初始化音频缓存
console.log('初始化音频缓存');
initAudioCache();