Advertisement
Xelieu

(Modern - Front) Core 2.3K Deck (Anacreon's)

Feb 17th, 2023 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <div id="expression">
  2. <div class="font-big">
  3. <a href="https://jisho.org/search/{{Word}}">{{edit:Word}}</a>
  4. <!-- No link replace: {{edit:Word}} -->
  5. <div style="line-height: 15%;">
  6. <br>
  7. </div>
  8. </div>
  9. </div>
  10.  
  11.  
  12. <div id="hintSentence" class="font-small" style="display: none">
  13. {{#Sentence}}
  14. <br><br>
  15. <div class="sentence">
  16. <div class="example_jp">
  17. {{edit:Sentence}}
  18. </div>
  19. </div>
  20. {{/Sentence}}
  21. </div>
  22.  
  23.  
  24. <script>
  25. (function () {
  26. // prevent loading front js on back side of card
  27. if (document.getElementById('answer')) {
  28. return;
  29. }
  30.  
  31. const expression = '{{Word}}';
  32. const furigana = '{{Reading}}';
  33.  
  34. const kanjiRegex = /[\u4e00-\u9faf]/g;
  35.  
  36. // if vocab word isn't kanji add hint sentence
  37. if (!expression.match(kanjiRegex)) {
  38. const hintSentence = document.getElementById('hintSentence');
  39. hintSentence.style.display = 'block';
  40. const sentenceElement = document.querySelector('#hintSentence a');
  41.  
  42. highlightWord(sentenceElement, expression, furigana);
  43. }
  44. })();
  45.  
  46. // run on sentence regardless of having highlight or not
  47. function highlightWord(sentenceElement, expression, furigana) {
  48. const sentence = sentenceElement.innerHTML;
  49.  
  50. // replace html tags in link
  51. const href = decodeURIComponent(sentenceElement.getAttribute('href'));
  52. sentenceElement.href = href.replace(/<[^>]*>/g, '');
  53.  
  54. if (!sentence.match('<strong>')) {
  55. let replace = expression;
  56. // shorten kanji expression
  57. replace = shorten(replace, sentence, 1);
  58. // try furi if kanji didnt work
  59. replace = sentence.match(replace) ? replace : shorten(furigana, sentence, 2);
  60. // try katakana
  61. replace = sentence.match(replace)
  62. ? replace
  63. : shorten(hiraganaToKatakana(furigana), sentence, 2);
  64.  
  65. sentenceElement.innerHTML = sentenceElement.innerHTML.replace(
  66. new RegExp(replace, 'g'),
  67. `<strong>${replace}</strong>`
  68. );
  69. }
  70. }
  71.  
  72. // takes an expression and shortens it until it's in the sentence
  73. function shorten(expression, sentence, minLength) {
  74. while (expression.length > minLength && !sentence.match(expression)) {
  75. expression = expression.substr(0, expression.length - 1);
  76. }
  77. return expression;
  78. }
  79.  
  80. function hiraganaToKatakana(hiragana) {
  81. return hiragana.replace(/[\u3041-\u3096]/g, function (c) {
  82. return String.fromCharCode(c.charCodeAt(0) + 0x60);
  83. });
  84. }
  85. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement