WaniKani Time's Up

Tell when you have taken too long for a question

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         WaniKani Time's Up
// @namespace    http://www.wanikani.com
// @version      0.1.4
// @description  Tell when you have taken too long for a question
// @author       polv
// @match        https://www.wanikani.com/extra_study/session*
// @match        https://www.wanikani.com/review/session*
// @match        https://www.wanikani.com/subjects/*
// @match        https://preview.wanikani.com/extra_study/session*
// @match        https://preview.wanikani.com/review/session*
// @match        https://preview.wanikani.com/subjects/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
// @license      MIT
// @homepage     https://greasyfork.org/en/scripts/478277-wanikani-time-s-up
// @supportURL   https://community.wanikani.com/t/what-do-you-want-now-request-extensions-here/3838/1710
// @grant        none
// ==/UserScript==

// @ts-check
(function () {
  'use strict';

  const TIMEUP_SECONDS = 5;

  let currentSubject = null;
  let currentQuestionType = '';
  let currentAnswer = '';

  /** @type {HTMLInputElement | null} */
  let inputContainer = null;

  addEventListener('willShowNextQuestion', (e) => {
    // @ts-ignore
    const { detail } = e;

    currentSubject = detail.subject;
    currentQuestionType = detail.questionType;

    if (!inputContainer) {
      inputContainer = document.querySelector('input[name="user-response"]');
      if (inputContainer) {
        const el = inputContainer;
        el.addEventListener('keydown', (ev) => {
          if (!currentAnswer) return;
          if (ev.key === 'Escape' || ev.code === 'Escape') {
            el.value = currentAnswer;
          }
        });
      }
    }

    if (inputContainer) {
      const el = inputContainer;
      setTimeout(() => {
        if (
          detail.subject === currentSubject &&
          detail.questionType === currentQuestionType
        ) {
          currentAnswer = el.value;

          // https://community.wanikani.com/t/userscript-i-dont-know-button/7231
          el.value =
            detail.questionType === 'reading'
              ? 'あああああ・むりだあああああ'
              : 'Aargh! What does that even mean? (╯°□°)╯︵ ┻━┻';
        }
      }, 1000 * TIMEUP_SECONDS);
    }
  });

  addEventListener('didAnswerQuestion', () => {
    clear();
  });

  function clear() {
    currentSubject = null;
    currentQuestionType = '';
    currentAnswer = '';
  }
})();