-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtexthook.js
2566 lines (2327 loc) · 85.9 KB
/
texthook.js
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
// Copyright 2017; Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
'use strict';
// updated by a timer looping function, based on local storage set by the options page
let settings = {
enabled: false,
kanji_mode: false,
compact: true,
usetextfields: true,
length: 25,
contextlength: 100, // nonconfigurable
fixedwidth: false,
fixedwidthpositioning: false,
superborder: false,
disableborder: false,
space_saver: false,
hide_deconj: false,
showoriginal: true,
reader_sticky: false,
definitions_mode: 0,
normal_definitions_in_mining: false,
alternatives_mode: 3, // 0: longest only; 1: longest and shortest; 2: longest and second longest; 3: all matches
strict_alternatives: true, // if true, alternatives looked up in all kana can not return results with kanji glosses that don't have any usually/exclusively kana info
strict_epwing: true,
json_newlines_replace: false,
json_newlines_character: " ⬥ ",
scale: 1,
width: 600,
lookuprate: 8,
bgcolor: "#111111",
fgcolor: "#CCCCCC",
hlcolor: "#99DDFF",
hlcolor2: "#99FF99",
titlecolor: "#D8A888",
font: "",
hlfont: "",
definition_fontsize: 13,
dict_item_fontsize: 18,
reading_fontsize: 15,
corner: 0,
xoffset: 5,
yoffset: 22,
strip_spaces: true,
ignore_linebreaks: true,
ignore_divs: false,
sticky: false,
popup_follows_mouse: true,
popup_requires_key: 0, // 0: none; 1: ctrl; 2: shift
x_dodge: 1,
y_dodge: 0,
sticky_maxheight: 0,
kanji_show_stroke_count: true,
kanji_show_readings: true,
kanji_show_composition: true,
kanji_show_quality_warning: true,
hotkey_mine: "m",
hotkey_nazeka_toggle: "j",
hotkey_close: "n",
hotkey_sticky: "b",
hotkey_audio: "p",
hotkey_kanji_mode: "k",
hotkey_nudge_left: "ArrowLeft",
hotkey_nudge_right: "ArrowRight",
volume: 0.2,
live_mining: false,
use_selection: false,
only_selection: false,
audio_force_https: false,
highlight_dict_headers: false,
dropshadow: false,
dropshadowradius: 10,
dropshadowcolor: "#000000",
padding: 2,
novalign: 2,
inserthr: false,
info_styling: false,
info_fgcolor: "#CCCCCC",
info_bgcolor_pos: "#606060",
info_bgcolor_num: "#406080",
info_bgcolor_etc: "#608040"
};
let platform = "win";
let is_reader = false;
async function get_real_platform()
{
let my_platform = undefined;
try {
my_platform = await browser.runtime.sendMessage({type:"platform"});
if(my_platform)
my_platform = my_platform["response"];
}catch(err){}// don't care
while(my_platform == "")
{
try {
my_platform = await browser.runtime.sendMessage({type:"platform"});
if(my_platform)
my_platform = my_platform["response"];
}catch(err){}// don't care
}
platform = my_platform;
}
get_real_platform();
let last_time_display = Date.now();
// FIXME: this should be the extension ID or a per-page random string, but it's not useful to change this yet, not until firefox lands shadow DOM
let div_class = "nazeka_fGKRTDGFGgr9atT";
// the popup is inserted into the page as a screen-relative div, we never delete it because modifying the root DOM of very long pages is expensive, we hide it instead
let last_displayed = undefined;
function delete_div()
{
last_displayed = undefined;
let other = get_div();
if(other)
{
other.innerHTML = "";
other.removeAttribute('style');
}
}
function set_sticky_styles(mydiv)
{
mydiv.style.position = "fixed";
mydiv.style.left = "unset";
mydiv.style.right= "unset";
mydiv.style.bottom = "unset";
mydiv.style.top = "10px";
if(settings.corner != 1 && settings.corner != 3)
mydiv.style.right = "10px";
else
mydiv.style.left = "10px";
mydiv.style.marginRight = "unset";
if(settings.sticky_maxheight == 0)
mydiv.style.maxHeight = "calc(100vh - 20px)";
else
mydiv.style.maxHeight = Math.round(Number(settings.sticky_maxheight))+"px";
mydiv.style.overflowY = "scroll";
mydiv.style.marginLeft = "unset";
mydiv.style.marginTop = "unset";
mydiv.style.marginBottom = "10px";
}
function is_not_frameset(root)
{
try
{
return root.parent.document.body.tagName != "FRAMESET";
}
catch(err)
{
return true;
}
}
function get_doc()
{
let find_root = document.defaultView;
let mydoc = document;
while(find_root.parent && find_root.parent != find_root && is_not_frameset(find_root))
{
find_root = find_root.parent;
try
{
mydoc = find_root.document;
}
catch(err)
{
console.log("hit error trying to find root; nazeka might behave incoherently");
console.log(err);
break;
}
}
return mydoc;
}
function mining_ui_exists()
{
return get_doc().body.getElementsByClassName("nazeka_mining_ui").length > 0;
}
function delete_mining_ui()
{
while(mining_ui_exists())
get_doc().body.getElementsByClassName("nazeka_mining_ui")[0].remove();
}
function get_mining_ui()
{
return get_doc().body.getElementsByClassName("nazeka_mining_ui")[0];
}
function getViewportSize(mydoc)
{
if (mydoc.compatMode == "CSS1Compat")
return { w: mydoc.documentElement.clientWidth, h: mydoc.documentElement.clientHeight };
return { w: mydoc.body.clientWidth, h: mydoc.body.clientHeight };
}
function is_sticky()
{
return (settings.sticky && platform != "android" && !mining_ui_exists()) || (is_reader && settings.reader_sticky);
}
// here we set all the styling and positioning of the div, passing "middle" as the actual contents of it.
// this is rather elaborate because of 1) a lack of shadow DOM, even for just styling 2) options 3) """features""" of how HTML viewport stuff works that are actually terrible
let last_display_x = 0;
let last_display_y = 0;
function display_div(middle, x, y)
{
last_display_x = x;
last_display_y = y;
let font = settings.font.trim().replace(";","").replace("}","");
if(font != "")
font += ",";
middle.style = `background-color: ${settings.bgcolor}; border-radius: 2.5px; border: 1px solid ${settings.bgcolor};`;
let border_text = (settings.disableborder)?("border: 0px solid transparent;"):(`border: 1px solid ${settings.fgcolor}`);
middle.firstChild.style = `${border_text}; border-radius: 2px; padding: ${settings.padding}px; background-color: ${settings.bgcolor}; color: ${settings.fgcolor}; font-family: ${font} Arial, sans-serif; text-align: left; font-size: ${settings.definition_fontsize}px;`;
let find_root = document.defaultView;
let newx = x;
let newy = y;
let mydoc = document;
while(find_root.parent && find_root.parent != find_root && is_not_frameset(find_root))
{
let rect = find_root.frameElement.getBoundingClientRect();
let rx = rect.x;
let ry = rect.y;
let sx1 = find_root.parent.scrollX;
let sy1 = find_root.parent.scrollY;
let sx2 = find_root.scrollX;
let sy2 = find_root.scrollY;
find_root = find_root.parent;
newx += (rx + sx1 - sx2);
newy += (ry + sy1 - sy2);
try
{
mydoc = find_root.document;
}
catch(err)
{
break;
}
}
// compensate for body being relative if it is, because our popup is absolute
let relative_body = getComputedStyle(mydoc.body).position == "relative";
if(relative_body)
{
let rect = mydoc.body.getBoundingClientRect();
newx -= rect.x;
newy -= rect.y;
// overcompensate for scrolling
newx -= mydoc.scrollingElement.scrollLeft;
newy -= mydoc.scrollingElement.scrollTop;
}
let basewidth = Math.round(Number(settings.width));
if(settings.x_dodge != 1 && basewidth > mydoc.documentElement.getBoundingClientRect().width/2 - settings.xoffset - 5)
basewidth = Math.max(150, Math.round(mydoc.documentElement.getBoundingClientRect().width/2 - settings.xoffset - 5));
let styletext = "";
if(settings.fixedwidth)
{
styletext += "max-width: " + basewidth + "px; "
styletext += "min-width: " + basewidth + "px; "
}
else
{
styletext += "max-width: " + basewidth + "px; "
styletext += "min-width: 150px; "
}
styletext += "position: absolute; top: 0; left: 0; transition: unset; ";
if(settings.superborder && !settings.disableborder)
styletext += `background-color: ${settings.fgcolor}; border-radius: 3px; border: 1px solid ${settings.fgcolor}; z-index: 1000000000000000000000;`;
else
styletext += `border-radius: 3px; background-color: ${settings.bgcolor}; z-index: 1000000000000000000000;`;
styletext += "writing-mode: horizontal-tb; line-height: initial; white-space: initial;";
if(settings.dropshadow)
{
let v = parseInt(settings.dropshadowcolor.substring(1), 16);
let r = Math.floor(v / 256 / 256);
let g = Math.floor(v / 256) % 256;
let b = Math.floor(v) % 256;
let rgba = "rgba(" + r + "," + g + "," + b + ",0.5)";
styletext += `box-shadow: 0 0 ${settings.dropshadowradius}px ${rgba};`
}
let other = mydoc.body.getElementsByClassName(div_class);
let outer = undefined;
if(other.length > 0)
{
outer = other[0];
if(!outer.firstChild)
outer.appendChild(middle);
else if(outer.firstChild != middle)
outer.replaceChild(middle, outer.firstChild);
outer.style.display = "block";
}
else
{
outer = mydoc.createElement("div");
outer.className = div_class;
if(!outer.firstChild || outer.firstChild != middle)
outer.appendChild(middle);
mydoc.body.appendChild(outer);
}
outer.lang = "ja";
if(settings.scale != 1 && settings.scale > 0 && settings.scale < 64)
styletext += " transform-origin: top left; transform: scale(" + Number(settings.scale) + ");";
outer.style = styletext;
let mywidth = basewidth*settings.scale;
if(!settings.fixedwidthpositioning)
mywidth = outer.offsetWidth*settings.scale;
let corner = settings.corner;
if(is_sticky())
set_sticky_styles(outer);
else
{
if(corner == 1 || corner == 3)
{
newx -= mywidth;
newx -= settings.xoffset;
}
else
newx += settings.xoffset;
if(corner == 2 || corner == 3)
newy = newy - outer.getBoundingClientRect().height - settings.yoffset;
else
newy = newy + settings.yoffset;
// fixes an absolute positioning """feature""" that doesn't work properly with very wide pages (e.g. pages of left-to-right vertical text)
outer.style.top = "0px";
outer.style.left = "0px";
let width = outer.offsetWidth;
//
outer.style.top = (newy)+"px";
outer.style.bottom = "unset";
outer.style.right = "unset";
outer.style.left = (newx)+"px";
outer.style.marginRight = "unset";
outer.style.marginLeft = "unset";
// fixes an absolute positioning """feature""" that doesn't work properly with very wide pages (e.g. pages of left-to-right vertical text)
outer.style.width = (width)+"px";
//
}
let localrect;
let dodged_vertically = false;
let viewport = getViewportSize(mydoc);
// dodge top/bottom
localrect = outer.getBoundingClientRect();
if(localrect.bottom+5 > viewport.h)
{
if(settings.y_dodge == 1) // slide
newy -= localrect.bottom+5 - viewport.h;
else // flip
newy -= localrect.height + settings.yoffset*2;
outer.style.top = (newy)+"px";
dodged_vertically = true;
}
localrect = outer.getBoundingClientRect();
if(localrect.top-5 < 0 && (!dodged_vertically || corner == 0 || corner == 1)) // (prefer running off the top when using "bottom" alignments)
{
if(settings.y_dodge == 1)
newy -= localrect.top-5;
else
newy += localrect.height + settings.yoffset*2;
outer.style.top = (newy)+"px";
}
// dodge left/right
localrect = outer.getBoundingClientRect();
if(localrect.right+5 > viewport.w)
{
if(settings.x_dodge == 1)
newx -= localrect.right+5 - viewport.w;
else
newx -= localrect.width + settings.xoffset*2;
outer.style.left = (newx)+"px";
}
localrect = outer.getBoundingClientRect();
if(localrect.left-5 < 0)
{
if(settings.x_dodge == 1)
newx -= localrect.left-5;
else
newx += localrect.width + settings.xoffset*2;
outer.style.left = (newx)+"px";
}
}
function exists_div()
{
let mydoc = get_doc();
let other = mydoc.body.getElementsByClassName(div_class);
return (other.length > 0 && other[0].style.display != "none" && other[0].innerHTML != "");
}
function get_div()
{
let mydoc = get_doc();
let other = mydoc.body.getElementsByClassName(div_class);
if(other.length > 0 && other[0].style.display != "none" && other[0].innerHTML != "")
return other[0];
else
return undefined;
}
// In JMdict, part-of-speech tags are XML entities.
// We processed JMdict's XML with entity processing disabled so we can just use the bare tags (e.g. "v1", not "ichidan verb").
// This left the entity syntax &this; behind so we want to cut it off when we return results.
function clip(str)
{
if(str == undefined)
return;
if(str.length <= 2)
return "";
return str.substring(1, str.length-1);
}
function elementize_jmdict_defs(goodsenses)
{
let lastpos = [];
let jmdict_defs = document.createElement("div");
jmdict_defs.className = "jmdict_definitions";
for(let j = 0; j < goodsenses.length; j++)
{
let sense = goodsenses[j];
if(sense.pos + "" == lastpos) // removing this + "" breaks repetitive sense hiding in chrome
sense.pos = undefined;
else
lastpos = sense.pos;
if(sense.pos && sense.pos.length > 0)
{
let part = document.createElement("span");
part.className = "nazeka_pos";
let temptext = "(";
let parts = [];
for(let l = 0; l < sense.pos.length; l++)
parts.push(sense.pos[l]);
temptext += parts.join(", ");
temptext += ")";
part.textContent = temptext;
jmdict_defs.appendChild(part);
if(settings.compact)
jmdict_defs.appendChild(document.createTextNode(" "));
else
jmdict_defs.appendChild(document.createElement("br"));
}
if(settings.compact)
{
if(goodsenses.length > 1)
{
let number = document.createElement("span");
number.className = "nazeka_num";
number.textContent = "("+(j+1)+")";
jmdict_defs.appendChild(number);
jmdict_defs.appendChild(document.createTextNode(" "));
}
}
else
{
let temptag = document.createElement("span");
temptag.className = "nazeka_num";
temptag.textContent = ""+(j+1)+".";
jmdict_defs.appendChild(temptag);
jmdict_defs.appendChild(document.createTextNode(" "));
}
if(sense.inf)
{
let info = document.createElement("i");
info.textContent = "("+sense.inf+") ";
jmdict_defs.appendChild(info);
}
if(sense.misc)
{
let temptag = document.createElement("span");
temptag.className = "nazeka_misc";
let temptext = "(";
let parts = [];
for(let l = 0; l < sense.misc.length; l++)
parts.push(sense.misc[l].substring(1, sense.misc[l].length-1));
temptext += parts.join(", ");
temptext += ")";
temptag.textContent = temptext;
jmdict_defs.appendChild(temptag);
jmdict_defs.appendChild(document.createTextNode(" "));
}
jmdict_defs.appendChild(document.createTextNode(sense.gloss.join("; ")));
if(settings.compact)
{
if(sense.gloss.length > 1 || j+1 != goodsenses.length)
jmdict_defs.appendChild(document.createTextNode("; "));
}
else
{
jmdict_defs.appendChild(document.createElement("br"));
}
}
return jmdict_defs;
}
function elementize_json_defs(json)
{
if(json.length > 0)
{
let json_defs = document.createElement("div");
json_defs.className = "json_definitions";
for(let entry of json)
{
let json_head = document.createElement("div");
let json_title = document.createElement("span");
json_title.appendChild(document.createTextNode("―"+entry["z"]+"―"));
json_title.className = "json_title";
json_head.appendChild(json_title);
json_head.appendChild(document.createElement("br"));
let json_head_text = entry["r"];
if(entry["s"] && entry["s"][0] != "")
{
let isfirst = true;
json_head_text += "【";
for(let spelling of entry["s"])
{
if(!isfirst)
json_head_text += "・";
json_head_text += spelling;
isfirst = false;
}
json_head_text += "】";
}
json_head.appendChild(document.createTextNode(json_head_text));
let json_definition = document.createElement("div");
let isfirst = true;
for(let line of entry["l"])
{
if(!isfirst)
{
json_definition.appendChild(document.createElement("br"));
let filler = document.createElement("span");
filler.className = "json_newline_filler";
json_definition.appendChild(filler);
}
let def_line = document.createElement("span");
def_line.textContent = line;
json_definition.appendChild(def_line);
isfirst = false;
}
json_head.className = "json_head";
json_definition.className = "json_definition";
json_defs.appendChild(json_head);
json_defs.appendChild(json_definition);
}
return json_defs;
}
else
{
return undefined;
}
}
function filter_for_css(text)
{
return text.replace(";","").replace("}","").replace("'","").replace('"',"");
}
function filter_for_css_trimmed(text)
{
return filter_for_css(text).trim();
}
function get_style()
{
// styling for highlighted stuff and the lookup text
let style = document.createElement("style");
style.type = "text/css";
let font = filter_for_css_trimmed(settings.hlfont);
let morefont = filter_for_css_trimmed(settings.font);
let newline_char = filter_for_css(settings.json_newlines_character);
if(font != "")
font += ",";
if(morefont != "")
font += morefont + ",";
let valign = ".nazeka_word * {vertical-align: middle}";
if(settings.novalign)
valign = "";
let infostyle = "";
if(settings.info_styling)
{
infostyle =
`.nazeka_pos, .nazeka_num, .nazeka_misc {font-size:80%; border-radius:3px; padding: 1px 3px 2px; color:${settings.info_fgcolor}; display:inline-block; position: relative; bottom: 1px; margin-bottom: -1px;}
.nazeka_pos {background-color:${settings.info_bgcolor_pos};}
.nazeka_num {background-color:${settings.info_bgcolor_num};}
.nazeka_misc {background-color:${settings.info_bgcolor_etc};}
`;
}
style.textContent =
`.nazeka_main_keb{font-size:${settings.dict_item_fontsize}px;white-space:nowrap;font-family: ${font}IPAGothic,TakaoGothic,Noto Sans CJK JP Regular,Meiryo,sans-serif;color:${settings.hlcolor}}
.nazeka_main_reb{font-size:${settings.dict_item_fontsize}px;white-space:nowrap;font-family: ${font}IPAGothic,TakaoGothic,Noto Sans CJK JP Regular,Meiryo,sans-serif;color:${settings.hlcolor}}
${valign}
${infostyle}
.${div_class} hr{margin: 0.5em; padding: 0; border: none; border-top: 1px solid ${settings.fgcolor}; opacity: 0.25}
.nazeka_sub_keb{font-size:${settings.reading_fontsize}px;white-space:nowrap;font-family: ${font}IPAGothic,TakaoGothic,Noto Sans CJK JP Regular,Meiryo,sans-serif}
.nazeka_sub_reb{font-size:${settings.reading_fontsize}px;white-space:nowrap;color:${settings.hlcolor2}}
.nazeka_original{float: right; margin-right: 2px; margin-left:4px; opacity:0.7;}
.json_head{margin: 0 4px 6px;}
.jmdict_definitions{margin: 0 4px 6px;}
.json_definition{margin: 0 4px 6px;}
.kanji_info{margin: 2px 4px 2px;}
`;
if(settings.highlight_dict_headers)
style.textContent +=
`.jmdict_title{color:${settings.titlecolor}}
.json_title{color:${settings.titlecolor}}
`;
if(settings.json_newlines_replace)
style.textContent +=
`.json_definition br{display: none;}
.json_definition .json_newline_filler::before{content: '${newline_char}';}
`;
return style;
}
// Here we actually build the content of the lookup popup, based on the text we looked up and the list of lookups from the background script
// note that we can get multiple lookups and this function only handles a single lookup
function build_div_inner(text, result, moreText, index, first_of_many = false)
{
let temp = document.createElement("div");
temp.style.position = "relative";
// the "Looked up XXXX" text
if(settings.showoriginal)
{
// nesting it like this lets us access nazeka_lookup when we want to get the text we looked up, if we have text in front of it, which we don't anymore
let original = document.createElement("div");
original.className = "nazeka_original";
//original.textContent = "Looked up ";
let moreText_start = moreText.substring(0, index)
let moreText_end = moreText.substring(index + text.length);
if(moreText_start.length > 5)
{
moreText_start = "…"+moreText_start.substring(moreText_start.length-3);
}
if(moreText_end.length > 5)
{
moreText_end = moreText_end.substring(0, 3)+"…";
}
let original_inner = document.createElement("span");
original_inner.className = "nazeka_lookup";
original_inner.textContent = text;
original_inner.style.fontWeight = "bold";
original_inner.style.color = `${settings.hlcolor}`;
original.appendChild(document.createTextNode(moreText_start));
original.appendChild(original_inner);
original.appendChild(document.createTextNode(moreText_end));
if(first_of_many && (platform == "android" || is_sticky()))
{
let buttons = document.createElement("div");
let left_arrow = document.createElement("img");
let right_arrow = document.createElement("img");
left_arrow.src = browser.extension.getURL("img/leftarrow24.png");
right_arrow.src = browser.extension.getURL("img/rightarrow24.png");
left_arrow.onclick = lookup_left;
right_arrow.onclick = lookup_right;
left_arrow.style.marginTop = "-3px";
right_arrow.style.marginTop = "-3px";
left_arrow.style.display = "inline";
right_arrow.style.display = "inline";
left_arrow.style.padding = "initial";
right_arrow.style.padding = "initial";
buttons.appendChild(left_arrow);
buttons.appendChild(right_arrow);
if(platform == "android")
{
let close_button = document.createElement("img");
close_button.src = browser.extension.getURL("img/closebutton24.png");
close_button.onclick = manual_close;
close_button.style.marginTop = "-3px";
close_button.style.display = "inline";
close_button.style.padding = "initial";
buttons.appendChild(close_button);
}
else
{
let close_button = document.createElement("img");
close_button.src = browser.extension.getURL("img/closebutton24.png");
close_button.onclick = manual_disable_sticky;
close_button.style.marginTop = "-3px";
close_button.style.display = "inline";
close_button.style.padding = "initial";
buttons.appendChild(close_button);
}
buttons.style.float = "right";
original.appendChild(buttons);
}
temp.appendChild(original);
let original_sentence = document.createElement("span");
original_sentence.className = "nazeka_lookup_sentence";
original_sentence.textContent = moreText;
original_sentence.style.display = "none";
temp.appendChild(original_sentence);
let original_index = document.createElement("span");
original_index.className = "nazeka_lookup_index";
original_index.textContent = `${index}`;
original_index.style.display = "none";
temp.appendChild(original_index);
}
else
{
let original_inner = document.createElement("span");
original_inner.className = "nazeka_lookup";
original_inner.textContent = text;
original_inner.style.display = "none";
temp.appendChild(original_inner);
let original_sentence = document.createElement("span");
original_sentence.className = "nazeka_lookup_sentence";
original_sentence.textContent = moreText;
original_sentence.style.display = "none";
temp.appendChild(original_sentence);
let original_index = document.createElement("span");
original_index.className = "nazeka_lookup_index";
original_index.textContent = `${index}`;
original_index.style.display = "none";
temp.appendChild(original_index);
}
let style = get_style();
temp.appendChild(style);
function makespan(text)
{
let s = document.createElement("span");
s.textContent = text;
return s;
}
// lookups can have multiple results (e.g. する -> 為る, 刷る, 掏る, 剃る, 擦る)
for(let i = 0; i < result.length; i++)
{
let term = result[i];
let container = document.createElement("div");
container.className = "nazeka_word_container";
container.style.marginBottom = "3px";
container.setAttribute("nazeka_seq", term.seq);
let text = "";
if(term.found.keb)
text = term.found.keb;
else if(term.found.reb)
text = term.found.reb;
else
continue; // shouldn't happen, but again, just in case of broken data
if(term.has_audio.length > 0)
{
for(let audio of term.has_audio)
{
let audio_data_holder = document.createElement("div");
audio_data_holder.className = "nazeka_audioref";
audio_data_holder.style.display = "none";
audio_data_holder.innerText += audio+"\n";
container.appendChild(audio_data_holder);
}
}
// for mining
let reading_data_holder = document.createElement("div");
reading_data_holder.className = "nazeka_mining_readings";
reading_data_holder.style.display = "none";
let reading_data_list = new Array();
for(let r of term.r_ele)
reading_data_list.push(r.reb);
reading_data_holder.innerText = reading_data_list.join("、");
container.appendChild(reading_data_holder);
let spelling_data_holder = document.createElement("div");
spelling_data_holder.className = "nazeka_mining_spellings";
spelling_data_holder.style.display = "none";
let spelling_data_list = new Array();
if(term.k_ele)
for(let k of term.k_ele)
spelling_data_list.push(k.keb);
spelling_data_holder.innerText = spelling_data_list.join("、");
container.appendChild(spelling_data_holder);
// end of "for mining"
let temptag = document.createElement("span");
temptag.className = "nazeka_word";
let original_kana = "";
if(term.found.reb)
original_kana = term.found.reb;
if(term.found.keb)
{
let k_ele = term.found;
let kanji_text = k_ele.keb;
let keb = document.createElement("span");
keb.className = "nazeka_main_keb";
keb.textContent = kanji_text;
temptag.appendChild(keb);
if(k_ele.inf)
{
let maininfos = document.createElement("span");
maininfos.className = "nazeka_main_infos";
for(let info of k_ele.inf)
{
let keb_inf = document.createElement("span");
keb_inf.className = "nazeka_main_inf";
keb_inf.textContent += "(";
keb_inf.textContent += clip(info);
keb_inf.textContent += ")";
maininfos.appendChild(keb_inf);
}
temptag.appendChild(maininfos);
}
// list readings
let readings = term.r_ele;
temptag.appendChild(document.createElement("wbr"));
let e_readings = document.createElement("span");
if(!settings.space_saver)
e_readings.appendChild(makespan("《"));
else
e_readings.appendChild(makespan(" "));
e_readings.className = "nazeka_readings";
for(let j = 0; j < readings.length; j++)
{
let subreb = document.createElement("span");
subreb.className = "nazeka_sub_reb";
subreb.textContent = readings[j].reb;
e_readings.appendChild(subreb);
if(readings[j].inf)
{
for(let info of readings[j].inf)
{
let reb_inf = document.createElement("span");
reb_inf.className = "nazeka_reb_inf";
reb_inf.textContent += "(";
reb_inf.textContent += clip(info);
reb_inf.textContent += ")";
e_readings.appendChild(reb_inf);
}
}
if(j+1 != readings.length)
{
e_readings.appendChild(document.createElement("wbr"));
e_readings.appendChild(makespan("、"));
}
}
if(!settings.space_saver)
e_readings.appendChild(makespan("》"));
else
e_readings.appendChild(makespan(" "));
temptag.appendChild(e_readings);
// list alternatives
let alternatives = [];
for(let j = 0; j < term.k_ele.length; j++)
if(term.k_ele[j].keb != kanji_text)
alternatives.push(term.k_ele[j]);
if(alternatives.length > 0)
temptag.appendChild(makespan((settings.space_saver)?(" ("):(" (also ")));
for(let j = 0; j < alternatives.length; j++)
{
let subkeb = document.createElement("span");
subkeb.className = "nazeka_sub_keb";
subkeb.textContent = alternatives[j].keb;
temptag.appendChild(subkeb);
if(alternatives[j].inf)
{
for(let info of alternatives[j].inf)
{
temptag.appendChild(makespan(" "));
let keb_inf = document.createElement("span");
keb_inf.className = "nazeka_keb_inf";
keb_inf.textContent += "(";
keb_inf.textContent += clip(info);
keb_inf.textContent += ")";
temptag.appendChild(keb_inf);
}
}
if(j+1 < alternatives.length)
temptag.appendChild(makespan(", "));
}
if(alternatives.length > 0)
temptag.appendChild(makespan(")"));
// deconjugations
if(term.deconj && !settings.hide_deconj)
{
let deconj = "";
let first = true;
for(let form of term.deconj)
{
let formtext = "";
let added = 0;
for(let f = form.process.length-1; f >= 0; f--)
{
let info = form.process[f];
if(info == "")
continue;
if(info.startsWith("(") && info.endsWith(")") && f != 0)
continue;
if(added > 0)
formtext += "→";
added++;
formtext += info;
}
if(formtext != "")
{
if(first)
{
temptag.appendChild(document.createElement("wbr"));
deconj = "~";
}
else
{
temptag.appendChild(document.createElement("wbr"));
deconj = ";";
}
temptag.appendChild(makespan(deconj));
temptag.appendChild(document.createElement("wbr"));
deconj = formtext;
temptag.appendChild(makespan(deconj));
}
first = false;
}
}
}
else
{
let main_reb = document.createElement("span");
main_reb.className = "nazeka_main_reb";
main_reb.textContent = term.found.reb;
temptag.appendChild(main_reb);
if(term.deconj && !settings.hide_deconj)
{
let deconj = "";
let first = true;
for(let form of term.deconj)
{
let formtext = "";
let added = 0;
for(let f = form.process.length-1; f >= 0; f--)
{
let info = form.process[f];
if(info == "")
continue;
if(info.startsWith("(") && info.endsWith(")") && f != 0)
continue;
if(added > 0)
formtext += "→";
added++;
formtext += info;
}
if(formtext != "")
{
if(first)
deconj += "~";
else
deconj += "・";
deconj += formtext;
}
first = false;
}
temptag.appendChild(makespan(deconj));