Greasy Fork is available in English.

WK Review Audio Switch

Add switches to lessons and reviews to toggle autoplaying audio on or off

Verzia zo dňa 06.04.2022. Pozri najnovšiu verziu.

// ==UserScript==
// @name         WK Review Audio Switch
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add switches to lessons and reviews to toggle autoplaying audio on or off
// @author       Gorbit99
// @match        https://www.wanikani.com/lesson/session
// @match        https://preview.wanikani.com/lesson/session
// @match        https://www.wanikani.com/review/session
// @match        https://preview.wanikani.com/review/session
// @match        https://www.wanikani.com/extra_study/session*
// @match        https://preview.wanikani.com/extra_study/session*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';

  let autoplay = window.audioAutoplay;

  const inLessons = location.href.includes("lesson");
  const inReviews = location.href.includes("review");
  const inExtraStudy = location.href.includes("extra_study");

  function addSwitch() {
    const buttonContainer = document.querySelector("#summary-button");

    const button = document.createElement("span");
    button.innerHTML = `
    <i class="fa fa-volume-${autoplay ? "up" : "off"}"></i>
    `;

    button.style.color = "#fff";
    button.style.opacity = "0.5";
    button.style.cursor = "pointer";
    button.style.width = "1em";
    button.style.display = "inline-block";

    let isTemp = false;

    button.addEventListener("click", () => {
      autoplay = !autoplay;
      window.audioAutoplay = autoplay;
      isTemp = false;

      const oldClass = autoplay ? "fa-volume-off" : "fa-volume-up";
      const newClass = autoplay ? "fa-volume-up" : "fa-volume-off";
      button.style.color = "#fff";

      button.querySelector("i").classList.replace(oldClass, newClass);

      handleChange();
    });

    button.addEventListener("contextmenu", (e) => {
      autoplay = !autoplay;
      window.audioAutoplay = autoplay;
      isTemp = !isTemp;

      const oldClass = autoplay ? "fa-volume-off" : "fa-volume-up";
      const newClass = autoplay ? "fa-volume-up" : "fa-volume-off";
      button.style.color = isTemp ? "red" : "white";

      button.querySelector("i").classList.replace(oldClass, newClass);

      e.preventDefault();
    });

    buttonContainer.append(button);
  };

  function handleChange() {
    let content = "_method=patch";

    if (inLessons) {
      content += `&user%5Blessons_autoplay_audio%5D=${autoplay}`;
    } else if (inReviews) {
      content += `&user%5Breviews_autoplay_audio%5D=${autoplay}`;
    } else if (inExtraStudy) {
      content += `&user%5Bextra_study_autoplay_audio%5D=${autoplay}`;
    }

    fetch("https://www.wanikani.com/settings/app.json", {
      "headers": {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "x-csrf-token": "EBHRTjOfKhyYVt4Izr3a732VWu60zgTjyMkNg1Z24LIrynPOMB7iqOELNQRYfWvhHfpS08_SxPW9vBUKqwIDgw",
      },
      "body": content,
      "method": "POST",
    });
  }

  addSwitch();
})();