-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
963 lines (828 loc) · 31.9 KB
/
content.js
File metadata and controls
963 lines (828 loc) · 31.9 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
/**
* content.js — V4 with LangDB + Kuromoji + CSS Custom Highlight API
*
* Differences from V3:
* - V3: Particles, colors, and tooltips hardcoded in Portuguese.
* - V4: Everything comes from the database (languages/*.json).
* Supports multiple languages (Japanese, Korean, Chinese, etc.)
* Tooltips translated to any locale.
* Pluggable engine: Kuromoji (morphological) OR Regex (pattern-based).
*
* Relevant Kuromoji POS tags (IPAdic):
* - 助詞 (joshi): particles
* - 格助詞: case (が, を, に, で, と, へ, から, まで)
* - 係助詞: topic/binding (は, も)
* - 接続助詞: conjunctive (て, ば, と, ので, のに, けど)
* - 終助詞: sentence final (ね, よ, ぞ, ぜ, わ, かな, かしら)
* - 副助詞: adverbial (って, でも, まで)
* - 助動詞 (jodōshi): verbal auxiliaries
* - 動詞-非自立: non-independent verbs (いる, ある, おく, しまう after て)
*/
// ═══════════════════════════════════════════════════════════════════
// STATE
// ═══════════════════════════════════════════════════════════════════
const SKIP_TAGS = new Set([
'SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT',
'CODE', 'PRE', 'SVG', 'MATH', 'CANVAS', 'VIDEO', 'AUDIO',
'IFRAME', 'OBJECT', 'EMBED',
]);
let isActive = false;
let tokenizer = null;
let isLoading = false;
let pendingActivation = false;
// Active languages and compiled data
let activeLangs = []; // [{ id, detectRegex, compiled, engine }]
let currentLocale = 'pt-BR';
// Mutation observer and SPA navigation
let observer = null;
let debounceTimer = null;
let lastUrl = location.href;
let DEBOUNCE_MS = 300;
// Optimization for video/subtitle sites
if (/(youtube\.com|netflix\.com|crunchyroll\.com|viki\.com|primevideo\.com|hulu\.com|animelon\.com)/i.test(location.hostname)) {
DEBOUNCE_MS = 10;
console.log('[NihongoColor] Video site detected. Debounce rate reduced to match subtitles.');
}
// Highlight API State (Without modifying the DOM)
const nodeRanges = new WeakMap(); // TextNode -> Range[]
const highlightMap = new Map(); // Color -> Highlight object
const rangeData = new WeakMap(); // Range -> { color, title }
const processedNodes = new WeakSet(); // TextNodes already processed
let tooltipEl = null;
let currentHoverRange = null;
// ═══════════════════════════════════════════════════════════════════
// LANGDB + KUROMOJI INITIALIZATION
// ═══════════════════════════════════════════════════════════════════
async function initLanguages() {
// Load saved or default locale
const stored = await chrome.storage.local.get(['jpLocale', 'jpEnabledLangs']);
currentLocale = stored.jpLocale || 'pt-BR';
const enabledLangIds = stored.jpEnabledLangs || ['japanese'];
// Initialize LangDB
await LangDB.init(currentLocale);
// Prepare active languages
activeLangs = [];
for (const langId of enabledLangIds) {
const compiled = LangDB.getCompiled(langId);
const detectRegex = LangDB.getDetectRegex(langId);
const entry = LangDB.getRegistryEntry(langId);
if (compiled && detectRegex && entry) {
activeLangs.push({
id: langId,
detectRegex,
compiled,
engine: entry.engine,
});
}
}
console.log(`[NihongoColor] LangDB ready — ${activeLangs.length} active language(s): ${activeLangs.map(l => l.id).join(', ')}`);
}
function initTokenizer() {
return new Promise((resolve, reject) => {
if (tokenizer) { resolve(tokenizer); return; }
if (isLoading) {
const check = setInterval(() => {
if (tokenizer) { clearInterval(check); resolve(tokenizer); }
}, 200);
return;
}
isLoading = true;
const dicPath = chrome.runtime.getURL('dict/');
if (typeof kuromoji === 'undefined') {
reject(new Error('kuromoji.js não foi carregado'));
return;
}
kuromoji.builder({ dicPath }).build((err, _tokenizer) => {
isLoading = false;
if (err) {
console.error('[NihongoColor] Error loading Kuromoji:', err);
reject(err);
} else {
tokenizer = _tokenizer;
console.log('[NihongoColor] Kuromoji successfully initialized ✓');
resolve(tokenizer);
}
});
});
}
// ═══════════════════════════════════════════════════════════════════
// LANGUAGE DETECTION BY TEXT
// ═══════════════════════════════════════════════════════════════════
function detectLanguagesInText(text) {
const matches = [];
for (const lang of activeLangs) {
if (lang.detectRegex.test(text)) {
matches.push(lang);
}
}
return matches;
}
// ═══════════════════════════════════════════════════════════════════
// CSS HIGHLIGHTS AND TOOLTIPS (WITHOUT MODIFYING DOM)
// ═══════════════════════════════════════════════════════════════════
function addHighlightCSS(color) {
let styleEl = document.getElementById('jp-hl-styles');
if (!styleEl) {
styleEl = document.createElement('style');
styleEl.id = 'jp-hl-styles';
styleEl.textContent = `
::highlight(jp-hover) {
background-color: rgba(0, 0, 0, 0.08) !important;
}
`;
document.head.appendChild(styleEl);
}
const className = 'jp-color-' + color.replace('#', '');
if (!styleEl.textContent.includes(className)) {
styleEl.textContent += `
::highlight(${className}) {
color: ${color} !important;
text-underline-offset: 3px;
}
`;
}
}
// CSS for SOV roles (uses background-color, coexists with grammar colors)
const sovHighlightMap = new Map(); // sovKey -> Highlight
function addSovHighlightCSS(bgColor, borderColor, sovKey) {
let styleEl = document.getElementById('jp-hl-styles');
if (!styleEl) return;
if (!styleEl.textContent.includes(sovKey)) {
styleEl.textContent += `
::highlight(${sovKey}) {
background-color: ${bgColor} !important;
text-decoration: underline solid ${borderColor} !important;
text-decoration-thickness: 2px !important;
text-underline-offset: 2px;
}
`;
}
}
function removeRangesForNode(node) {
const ranges = nodeRanges.get(node);
if (ranges) {
for (const r of ranges) {
const data = rangeData.get(r);
if (data) {
const hl = highlightMap.get(data.color);
if (hl) hl.delete(r);
}
}
nodeRanges.delete(node);
}
}
function createTooltip() {
if (tooltipEl) return;
tooltipEl = document.createElement('div');
tooltipEl.id = 'jp-hl-tooltip';
tooltipEl.style.cssText = `
position: fixed;
z-index: 999999;
background: #2C3E50;
color: #ECF0F1;
padding: 6px 10px;
border-radius: 4px;
font-family: 'Segoe UI', sans-serif;
font-size: 13px;
pointer-events: none;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
display: none;
white-space: nowrap;
transition: opacity 0.1s;
`;
document.body.appendChild(tooltipEl);
const hoverHl = new Highlight();
CSS.highlights.set('jp-hover', hoverHl);
}
// Detects mouse hovering over text with highlighted Range
document.addEventListener('mousemove', (e) => {
if (!isActive) {
if (tooltipEl) tooltipEl.style.display = 'none';
return;
}
let range;
if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(e.clientX, e.clientY);
}
let foundRange = null;
if (range && range.startContainer.nodeType === Node.TEXT_NODE) {
const node = range.startContainer;
const offset = range.startOffset;
const ranges = nodeRanges.get(node);
if (ranges) {
for (const r of ranges) {
if (offset >= r.startOffset && offset < r.endOffset) {
foundRange = r;
break;
}
}
}
}
if (foundRange) {
const data = rangeData.get(foundRange);
if (!tooltipEl) createTooltip();
tooltipEl.textContent = data.title;
// Show above cursor to not overlap Rikaikun (which appears below)
tooltipEl.style.display = 'block';
const tipH = tooltipEl.offsetHeight || 30;
const tipW = tooltipEl.offsetWidth || 200;
const OFFSET = 12;
let top = e.clientY - tipH - OFFSET;
let left = e.clientX + OFFSET;
// Fallback: if it goes off top of screen, show below
if (top < 4) top = e.clientY + OFFSET + 16;
// Horizontal clamp: do not go off right edge
if (left + tipW > window.innerWidth - 4) left = window.innerWidth - tipW - 8;
// Left clamp
if (left < 4) left = 4;
tooltipEl.style.left = left + 'px';
tooltipEl.style.top = top + 'px';
if (currentHoverRange !== foundRange) {
currentHoverRange = foundRange;
const hoverHl = CSS.highlights.get('jp-hover');
if (hoverHl) {
hoverHl.clear();
hoverHl.add(foundRange);
}
}
} else {
if (tooltipEl) tooltipEl.style.display = 'none';
if (currentHoverRange) {
currentHoverRange = null;
const hoverHl = CSS.highlights.get('jp-hover');
if (hoverHl) hoverHl.clear();
}
}
});
// ═══════════════════════════════════════════════════════════════════
// MORPHOLOGICAL ANALYSIS (KUROMOJI ENGINE)
// ═══════════════════════════════════════════════════════════════════
function processTextNodeKuromoji(node, compiled) {
if (!tokenizer) return;
const text = node.nodeValue;
if (!text) return;
const tokens = tokenizer.tokenize(text);
let currentIndex = 0;
let i = 0;
const ranges = nodeRanges.get(node) || [];
// Pre-compute positions of each token for SOV pass
const tokenPositions = [];
let posAccum = 0;
for (const tok of tokens) {
tokenPositions.push({ start: posAccum, end: posAccum + tok.surface_form.length });
posAccum += tok.surface_form.length;
}
// Pass 1: Grammar highlighting (particles, verb forms, adjectives)
while (i < tokens.length) {
const tok = tokens[i];
const style = resolveTokenStyle(tokens, i, compiled);
if (style) {
try {
const range = new Range();
range.setStart(node, currentIndex);
range.setEnd(node, currentIndex + style.text.length);
let hl = highlightMap.get(style.color);
if (!hl) {
hl = new Highlight();
highlightMap.set(style.color, hl);
CSS.highlights.set('jp-color-' + style.color.replace('#', ''), hl);
addHighlightCSS(style.color);
}
hl.add(range);
rangeData.set(range, { color: style.color, title: style.title });
ranges.push(range);
} catch (e) {
// Range errors occur if text changed at the exact millisecond, ignore peacefully
}
currentIndex += style.text.length;
i += style.skip;
} else {
currentIndex += tok.surface_form.length;
i++;
}
}
// Pass 2: SOV role highlighting (background-color layer)
if (compiled.sovRoles) {
processSovPass(node, tokens, tokenPositions, compiled.sovRoles, ranges);
}
if (ranges.length > 0) {
nodeRanges.set(node, ranges);
}
}
// ═══════════════════════════════════════════════════════════════════
// SOV PASS — SUBJECT / OBJECT / VERB (BACKGROUND LAYER)
// ═══════════════════════════════════════════════════════════════════
function isNounLike(token, sovRole) {
const pos = token.pos;
const pos1 = token.pos_detail_1;
// Noun + not excluded subtypes (非自立, 接尾)
if (sovRole.nounPOS.has(pos) && !sovRole.nounPOSExclude.has(pos1)) return true;
// Also include prefix/prenominal (接頭詞, 連体詞)
if (sovRole.alsoIncludePOS.has(pos)) return true;
return false;
}
function addSovRange(node, start, end, sovRole, ranges) {
try {
const range = new Range();
range.setStart(node, start);
range.setEnd(node, end);
const sovKey = 'jp-sov-' + sovRole.borderColor.replace('#', '');
let hl = sovHighlightMap.get(sovKey);
if (!hl) {
hl = new Highlight();
sovHighlightMap.set(sovKey, hl);
CSS.highlights.set(sovKey, hl);
addSovHighlightCSS(sovRole.color, sovRole.borderColor, sovKey);
}
hl.add(range);
rangeData.set(range, { color: sovRole.borderColor, title: sovRole.title });
ranges.push(range);
} catch (e) {
// Ignore Range errors
}
}
function processSovPass(node, tokens, tokenPositions, sovRoles, ranges) {
// 1) Subject & Object: find trigger particles and look back
for (const role of ['subject', 'object']) {
const config = sovRoles[role];
if (!config) continue;
for (let i = 0; i < tokens.length; i++) {
const tok = tokens[i];
if (tok.pos !== config.triggerPOS) continue;
if (!config.triggerParticles.has(tok.surface_form)) continue;
// Lookback: find consecutive nouns before the particle
let spanStart = null;
let spanEnd = null;
for (let j = i - 1; j >= 0; j--) {
if (isNounLike(tokens[j], config)) {
spanStart = tokenPositions[j].start;
if (spanEnd === null) spanEnd = tokenPositions[j].end;
} else {
break; // Stop at first non-noun token
}
}
if (spanStart !== null && spanEnd !== null) {
addSovRange(node, spanStart, spanEnd, config, ranges);
}
}
}
// 2) Verb: independent verbs (動詞 + 自立)
if (sovRoles.verb) {
const config = sovRoles.verb;
for (let i = 0; i < tokens.length; i++) {
const tok = tokens[i];
if (tok.pos === config.pos && tok.pos_detail_1 === config.posDetail) {
addSovRange(node, tokenPositions[i].start, tokenPositions[i].end, config, ranges);
}
}
}
}
function resolveTokenStyle(tokens, idx, compiled) {
const tok = tokens[idx];
const pos = tok.pos;
const pos1 = tok.pos_detail_1;
const surface = tok.surface_form;
const {
particleStyle,
finalParticleStyle,
finalParticleMulti,
conditionalStyle,
quoteStyle,
auxCompoundColor,
auxCompoundMap,
auxTeFormConfig,
colloquialStyle,
auxVerbalStyle,
verbFormRules,
verbConjugationRules,
teFormStyle,
adjectiveRules,
specificWordsStyle,
} = compiled;
// 0) Specific words (Demonstratives, etc) - Highest priority
if (specificWordsStyle && specificWordsStyle[surface]) {
const style = specificWordsStyle[surface];
return { text: surface, color: style.color, title: style.title, skip: 1 };
}
// 1) Compound auxiliaries: て + non-independent verb
if (auxTeFormConfig && pos === auxTeFormConfig.triggerPOS && pos1 === auxTeFormConfig.triggerPOSDetail && auxTeFormConfig.triggers.has(surface)) {
const next = tokens[idx + 1];
if (next && next.pos === auxTeFormConfig.nextPOS && next.pos_detail_1 === auxTeFormConfig.nextPOSDetail) {
const baseForm = next.basic_form || next.surface_form;
const auxTitle = auxCompoundMap[baseForm] || auxCompoundMap[next.surface_form];
if (auxTitle) {
return { text: surface + next.surface_form, color: auxCompoundColor, title: auxTitle, skip: 2 };
}
}
}
// 2) Colloquial auxiliaries
if (colloquialStyle[surface]) {
const style = colloquialStyle[surface];
return { text: surface, color: style.color, title: style.title, skip: 1 };
}
// 3) Particles
if (pos === '助詞') {
if (quoteStyle[surface]) return { text: surface, color: quoteStyle[surface].color, title: quoteStyle[surface].title, skip: 1 };
if (conditionalStyle[surface]) return { text: surface, color: conditionalStyle[surface].color, title: conditionalStyle[surface].title, skip: 1 };
if (finalParticleMulti[surface]) return { text: surface, color: finalParticleMulti[surface].color, title: finalParticleMulti[surface].title, skip: 1 };
if (pos1 === '終助詞' && finalParticleStyle) {
return { text: surface, color: finalParticleStyle.color, title: finalParticleStyle.title + ' (' + surface + ')', skip: 1 };
}
// 5) Standalone て form (when NOT followed by compound auxiliary)
if (teFormStyle && teFormStyle.surfaces.has(surface) && pos1 === teFormStyle.posDetail) {
// Already handled by check 1) if followed by auxiliary, so only "loose" て arrives here
const next = tokens[idx + 1];
const isAuxNext = next && next.pos === '動詞' && next.pos_detail_1 === '非自立';
if (!teFormStyle.skipIfNextIsAuxiliary || !isAuxNext) {
return { text: surface, color: teFormStyle.color, title: teFormStyle.title, skip: 1 };
}
}
if (particleStyle[surface]) {
const s = particleStyle[surface];
return { text: surface, color: s.color, title: s.title, skip: 1 };
}
}
// 4) Verbal auxiliaries (たら / なら as 助動詞) — high priority
if (pos === '助動詞') {
for (const [auxSurface, auxData] of Object.entries(auxVerbalStyle)) {
if (surface === auxSurface || (auxData.altMatchField && tok[auxData.altMatchField] === auxSurface)) {
return { text: surface, color: auxData.color, title: auxData.title, skip: 1 };
}
}
}
// 6) Verb forms (た, ない, ます, う, よう, です, etc.)
if (pos === '助動詞' && verbFormRules && verbFormRules.length > 0) {
for (const rule of verbFormRules) {
if (tok.pos !== rule.pos) continue;
const fieldValue = tok[rule.matchField];
if (fieldValue === rule.matchValue) {
// Check surface exclusions (e.g. だ as auxiliary but not な/なら)
if (rule.surfaceExclude.length > 0 && rule.surfaceExclude.includes(surface)) continue;
return { text: surface, color: rule.color, title: rule.title, skip: 1 };
}
}
}
// 7) Verb conjugations (imperative: 命令形)
if (pos === '動詞' && verbConjugationRules && verbConjugationRules.length > 0) {
for (const rule of verbConjugationRules) {
if (tok.pos !== rule.pos) continue;
// Kuromoji uses conjugated_form (e.g. "命令e", "命令i", "命令ro")
const conjForm = tok.conjugated_form || '';
if (conjForm.includes(rule.conjugatedFormMatch)) {
return { text: surface, color: rule.color, title: rule.title, skip: 1 };
}
}
}
// 8) Adjectives (い-adj: 形容詞-自立, な-adj: 名詞-形容動詞語幹)
if (adjectiveRules && adjectiveRules.length > 0) {
for (const rule of adjectiveRules) {
if (tok.pos === rule.pos && tok.pos_detail_1 === rule.posDetail) {
return { text: surface, color: rule.color, title: rule.title, skip: 1 };
}
}
}
return null;
}
// ═══════════════════════════════════════════════════════════════════
// REGEX ANALYSIS (REGEX ENGINE — FOR KOREAN, CHINESE, ETC.)
// ═══════════════════════════════════════════════════════════════════
function processTextNodeRegex(node, compiled) {
const text = node.nodeValue;
if (!text) return;
const ranges = nodeRanges.get(node) || [];
for (const rule of compiled.regexRules) {
// Reset regex lastIndex for each node
rule.regex.lastIndex = 0;
let match;
while ((match = rule.regex.exec(text)) !== null) {
try {
const startIdx = match.index;
const endIdx = startIdx + match[0].length;
const range = new Range();
range.setStart(node, startIdx);
range.setEnd(node, endIdx);
let hl = highlightMap.get(rule.color);
if (!hl) {
hl = new Highlight();
highlightMap.set(rule.color, hl);
CSS.highlights.set('jp-color-' + rule.color.replace('#', ''), hl);
addHighlightCSS(rule.color);
}
hl.add(range);
rangeData.set(range, { color: rule.color, title: rule.title });
ranges.push(range);
} catch (e) {
// Ignore Range errors
}
}
}
if (ranges.length > 0) {
nodeRanges.set(node, ranges);
}
}
// ═══════════════════════════════════════════════════════════════════
// TREE WALKER — PROCESS ALL TEXT NODES
// ═══════════════════════════════════════════════════════════════════
function processSubtree(root) {
if (!root) return 0;
if (activeLangs.length === 0) return 0;
// Check if at least Kuromoji is ready (if necessary)
const needsKuromoji = activeLangs.some(l => l.engine === 'kuromoji');
if (needsKuromoji && !tokenizer) return 0;
// Build combined regex from all active languages
const combinedPattern = activeLangs.map(l => l.detectRegex.source).join('|');
const combinedRegex = new RegExp(combinedPattern);
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT,
{
acceptNode(node) {
const parent = node.parentElement;
if (!parent) return NodeFilter.FILTER_REJECT;
if (SKIP_TAGS.has(parent.tagName)) return NodeFilter.FILTER_REJECT;
if (!combinedRegex.test(node.nodeValue)) return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
}
}
);
let count = 0;
let current;
while ((current = walker.nextNode())) {
if (processedNodes.has(current)) continue;
processedNodes.add(current);
// Detect which languages are present in the node
const matchedLangs = detectLanguagesInText(current.nodeValue);
for (const lang of matchedLangs) {
if (lang.engine === 'kuromoji' && tokenizer) {
processTextNodeKuromoji(current, lang.compiled);
} else if (lang.engine === 'regex') {
processTextNodeRegex(current, lang.compiled);
}
}
count++;
}
return count;
}
function processPage() {
const count = processSubtree(document.body);
if (count > 0) {
console.log(`[NihongoColor] Processed ${count} text nodes.`);
}
}
// ═══════════════════════════════════════════════════════════════════
// MUTATION OBSERVER — DYNAMIC CONTENT
// ═══════════════════════════════════════════════════════════════════
let pendingNodes = [];
function scheduleDebouncedProcess() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
if (!isActive) return;
const nodes = pendingNodes.splice(0);
const unique = deduplicateNodes(nodes);
let total = 0;
for (const node of unique) {
if (node.isConnected) {
total += processSubtree(node);
}
}
if (total > 0) {
console.log(`[NihongoColor] Dynamic: ${total} nodes processed.`);
}
}, DEBOUNCE_MS);
}
function deduplicateNodes(nodes) {
if (nodes.length <= 1) return nodes;
const set = new Set(nodes);
return nodes.filter(node => {
if (!node) return false;
let parent = node.parentElement;
while (parent && parent !== document.body) {
if (set.has(parent)) return false;
parent = parent.parentElement;
}
return true;
});
}
function containsAnyLanguage(text) {
return activeLangs.some(l => l.detectRegex.test(text));
}
function startObserver() {
if (observer) return;
observer = new MutationObserver((mutations) => {
if (!isActive) return;
let hasWork = false;
for (const mutation of mutations) {
if (mutation.type === 'childList') {
for (const added of mutation.addedNodes) {
if (added.nodeType === Node.ELEMENT_NODE) {
pendingNodes.push(added);
hasWork = true;
} else if (added.nodeType === Node.TEXT_NODE && containsAnyLanguage(added.nodeValue)) {
if (added.parentElement) {
pendingNodes.push(added.parentElement);
hasWork = true;
}
}
}
}
if (mutation.type === 'characterData') {
const target = mutation.target;
removeRangesForNode(target);
processedNodes.delete(target);
if (containsAnyLanguage(target.nodeValue)) {
if (target.parentElement) {
pendingNodes.push(target.parentElement);
hasWork = true;
}
}
}
}
if (hasWork) {
scheduleDebouncedProcess();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
characterData: true,
});
console.log('[NihongoColor] MutationObserver activated ✓');
}
function stopObserver() {
if (observer) {
observer.disconnect();
observer = null;
}
clearTimeout(debounceTimer);
pendingNodes = [];
}
// ═══════════════════════════════════════════════════════════════════
// SPA NAVIGATION DETECTION (pushState / replaceState / popstate)
// ═══════════════════════════════════════════════════════════════════
let historyPatched = false;
function onSpaNavigation() {
const newUrl = location.href;
if (newUrl === lastUrl) return;
lastUrl = newUrl;
if (!isActive) return;
console.log(`[NihongoColor] SPA navigation detected → ${newUrl}`);
setTimeout(() => {
if (isActive) processPage();
}, 800);
}
function patchHistory() {
if (historyPatched) return;
historyPatched = true;
const origPush = history.pushState;
history.pushState = function (...args) {
origPush.apply(this, args);
onSpaNavigation();
};
const origReplace = history.replaceState;
history.replaceState = function (...args) {
origReplace.apply(this, args);
onSpaNavigation();
};
window.addEventListener('popstate', onSpaNavigation);
console.log('[NihongoColor] SPA detection activated ✓');
}
// ═══════════════════════════════════════════════════════════════════
// CLEAR HIGHLIGHTS
// ═══════════════════════════════════════════════════════════════════
function clearHighlights() {
if (typeof CSS !== 'undefined' && CSS.highlights) {
for (const key of highlightMap.keys()) {
CSS.highlights.delete('jp-color-' + key.replace('#', ''));
}
for (const key of sovHighlightMap.keys()) {
CSS.highlights.delete(key);
}
CSS.highlights.delete('jp-hover');
}
highlightMap.clear();
sovHighlightMap.clear();
if (tooltipEl) tooltipEl.style.display = 'none';
}
// ═══════════════════════════════════════════════════════════════════
// ACTIVATION / DEACTIVATION
// ═══════════════════════════════════════════════════════════════════
async function activate() {
try {
// 1) Load languages database
await initLanguages();
// 2) Initialize Kuromoji if any language needs it
const needsKuromoji = activeLangs.some(l => l.engine === 'kuromoji');
if (needsKuromoji) {
await initTokenizer();
}
// 3) Process page
processPage();
isActive = true;
startObserver();
patchHistory();
return 'activated';
} catch (err) {
console.error('[NihongoColor] Failed to activate:', err);
return 'error';
}
}
function deactivate() {
isActive = false;
stopObserver();
clearHighlights();
return 'deactivated';
}
// ═══════════════════════════════════════════════════════════════════
// POPUP MESSAGES
// ═══════════════════════════════════════════════════════════════════
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.action === 'activate') {
if (isLoading) {
pendingActivation = true;
sendResponse({ status: 'loading' });
return true;
}
activate().then((status) => {
sendResponse({ status });
});
return true;
}
if (message.action === 'deactivate') {
sendResponse({ status: deactivate() });
return true;
}
if (message.action === 'status') {
if (isLoading) {
sendResponse({ status: 'loading' });
} else if (isActive) {
sendResponse({ status: 'ready' });
} else {
sendResponse({ status: 'off' });
}
return true;
}
// New commands for multi-language popup
if (message.action === 'getLanguages') {
(async () => {
if (!LangDB.isReady()) await LangDB.init(currentLocale);
sendResponse({ languages: LangDB.getLanguages() });
})();
return true;
}
if (message.action === 'getLegend') {
const langId = message.langId || 'japanese';
if (!LangDB.isReady()) {
sendResponse({ legend: [] });
} else {
sendResponse({ legend: LangDB.getLegend(langId) });
}
return true;
}
if (message.action === 'setLocale') {
currentLocale = message.locale || 'pt-BR';
chrome.storage.local.set({ jpLocale: currentLocale });
if (LangDB.isReady()) {
LangDB.setLocale(currentLocale);
// Recompile active languages
for (const lang of activeLangs) {
lang.compiled = LangDB.getCompiled(lang.id);
}
}
sendResponse({ status: 'ok', locale: currentLocale });
return true;
}
if (message.action === 'updateCustomStyles') {
if (LangDB.isReady()) {
LangDB.updateCustomStyles(message.styles);
for (const lang of activeLangs) {
lang.compiled = LangDB.getCompiled(lang.id);
}
clearHighlights();
nodeRanges.clear();
rangeData.clear();
if (isActive) processPage();
}
sendResponse({ status: 'ok' });
return true;
}
if (message.action === 'setEnabledLangs') {
const langIds = message.langIds || ['japanese'];
chrome.storage.local.set({ jpEnabledLangs: langIds });
// If active, reload
if (isActive) {
deactivate();
activate().then(() => {
sendResponse({ status: 'reloaded' });
});
return true;
}
sendResponse({ status: 'ok' });
return true;
}
return false;
});
chrome.storage.local.get(['jpHighlighterActive'], (result) => {
if (result.jpHighlighterActive) {
requestAnimationFrame(() => activate());
}
});