-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBack Template.html
More file actions
1053 lines (948 loc) · 33.8 KB
/
Back Template.html
File metadata and controls
1053 lines (948 loc) · 33.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
<!-- version b174588 -->
<script>
// ############## HINT REVEAL SHORTCUTS ##############
// All shortcuts will also open with "H" if using the Hint Hotkeys add-on
var ButtonShortcuts = {
"Lecture Notes" : "Alt + 1",
"Missed Questions" : "Alt + 2",
"Pixorize" : "Alt + 3",
"Sketchy" : "Alt + 4",
"Additional Resources" : "Alt + 5",
}
var ToggleAllButtonsShortcut = "'" // '
var ToggleNextButtonShortcut = "H";
// ############## SHOW HINTS AUTOMATICALLY ##############
var ButtonAutoReveal = {
"Lecture Notes": false,
"Missed Questions": false,
"Pixorize": false,
"Sketchy": false,
"Additional Resources": false,
}
var ScrollToButton = true;
// ############## TAG SHORTCUT ##############
var toggleTagsShortcut = "C";
// ENTER THE TAG TERM WHICH, WHEN PRESENT, WILL TRIGGER A RED BACKGROUND
var tagID = "XXXYYYZZZ"
// WHETHER THE WHOLE TAG OR ONLY THE LAST PART SHOULD BE SHOWN
var numTagLevelsToShow = 0;
// ############## CLOZE ONE BY ONE ##############
var revealNextShortcut = "N"
var revealNextWordShortcut = "Shift + N"
var toggleAllShortcut = ","
// Changes how "Reveal Next" and clicking behaves. Either "cloze" or "word".
// "word" reveals word by word.
var revealNextClozeMode = "cloze"
// What cloze is hidden with
var clozeHider = (elem) => "👑"
/*
You can replace the above line with below examples. '█' or '_' works well for hiding clozes.
// Fixed length:
var clozeHider = (elem) => "███"
// Replace each character with "█":
var clozeHider = (elem) => "█".repeat(elem.textContent.length)
// Show whitespaces:
var clozeHider = (elem) => "[" + elem.textContent.split(" ").map((t) => "█".repeat(t.length)).join(" ") + "]"
// Color-filled box (doesn't hide images):
var clozeHider = (elem) => `<span style="background-color: red; color: transparent;">${elem.innerHTML}</span>`
*/
// enables selective cloze one-by-one (e.g. only c1 and c3)
// seperate wanted numbers by "," in one-by-one field
var selectiveOneByOne = false;
// if selective one-by-one is disabled, set this to select a min number of clozes necessary to activate 1b1
// can be set to any number to set lower bound, any falsy value (e.g. 0 or null) disables this setting
var minNumberOfClozes = 0;
</script>
<section>
<div class="clozefield">
<p>{{cloze:Text}}</p>
</div>
<!-- ############## EDIT CLOZE DURING REVIEW ##############
-change below (not above) to "edit:cloze:Text" for editable field,
but be sure to have the correct add-on installed-->
<div class="editcloze">
<p>{{edit:cloze:Text}}</p>
</div>
<br>
<!-- ############## TEXT-TO-SPEECH ##############
replace the arrows/dashes from the statement below with double curly brackets-->
{{^One by one}}
<!--tts en_US voices=Apple_Samantha,Microsoft_Zira speed=1.4:cloze-only:Text-->
{{/One by one}}
</section>
<hr>
<section>
<!-- BUTTON FIELDS -->
<!-- ClOZE ONE BY ONE BUTTONS -->
<div id="one-by-one" style="display: none;">{{One by one}}</div>
<div id="1by1-btns" style="display: none;">
<button id="button-reveal-next" class="button-general" onclick="revealNextCloze()">Reveal Next</button>
<button id="button-toggle-all" class="button-general" onclick="toggleAllCloze()">Toggle All</button>
</div>
<script>
(() => {
let clozeOneByOneEnabled = true;
clozeOneByOneEnabled = document.getElementById("one-by-one").textContent !== "";
if (clozeOneByOneEnabled) {
document.getElementById("1by1-btns").style.display = "block";
}
})()
</script>
{{#Lecture Notes}}
<span id = "hint-ln" class="hintBtn" data-name="Lecture Notes">
<a href="#" class="hint" onclick="toggleHintBtn('hint-ln')"></a>
<button id="button-ln" class="button-general" onclick="toggleHintBtn('hint-ln')">
<img src="_AnKingIconSmall.png" style="height:12px"> Lecture Notes
</button>
<div dir="auto" id="notes" class="hints" style="display: none;">{{edit:Lecture Notes}}</div>
</span>
{{/Lecture Notes}}
{{#Missed Questions}}
<span id = "hint-mq" class="hintBtn" data-name="Missed Questions">
<a href="#" class="hint" onclick="toggleHintBtn('hint-mq')"></a>
<button id="button-mq" class="button-general" onclick="toggleHintBtn('hint-mq')">
<img src="_AnKingIconSmall.png" style="height:12px"> Missed Questions
</button>
<div dir="auto" id="missed" class="hints" style="display: none;">{{edit:Missed Questions}}</div>
</span>
{{/Missed Questions}}
<!-- Extra field as margin note -->
{{#Extra}}
<span class="marginnote" id="extra">{{edit:Extra}}</span>
{{/Extra}}
{{#Pixorize}}
<span id = "hint-pixorize" class="hintBtn" data-name="Pixorize">
<a href="#" class="hint" onclick="toggleHintBtn('hint-pixorize')"></a>
<button id="button-pixorize" class="button-general" onclick="toggleHintBtn('hint-pixorize')">
<img src="_pixorize.png" style="height:12px"> Pixorize
</button>
<div dir="auto" id="pixorize" class="hints" style="display: none;">{{edit:Pixorize}}</div>
</span>
{{/Pixorize}}
{{#Sketchy}}
<span id = "hint-sketchy" class="hintBtn" data-name="Sketchy">
<a href="#" class="hint" onclick="toggleHintBtn('hint-sketchy')"></a>
<button id="button-sketchy" class="button-general" onclick="toggleHintBtn('hint-sketchy')">
<img src="_sketchy.icon.png" style="height:12px"> Sketchy
</button>
<div dir="auto" id="sketchy" class="hints" style="display: none;">{{edit:Sketchy}}</div>
</span>
{{/Sketchy}}
{{#Additional Resources}}
<span id = "hint-ar" class="hintBtn" data-name="Additional Resources">
<a href="#" class="hint" onclick="toggleHintBtn('hint-ar')"></a>
<button id="button-ar" class="button-general" onclick="toggleHintBtn('hint-ar')">
<img src="_AnKingIconSmall.png" style="height:12px"> Additional Resources
</button>
<div dir="auto" id="additional" class="hints" style="display: none;">{{edit:Additional Resources}}</div>
</span>
{{/Additional Resources}}
</section>
<!-- ANKING HYPERLINK IMAGE -->
<a href="https://www.ankingmed.com"><img src="_AnKingIconSmall.png" alt="The AnKing" id="pic"></a>
<!-- NOT-PERSISTING EVENT LISTENER -->
<script>
if (window.ankingEventListeners) {
for (const listener of ankingEventListeners) {
const type = listener[0]
const handler = listener[1]
document.removeEventListener(type, handler)
}
}
window.ankingEventListeners = []
window.ankingAddEventListener = function(type, handler) {
document.addEventListener(type, handler)
window.ankingEventListeners.push([type, handler])
}
</script>
<!-- Shortcut Matcher Function -->
<script>
var specialCharCodes = {
"-": "minus",
"=": "equal",
"[": "bracketleft",
"]": "bracketright",
";": "semicolon",
"'": "quote",
"`": "backquote",
"\\": "backslash",
",": "comma",
".": "period",
"/": "slash",
};
// Returns function that match keyboard event to see if it matches given shortcut.
function shortcutMatcher(shortcut) {
let shortcutKeys = shortcut.toLowerCase().split(/[+]/).map(key => key.trim())
let mainKey = shortcutKeys[shortcutKeys.length - 1]
const mainKeyVariants = [];
if (mainKey.length === 1) {
if (/\d/.test(mainKey)) {
// Treat alphanumeric and numpad keys interchangeably
mainKeyVariants.push("digit" + mainKey)
mainKeyVariants.push("numpad" + mainKey)
} else if (/[a-zA-Z]/.test(mainKey)) {
mainKeyVariants.push("key" + mainKey)
} else {
let code = specialCharCodes[mainKey];
if (code) {
mainKey = code
mainKeyVariants.push(code)
} else {
mainKeyVariants.push(mainKey)
}
}
} else {
mainKeyVariants.push(mainKey)
}
let ctrl = shortcutKeys.includes("ctrl")
let shift = shortcutKeys.includes("shift")
let alt = shortcutKeys.includes("alt")
let matchShortcut = function (ctrl, shift, alt, mainKeyVariants, event) {
if (!mainKeyVariants.includes(event.code.toLowerCase())) return false
if (ctrl !== (event.ctrlKey || event.metaKey)) return false
if (shift !== event.shiftKey) return false
if (alt !== event.altKey) return false
return true
}.bind(window, ctrl, shift, alt, mainKeyVariants)
return matchShortcut
}
</script>
<!-- CLICKABLE COLORFUL TAGS -->
{{#Tags}}
<div id="tags-container">{{clickable::Tags}}</div>
<script>
var tagContainer = document.getElementById("tags-container")
var tagList;
if (tagContainer.childElementCount == 0) {
tagList = tagContainer.innerHTML.split(" ");
var kbdList = [];
var newTagContent = document.createElement("div");
for (var i = 0; i < tagList.length; i++) {
var newTag = document.createElement("kbd");
var tag = tagList[i];
// numTagLevelsToShow == 0 means the whole tag should be shown
if(numTagLevelsToShow != 0){
tag = tag.split('::').slice(-numTagLevelsToShow).join("::");
}
newTag.innerHTML = tag;
newTagContent.append(newTag)
}
tagContainer.innerHTML = newTagContent.innerHTML;
tagContainer.style.cursor = "default";
}
else {
tagList = Array.from(tagContainer.children).map(e => e.innerText);
}
globalThis.tagList = tagList.map(t => t.trim().toLowerCase());
if (tagContainer.innerHTML.indexOf(tagID) != -1) {
tagContainer.style.backgroundColor = "rgba(251,11,11,.15)";
}
function showtags() {
var tagContainerShortcut = document.getElementById("tags-container");
if (tagContainerShortcut.style.display
=== "none") {
tagContainerShortcut.style.display = "block";
} else {
tagContainerShortcut.style.display =
"none";
}
}
var isShortcut = shortcutMatcher(toggleTagsShortcut)
ankingAddEventListener('keyup', function (e) {
if (isShortcut(e)) {
showtags();
}
});
</script>
{{/Tags}}
<!-- HINT BUTTONS SETUP -->
<script>
(function() {
window.toggleHintBtn = function(containerId, noScrolling=false) {
const container = document.getElementById(containerId)
const link = container.getElementsByTagName("a")[0]
const button = container.getElementsByTagName("button")[0]
const hint = container.getElementsByTagName("div")[0]
if (hint.style.display == "none") {
button.classList.add("expanded-button")
hint.style.display = "block"
link.style.display = "none"
if (ScrollToButton && !noScrolling) {
hint.scrollIntoView({
behavior: "smooth", // "auto" for instant scrolling
block: "start",
inline: "nearest"
});
}
} else {
button.classList.remove("expanded-button")
hint.style.display = "none"
link.style.display = ""
}
}
window.toggleNextButton = function(){
// adapted from Hint Hotkey add-on
var customEvent = document.createEvent('MouseEvents');
customEvent.initEvent('click', false, true);
var arr = document.getElementsByTagName('a');
for (var i=0; i<arr.length; i++) {
var el = arr[i];
if (
window.getComputedStyle(el).display === 'none' ||
window.getComputedStyle(el.parentElement).display === 'none'
) {
continue;
}
if (el.classList.contains("hint")) {
el.dispatchEvent(customEvent);
break
}
}
}
const isToggleNextShortcut = shortcutMatcher(ToggleNextButtonShortcut)
ankingAddEventListener("keydown", (evt) => {
if (evt.repeat) return
if (isToggleNextShortcut(evt)) {
toggleNextButton()
}
})
const setupHintBtn = function(elem) {
const containerId = elem.id
const fieldName = elem.dataset.name
const container = document.getElementById(containerId)
const button = elem.getElementsByClassName("button")[0]
const tag = `autoopen::${fieldName.toLowerCase().replace(' ', '_')}`
if (ButtonAutoReveal[fieldName] || (globalThis.tagList && globalThis.tagList.includes(tag))) {
toggleHintBtn(containerId, noScrolling=true)
}
const shortcut = ButtonShortcuts[fieldName]
container.title = shortcut ? `Shortcut: ${shortcut}` : ""
const isShortcut = shortcutMatcher(shortcut)
const isToggleAllShortcut = shortcutMatcher(ToggleAllButtonsShortcut)
ankingAddEventListener("keydown", (evt) => {
if (evt.repeat) return
if (isShortcut(evt) || isToggleAllShortcut(evt)) {
toggleHintBtn(containerId)
}
})
}
const hints = document.getElementsByClassName("hintBtn")
for (let i = 0; i < hints.length; i++) {
setupHintBtn(hints[i])
}
})()
</script>
<!-- Credit: Anki Persistence, MIT: https://github.com/SimonLammer/anki-persistence/blob/8190116c95df202773aa692c5e9ae704fb407bc9/script.js -->
<script>
if (typeof(window.Persistence) === 'undefined') {
var _persistenceKey = 'github.com/SimonLammer/anki-persistence/';
var _defaultKey = '_default';
window.Persistence_sessionStorage = function() { // used in android, iOS, web
var isAvailable = false;
try {
if (typeof(window.sessionStorage) === 'object') {
isAvailable = true;
this.clear = function() {
for (var i = 0; i < sessionStorage.length; i++) {
var k = sessionStorage.key(i);
if (k.indexOf(_persistenceKey) == 0) {
sessionStorage.removeItem(k);
i--;
}
};
};
this.setItem = function(key, value) {
if (value == undefined) {
value = key;
key = _defaultKey;
}
sessionStorage.setItem(_persistenceKey + key, JSON.stringify(value));
};
this.getItem = function(key) {
if (key == undefined) {
key = _defaultKey;
}
return JSON.parse(sessionStorage.getItem(_persistenceKey + key));
};
this.removeItem = function(key) {
if (key == undefined) {
key = _defaultKey;
}
sessionStorage.removeItem(_persistenceKey + key);
};
this.getAllKeys = function () {
var keys = [];
var prefixedKeys = Object.keys(sessionStorage);
for (var i = 0; i < prefixedKeys.length; i++) {
var k = prefixedKeys[i];
if (k.indexOf(_persistenceKey) == 0) {
keys.push(k.substring(_persistenceKey.length, k.length));
}
};
return keys.sort()
}
}
} catch(err) {}
this.isAvailable = function() {
return isAvailable;
};
};
window.Persistence_windowKey = function(persistentKey) { // used in windows, linux, mac
var obj = window[persistentKey];
var isAvailable = false;
if (typeof(obj) === 'object') {
isAvailable = true;
this.clear = function() {
obj[_persistenceKey] = {};
};
this.setItem = function(key, value) {
if (value == undefined) {
value = key;
key = _defaultKey;
}
obj[_persistenceKey][key] = value;
};
this.getItem = function(key) {
if (key == undefined) {
key = _defaultKey;
}
return obj[_persistenceKey][key] == undefined ? null : obj[_persistenceKey][key];
};
this.removeItem = function(key) {
if (key == undefined) {
key = _defaultKey;
}
delete obj[_persistenceKey][key];
};
this.getAllKeys = function () {
return Object.keys(obj[_persistenceKey]);
}
if (obj[_persistenceKey] == undefined) {
this.clear();
}
}
this.isAvailable = function() {
return isAvailable;
};
};
/*
* client | sessionStorage | persistentKey | useful location |
* ----------|----------------|---------------|-----------------|
* web | YES | - | NO |
* windows | NO | py | NO |
* android | YES | - | NO |
* linux 2.0 | NO | qt | YES |
* linux 2.1 | NO | qt | YES |
* mac 2.0 | NO | py | NO |
* mac 2.1 | NO | qt | YES |
* iOS | YES | - | NO |
*/
window.Persistence = new Persistence_sessionStorage(); // android, iOS, web
if (!Persistence.isAvailable()) {
window.Persistence = new Persistence_windowKey("py"); // windows, mac (2.0)
}
if (!Persistence.isAvailable()) {
var titleStartIndex = window.location.toString().indexOf('title'); // if titleStartIndex > 0, window.location is useful
var titleContentIndex = window.location.toString().indexOf('main', titleStartIndex);
if (titleStartIndex > 0 && titleContentIndex > 0 && (titleContentIndex - titleStartIndex) < 10) {
window.Persistence = new Persistence_windowKey("qt"); // linux, mac (2.1)
}
}
}
</script>
<!-- AUTOFLIP BACK SCRIPT -->
<script>
// autoflip hides card in front template
document.getElementById("qa").style.removeProperty("display")
</script>
<!-- CLOZE ONE BY ONE SCRIPT -->
<style>
.cloze-replacer:hover {
cursor: pointer;
}
.cloze-hidden {
display: none;
}
.cloze-replacer .hidden {
display: none;
}
#extra.hidden {
display: none;
}
</style>
<!-- Credit: Code for selective 1b1 clozes is adapted from the Ankizin project: https://ankiweb.net/shared/info/2058530482 -->
<script>
(function() {
// enables cloze one-by-one even when one-by-one field is empty
// minNumberOfClozes is still considered in this case
// overridden in importance by selectiveOneByOne
var alwaysOneByOne = false;
var clozeOneByOneEnabled = true;
var oneByOneFieldNotEmpty = document.getElementById("one-by-one").textContent !== "";
clozeOneByOneEnabled = alwaysOneByOne || oneByOneFieldNotEmpty;
// --- CHECK IF ONE BY ONE SHOULD BE ENABLED FOR THIS SPECIFIC CARD ---
/**
* Credit for the getCardNumber function goes to foenixx (GitHub) / ollevolle (AnkiWeb Forum)
*/
const getCardNumber = function () {
clz = document.body.className;
const regex = /card(\d+)/gm;
let m;
if ((m = regex.exec(clz)) !== null) {
return m[1];
} else {
// Should only fire if card is not cloze
console.error("Cannot find cardN class of body element!");
return "0";
}
}
// parse the cloze numbers for which selectiveOneByOne is enabled
var clozeNumbers = document.getElementById("one-by-one").textContent.split(',').filter(element => element).map(Number)
var cardNumberIsOneByOne = !clozeNumbers.filter(n => !Number.isNaN(n)).length || clozeNumbers.includes(parseInt(getCardNumber()))
// check the amount of clozes -> disable OneByOne if less than minimum value wanted (minNumberOfClozes)
var numClozesForNumber = (minNumberOfClozes) ? document.querySelectorAll('.cloze').length : 0
// stop OneByOne if selectiveOneByOne is not enabled for this specific card OR if OneByOne is disabled some other way
// -> show normal backside
if (!alwaysOneByOne && ((selectiveOneByOne && !cardNumberIsOneByOne) || (oneByOneFieldNotEmpty && (numClozesForNumber < minNumberOfClozes)))) {
clozeOneByOneEnabled = false
}
if (!clozeOneByOneEnabled) {
return
}
// Needed for amboss to recognize first word in .cloze-hidden
const CLOZE_REPLACER_SEP = "<span class='hidden'> </span>"
const hideAllCloze = function(initial) {
let clozes = document.getElementsByClassName("cloze")
let count = 0 // hidden cloze count
for (const cloze of clozes) {
const existingHidden = cloze.getElementsByClassName("cloze-hidden")[0]
if (existingHidden) {
revealCloze(cloze);
}
if (cloze.offsetWidth === 0) {
continue
}
const clozeReplacer = document.createElement("span")
const clozeHidden = document.createElement("span")
clozeReplacer.classList.add("cloze-replacer")
clozeHidden.classList.add("cloze-hidden")
while (cloze.childNodes.length > 0) {
clozeHidden.appendChild(cloze.childNodes[0])
}
cloze.appendChild(clozeReplacer)
cloze.appendChild(clozeHidden)
var clozeHints = Persistence.getItem("clozeHints");
if (clozeHints && clozeHints[count]) {
clozeReplacer.classList.add("cloze-hint")
clozeReplacer.innerHTML = clozeHints[count] + CLOZE_REPLACER_SEP
} else {
clozeReplacer.innerHTML = clozeHider(cloze) + CLOZE_REPLACER_SEP
}
count += 1
if (initial) {
cloze.addEventListener("touchend", revealClozeClicked)
cloze.addEventListener("click", revealClozeClicked)
cloze.classList.add("one-by-one");
}
}
// Don't hide the Extra field - let it display as a margin note
// const extra = document.getElementById("extra");
// if (extra) {
// extra.classList.add("hidden");
// }
for (const [field, autoReveal] of Object.entries(ButtonAutoReveal)) {
const container = document.querySelector(`[data-name="${field}"]`)
if (container) {
const tag = `autoopen::${field.toLowerCase().replace(' ', '_')}`
if (autoReveal || (globalThis.tagList && globalThis.tagList.includes(tag))) {
const link = container.getElementsByTagName("a")[0]
const button = container.getElementsByTagName("button")[0]
const hint = container.getElementsByTagName("div")[0]
button.classList.remove("expanded-button")
hint.style.display = "none"
link.style.display = ""
}
}
}
}
const revealCloze = function(cloze) {
const clozeReplacer = cloze.getElementsByClassName("cloze-replacer")[0]
const clozeHidden = cloze.getElementsByClassName("cloze-hidden")[0]
if (!clozeReplacer || !clozeHidden) return;
cloze.removeChild(clozeReplacer)
cloze.removeChild(clozeHidden)
while (clozeHidden.childNodes.length > 0) {
cloze.appendChild(clozeHidden.childNodes[0])
}
maybeRevealExtraField()
}
const revealClozeWord = function(cloze) {
const clozeReplacer = cloze.getElementsByClassName("cloze-replacer")[0]
const clozeHidden = cloze.getElementsByClassName("cloze-hidden")[0]
if (!clozeReplacer || !clozeHidden) return;
let range = new Range()
range.setStart(clozeHidden, 0)
const foundSpace = setRangeEnd(range, clozeHidden, "beforeFirstSpace")
if (!foundSpace) {
range.setEnd(clozeHidden, clozeHidden.childNodes.length)
}
let fragment = range.extractContents()
cloze.insertBefore(fragment, clozeReplacer)
// Extract whitespaces after word
range = new Range()
range.setStart(clozeHidden, 0)
const foundWord = setRangeEnd(range, clozeHidden, "beforeFirstChar")
if (!foundWord) {
range.setEnd(clozeHidden, clozeHidden.childNodes.length)
}
fragment = range.extractContents();
cloze.insertBefore(fragment, clozeReplacer)
if (!foundWord) {
cloze.removeChild(clozeHidden)
cloze.removeChild(clozeReplacer)
maybeRevealExtraField()
return
}
clozeReplacer.innerHTML = clozeHider(clozeHidden) + CLOZE_REPLACER_SEP
if (clozeReplacer.classList.contains("cloze-hint")) [
clozeReplacer.classList.remove("cloze-hint")
]
maybeRevealExtraField()
}
const revealNextClozeOf = (type) => {
const nextHidden = document.querySelector(".cloze-hidden")
if(!nextHidden) {
return
}
const cloze = clozeElOfClozeHidden(nextHidden);
if (type === "word") {
revealClozeWord(cloze)
} else if (type === "cloze") {
revealCloze(cloze)
} else {
console.error("Invalid type: " + type)
}
}
const revealClozeClicked = function(ev) {
let elem = ev.currentTarget
if (!ev.altKey && (revealNextClozeMode !== "word")) {
revealCloze(elem)
} else {
revealClozeWord(elem)
}
ev.stopPropagation()
ev.preventDefault()
}
window.revealNextCloze = function() {
revealNextClozeOf(revealNextClozeMode)
}
window.toggleAllCloze = function() {
let elems = document.querySelectorAll(".cloze-hidden")
if(elems.length > 0) {
for (const elem of elems) {
const cloze = clozeElOfClozeHidden(elem)
revealCloze(cloze)
}
} else {
hideAllCloze(initial=false)
}
}
const clozeElOfClozeHidden = (cloze) => {
while (!cloze.classList.contains("cloze")) {
cloze = cloze.parentElement;
}
return cloze;
}
const maybeRevealExtraField = () => {
let elems = document.querySelectorAll(".cloze-hidden")
if (elems.length == 0) {
// Extra field is always visible as margin note, no need to reveal
// const extra = document.getElementById("extra")
// if (extra) {
// extra.classList.remove("hidden")
// }
// Also reveal autoReveal fields
for (const [field, autoReveal] of Object.entries(ButtonAutoReveal)) {
const container = document.querySelector(`[data-name="${field}"]`)
if (container) {
const tag = `autoopen::${field.toLowerCase().replace(' ', '_')}`
if (autoReveal || (globalThis.tagList && globalThis.tagList.includes(tag))) {
const link = container.getElementsByTagName("a")[0]
const button = container.getElementsByTagName("button")[0]
const hint = container.getElementsByTagName("div")[0]
button.classList.add("expanded-button")
hint.style.display = "block"
link.style.display = "none"
}
}
}
}
}
/**
* mode: 'beforeFirstSpace' or 'beforeFirstChar'
* Return `true` if it exists and setEnd() was called, otherwise `false`
*/
const setRangeEnd = function(range, node, mode) {
if (node.nodeType === Node.TEXT_NODE) {
const regex = mode === 'beforeFirstSpace' ? /\s/ : /\S/
const match = node.textContent.match(regex)
if (match) {
if (match.index === 0) {
while (node.previousSibling === null) {
node = node.parentElement
}
range.setEndBefore(node)
} else {
range.setEnd(node, match.index);
}
return true;
} else {
return false;
}
} else if (mode === 'beforeFirstChar' && isCharNode(node)) {
range.setEndBefore(node)
return true
} else if (!ignoreSpaceInNode(node)) {
for (const child of node.childNodes) {
if (setRangeEnd(range, child, mode)) {
return true;
}
}
return false;
}
}
const ignoreSpaceInNode = function (node) {
return node.tagName === "MJX-ASSISTIVE-MML"
}
const isCharNode = function(node) {
return ["IMG", "MJX-CONTAINER"].includes(node.tagName)
}
hideAllCloze(initial=true)
const toggleAllButton = document.getElementById("button-toggle-all")
const revealNextButton = document.getElementById("button-reveal-next")
toggleAllButton.title = `Shortcut: ${window.toggleAllShortcut}`;
revealNextButton.title = `Shortcut: ${window.revealNextShortcut}`;
let isShowNextShortcut = shortcutMatcher(window.revealNextShortcut)
let isShowWordShortcut = shortcutMatcher(window.revealNextWordShortcut)
let isToggleAllShortcut = shortcutMatcher(window.toggleAllShortcut)
ankingAddEventListener("keydown", (ev) => {
let next = isShowNextShortcut(ev)
let word = isShowWordShortcut(ev)
let all = isToggleAllShortcut(ev)
if (next) {
revealNextClozeOf("cloze")
} else if (word) {
revealNextClozeOf("word")
} else if (all) {
toggleAllCloze()
} else {
return;
}
ev.stopPropagation()
ev.preventDefault()
});
})()
</script>
<!-- WIKIPEDIA SEARCHES -->
<div id="popup-container">
<button id="close-popup-btn" onclick="closePopup(true)">×</button>
<a id="open-wiki-btn" href="">↪</a>
<div id="tc"></div>
<div id="fadebottom_v"></div>
<div id="ic"><img id="popup-image"></div>
</div>
<style>
#tc {
color: #222222;
position: absolute;
top: 16px;
margin: 0px;
left: 15px;
text-decoration: none;
height: 320px;
overflow: hidden;
overflow-y: scroll;
white-space: pre-wrap;
width: 300px;
}
#tc p {
margin: 0px;
}
#tc::-webkit-scrollbar {
display: none;
}
#fadebottom_v {
height: 30px;
width: 300px;
background: -webkit-linear-gradient(270deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 1));
z-index: 111;
position: absolute;
bottom: 0px;
left: 15px;
}
#hc {
color: #666;
font-weight: bold;
}
#ic {
right: 0px;
top: 30px;
position: absolute;
}
#ic img {
width: 160px;
height: auto;
object-fit: cover;
overflow: hidden;
}
#popup-image {
width: 140px;
height: auto;
}
#popup-container {
background: #fff;
position: absolute;
bottom: 30px;
right: 10px;
z-index: 110;
-webkit-box-shadow: 0 30px 90px -20px rgba(0, 0, 0, 0.3), 0 0 1px 1px rgba(0, 0, 0, 0.05);
box-shadow: 0 30px 90px -20px rgba(0, 0, 0, 0.3), 0 0 1px 1px rgba(0, 0, 0, 0.05);
padding: 0;
display: none;
font-size: 17px;
line-height: 20px;
border-radius: 2px;
width: 480px;
height: 340px;
overflow: hidden;
font-family: Arial;
text-align: left;
border: 1px solid #d0d0d0;
border-radius: 5px;
}
#close-popup-btn {
position: absolute;
top: 1px;
right: 5px;
width: 32px;
height: 32px;
background: none;
border: 0;
cursor: pointer;
font-family: 'Josefin Sans', sans-serif;
font-size: 20px;
outline: none;
text-align: right;
z-index: 112;
}
#open-wiki-btn {
position: absolute;
top: 10px;
right: 30px;
width: 15px;
height: 32px;
background: none;
border: 0;
cursor: pointer;
text-decoration: none;
color: #222222;
font-family: 'Josefin Sans', sans-serif;
font-size: 17px;
outline: none;
text-align: left;
z-index: 112;
}
</style>
<script>
function getSummaryFor(word) {
word = word.replace(/^[\.,\/#\!$%\^&\*;:{}=\-_`~() \'\s]+|[\.,\/#\!$%\^&\*;:{}=\-_`~()\'\s]+$/g, "");
var pc = document.getElementById("popup-container");
var hc = document.getElementById("hc");
var tc = document.getElementById("tc");
var ic = document.getElementById("ic");
var imgelem = document.getElementById("popup-image");
imgelem.src = "";
var shortsum = "";
fetch("https://en.wikipedia.org/api/rest_v1/page/summary/" + word)
.then(function (response) { return response.json(); })
.then(function (response) {
shortsum = response.description;
shortsum = shortsum.replace(/(Disambiguation.*)/g, "Disambiguation");
tc.innerHTML = "<span id='hc'>" + capfl(shortsum) + "</span>" + "\n" + response.extract_html + "\n";
tc.style.width = "420px";
if (response.extract_html && !response.extract.endsWith("to:")) {
pc.style.display = "block";
document.getElementById("open-wiki-btn").href = response.content_urls.desktop.page;
} else {
pc.style.display = "none";
}
if (!response.thumbnail.source || response.type === "disambiguation") {
tc.style.width = "420px";
} else {
tc.style.width = "300px"; imgelem.src = response.thumbnail.source;
}
})
.catch(function (error) {
console.log(error);
});
}
function closePopup(deselectAlso = false) {
pcc.style.display = 'none';
if (deselectAlso) { clearSelection(); }
}
var pcc = document.getElementById("popup-container");
var prevSel = "";
ankingAddEventListener('click', function () {
var currentSelection = getSelectionText();
if (currentSelection !== "") { prevSel = currentSelection; }
if (currentSelection && !mustClickW) {
getSummaryFor(currentSelection);
} else { closePopup(); }
});
ankingAddEventListener('keyup', function (e) {
if (e.key == "w") {