Advertisement
Xelieu

(Simple - Front) Mining Deck (Xelieu's)

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