Guest User

Wanikani hide context sentences updated

a guest
Oct 3rd, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. // ==UserScript==
  2. // @name WaniKani Hide Context Sentences and Meaning Header
  3. // @namespace rfindley
  4. // @description Hide context sentences and meaning header until hovered.
  5. // @version 2.3.0
  6. // @match https://www.wanikani.com/*
  7. // @match https://preview.wanikani.com/*
  8. // @copyright 2015+, Robin Findley
  9. // @license MIT; http://opensource.org/licenses/MIT
  10. // @run-at document-start
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function(gobj) {
  15.  
  16. /* global app_load, page_load, before_page_render, frame_load, before_frame_render */
  17.  
  18. const match_patterns = [
  19. '/subjects/extra_study',
  20. '/subject-lessons/*',
  21. '/subjects/*/lesson',
  22. '/subjects/review',
  23. '/vocabulary/*'
  24. ];
  25. function url_matches(patterns,url) {patterns=patterns||match_patterns;url=url||window.location.pathname;if(url[0]!=='/')url=new URL(url).pathname;return ((Array.isArray(patterns)?patterns:[patterns]).findIndex((pattern)=>{let regex=new RegExp(pattern.replace(/[.+?^${}()|[\]\\]/g,'\\$&').replaceAll('*','.*'));return (regex.test(url));})>=0);}
  26. function is_turbo_page() {return (document.querySelector('script[type="importmap"]')?.innerHTML.match('@hotwired/turbo') != null);}
  27.  
  28. if (is_turbo_page()) {
  29. try {app_load();} catch(e){}
  30. try {document.documentElement.addEventListener('turbo:load', page_load);} catch(e){}
  31. try {document.documentElement.addEventListener('turbo:before-render', before_page_render);} catch(e){}
  32. try {document.documentElement.addEventListener('turbo:frame-load', frame_load);} catch(e){}
  33. try {document.documentElement.addEventListener('turbo:before-frame-render', before_frame_render);} catch(e){}
  34. } else {
  35. try {app_load();} catch(e){}
  36. try {page_load({detail:{url:window.location.href},target:document.documentElement});} catch(e){}
  37. try {frame_load({target:document.documentElement});} catch(e){}
  38. }
  39.  
  40. function app_load() {
  41. if (!url_matches()) return;
  42.  
  43. // Insert CSS to hide both context sentences and meaning header
  44. document.head.insertAdjacentHTML('beforeend',`
  45. <style name="hide_context_sentence_and_meaning" type="text/css">
  46. .context-sentence-group p:not(:hover),
  47. .subject-collocations__collocation-text:not(:hover),
  48. .context-sentences .wk-text:not(:hover),
  49. .character-header__meaning:not(:hover)
  50. {
  51. background-color:#ccc;
  52. color:#ccc;
  53. text-shadow:none;
  54. }
  55. </style>
  56. `);
  57. }
  58.  
  59. function page_load(e) { // e = {detail: {url: '...'}, target: <elem> }
  60. if (!url_matches()) return;
  61. add_new_context_sentences(e.target);
  62. hide_meaning_header(e.target);
  63. }
  64.  
  65. function before_page_render(e) { // e = {detail: {newBody: <elem>} }
  66. if (!url_matches()) return;
  67. add_new_context_sentences(e.detail.newBody);
  68. hide_meaning_header(e.detail.newBody);
  69. }
  70.  
  71. function before_frame_render(e) { // e = {detail: {newFrame: <elem>} }
  72. if (!url_matches()) return;
  73. add_new_context_sentences(e.detail.newFrame);
  74. hide_meaning_header(e.detail.newFrame);
  75. }
  76.  
  77. function add_new_context_sentences(target) {
  78. // Add '.context-sentence-group' to "Context Sentences" sections.
  79. Array.from(target.querySelectorAll('.subject-section__subtitle'))
  80. ?.find((node) => node.textContent.match('Context Sentences'))
  81. ?.closest('section')
  82. ?.querySelectorAll('.subject-section__text')
  83. ?.forEach((elem) => elem.classList.add('context-sentence-group'));
  84.  
  85. // Hide both Japanese and English sentences
  86. Array.from(target.querySelectorAll('.context-sentences__sentence p'))
  87. ?.forEach((elem) => {
  88. elem.style.backgroundColor = '#ccc';
  89. elem.style.color = '#ccc';
  90. elem.style.textShadow = 'none';
  91.  
  92. elem.addEventListener('mouseover', () => {
  93. elem.style.backgroundColor = '';
  94. elem.style.color = '';
  95. });
  96.  
  97. elem.addEventListener('mouseout', () => {
  98. elem.style.backgroundColor = '#ccc';
  99. elem.style.color = '#ccc';
  100. });
  101. });
  102. }
  103.  
  104. function hide_meaning_header(target) {
  105. // Hide the 'character-header__meaning' div
  106. Array.from(target.querySelectorAll('.character-header__meaning'))
  107. ?.forEach((elem) => {
  108. elem.style.backgroundColor = '#ccc';
  109. elem.style.color = '#ccc';
  110. elem.style.textShadow = 'none';
  111.  
  112. elem.addEventListener('mouseover', () => {
  113. elem.style.backgroundColor = '';
  114. elem.style.color = '';
  115. });
  116.  
  117. elem.addEventListener('mouseout', () => {
  118. elem.style.backgroundColor = '#ccc';
  119. elem.style.color = '#ccc';
  120. });
  121. });
  122. }
  123.  
  124. }());
  125.  
Advertisement
Add Comment
Please, Sign In to add comment