// ==UserScript== // @name WaniKani Review Countdown Timer // @namespace ajpazder // @description Adds a time limit to review questions. // @version 1.0.0 // @author Johnathon Pazder // @copyright 2016+, Johnathon Pazder // @license MIT; http://opensource.org/licenses/MIT // @include http*://www.wanikani.com/review/session* // @run-at document-end // @grant none // @downloadURL none // ==/UserScript== // Change this if you want to make things easier (or harder). var timeLimitSeconds = 10; onReviewItemChange(function () { startCountdown(timeLimitSeconds); }); function onReviewItemChange(callback) { // questionType is always updated, even when switching between // radicals (which only have one question type). $.jStorage.listenKeyChange('questionType', callback); } var countdown; function startCountdown(seconds) { // This function could potentially be called multiple times on // the same item so, just to be safe, we'll clear any existing // counter interval before we start a new one. clearInterval(countdown); var timeRemaining = seconds * 1000; var updateInterval = 100; // ms countdown = setInterval(function () { if (answerAlreadySubmitted()) { clearInterval(countdown); return; } var displayTime = (timeRemaining / 1000).toFixed(1); updateCountdownDisplay(displayTime); if (timeRemaining === 0) { clearInterval(countdown); submitWrongAnswer(); return; } timeRemaining -= updateInterval; }, updateInterval); } function answerAlreadySubmitted() { return $("#user-response").is(":disabled"); } function submitWrongAnswer() { if (isReadingQuestion()) { setResponseTo('これをわからない'); } else { setResponseTo('I don\'t know this'); } submitAnswer(); } function isReadingQuestion() { return $('#question-type').hasClass('reading'); } function setResponseTo(value) { $('#answer-form input').val(value); } function submitAnswer() { $('#answer-form button').click(); } function updateCountdownDisplay(time) { // If this is only called once per question change, the counter doesn't show // for some reason. There's probably some other JS running that overwrites it. if ($('#countdown').length === 0) { $('#question-type h1').append(' (s)'); } $('#countdown').text(time); }