Audio Tweak 4.0

Plays review audios on meaning answers as long as the reading has been got correctly beforehand

当前为 2023-05-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Audio Tweak 4.0
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Plays review audios on meaning answers as long as the reading has been got correctly beforehand
  6. // @author Hubbit200
  7. // @match https://www.wanikani.com/subjects/review
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Initialize an empty array to store the words and their information
  17. var wordList = [];
  18. var hasPlayedAlready = false;
  19. var queueElement = document.body.querySelector(`[id="quiz-queue"]`).firstElementChild;
  20.  
  21. // Listen for the Turbo event "didAnswerQuestion"
  22. function handleDidAnswerQuestion() {
  23. // Get the word from the div with class "character-header__characters"
  24. let word = document.querySelector(".character-header__characters").textContent;
  25. // Get the question type from the span with class "quiz-input__question-type"
  26. let questionType = document.querySelector(".quiz-input__question-type").textContent;
  27. // Get item type
  28. let itemType = document.querySelector(".quiz-input__question-category").textContent;
  29.  
  30. // Check if the word is already in the list
  31. let index = wordList.findIndex(function(item) {
  32. return item.word === word;
  33. });
  34.  
  35. // If the word is not in the list
  36. if (index === -1 && itemType == "Vocabulary") {
  37. // Create a new object with the word, audio link and isReadingComplete properties
  38. let newItem = {
  39. word: word,
  40. audioLink: null,
  41. isReadingComplete: false
  42. };
  43. // Add the new item to the list
  44. wordList.push(newItem);
  45. }
  46. // If the word is already in the list
  47. else if (index !== -1) {
  48. if(hasPlayedAlready) return;
  49. // Get the existing item from the list
  50. let existingItem = wordList[index];
  51.  
  52. // If isReadingComplete is true
  53. if (existingItem.isReadingComplete) {
  54. // Play the audio link
  55. if(existingItem.audioLink == null) return;
  56. let audio = new Audio(existingItem.audioLink);
  57. if(audio == null) console.log("Invalid Audio");
  58. else {
  59. audio.play();
  60. hasPlayedAlready = true;
  61. let audioButton = document.querySelector('[data-action="quiz-audio#play"]');
  62. let audioIcon = audioButton.querySelector('i');
  63. audioButton.onclick = function() { if(audio.paused) { audio.play(); audioIcon.classList.remove("fa-volume-off"); audioIcon.classList.add("fa-volume-high"); audioButton.classList.remove("additional-content__item--disabled");} }
  64. audio.onended = function() { audioIcon.classList.remove("fa-volume-high"); audioIcon.classList.add("fa-volume-off"); }
  65. setTimeout(function() { audioButton.classList.remove("additional-content__item--disabled"); }, 100);
  66. }
  67. }
  68. }
  69. }
  70.  
  71. // Listen for the Turbo event "didCompleteSubject"
  72. function handleDidCompleteSubject() {
  73. // Get the word from the div with class "character-header__characters"
  74. let word = document.querySelector(".character-header__characters").textContent;
  75.  
  76. // Check if the word is in the list
  77. let index = wordList.findIndex(function(item) {
  78. return item.word === word;
  79. });
  80.  
  81. // If the word is in the list
  82. if (index !== -1) {
  83. // Remove it from the list
  84. wordList.splice(index, 1);
  85. }
  86. }
  87.  
  88. function handleWillShowNextQuestion() {
  89. hasPlayedAlready = false;
  90. // Get the question type from the span with class "quiz-input__question-type"
  91. let questionType = document.querySelector(".quiz-input__question-type").textContent;
  92. // Get item type
  93. let itemType = document.querySelector(".quiz-input__question-category").textContent;
  94. // Check if previous item was correct
  95. let wasPrevCorrect = document.querySelector(".quiz-input__input-container[correct='true']");
  96.  
  97. let audioButton = document.querySelector('[data-action="quiz-audio#play"]');
  98. if (audioButton.onclick != null) audioButton.onclick = null;
  99.  
  100. if (questionType.includes("reading") && itemType.includes("Vocabulary") && wasPrevCorrect != undefined) {
  101. // Get the word from the div with class "character-header__characters"
  102. let word = document.querySelector(".character-header__characters").textContent;
  103. // Check if the word is already in the list
  104. let listItem = wordList[wordList.findIndex(function(item) {
  105. return item.word === word;
  106. })];
  107.  
  108. // If listItem is null, break
  109. if (listItem == null) return;
  110.  
  111. // Get the audio link from the queue
  112. let audioObj = JSON.parse(queueElement.textContent).find(obj => {
  113. return obj.type == "Vocabulary" && obj.characters == word;
  114. });
  115. // Set the audio link and isReadingComplete to true
  116. listItem.audioLink = audioObj.readings[0].pronunciations[0].sources[0].url;
  117. listItem.isReadingComplete = true;
  118. }
  119. }
  120.  
  121. // Add event listeners for both events using addEventListener
  122. window.addEventListener("didAnswerQuestion", handleDidAnswerQuestion);
  123. window.addEventListener("didCompleteSubject", handleDidCompleteSubject);
  124. window.addEventListener("willShowNextQuestion", handleWillShowNextQuestion);
  125. })();