Input multiple readings/meanings into Wanikani
目前為
// ==UserScript==
// @name WaniKani Multiple Answer Input (2023)
// @namespace http://www.wanikani.com
// @version 2.0.1
// @description Input multiple readings/meanings into Wanikani
// @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://gf.qytechs.cn/en/scripts/465750-wanikani-please-check-spelling
// @supportURL https://community.wanikani.com/t/userscript-multiple-answer-input-revamped/49075/44
// @source https://github.com/patarapolw/wanikani-userscript/blob/master/userscripts/mulitple-answer.user.js
// @grant none
// ==/UserScript==
// @ts-check
(function () {
'use strict';
/** @typedef {'whitelist' | 'blacklist' | 'warning'} AuxiliaryType */
/**
* @typedef {{
* questionType: string
* item: {
* type: string
* characters: string
* readings?: string[]
* auxiliary_readings?: {
* reading: string
* type: AuxiliaryType
* }[]
* meanings: string[]
* auxiliary_meanings: {
* meaning: string
* type: AuxiliaryType
* }[]
* }
* userSynonyms: string[]
* response: string
* }} EvaluationParam
*/
/**
* @typedef {{
* action: 'pass' | 'fail' | 'retry'
* message: null | {
* text: string
* type: 'itemInfoException' | 'answerException'
* }
* }} Evaluation
*/
/** @typedef {((e: EvaluationParam) => Evaluation)} EvaluationFunction */
/** @typedef {((e: EvaluationParam, check: EvaluationFunction) => Evaluation | null)} TryEvaluationFunction */
class ModAnswerChecker {
/**
* @type {TryEvaluationFunction[]}
*/
mods = [];
/**
*
* @param {TryEvaluationFunction} fn
*/
register(fn) {
this.mods.push(fn);
}
constructor() {
// Automatically init on new instance
this.init();
}
async init() {
const answerChecker = await this.getAnswerChecker(60000);
answerChecker.oldEvaluate = answerChecker.evaluate.bind(answerChecker);
/** @type {(fns: TryEvaluationFunction[]) => EvaluationFunction} */
const evaluateWith = (fns) => {
return (e) => {
for (const fn of fns) {
const r = fn(e, evaluateWith(fns.filter((it) => it !== fn)));
if (r) return r;
}
return answerChecker.oldEvaluate(e);
};
};
answerChecker.evaluate = evaluateWith(this.mods);
}
/**
* Get answerChecker Object
* @param {number} timeout
* @returns {Promise<{
* oldEvaluate: EvaluationFunction
* evaluate: EvaluationFunction
* }>}
*/
async getAnswerChecker(timeout) {
//Stimulus.controllers.filter((x)=>{return x.answerChecker;})[0]
const start = Date.now();
function waitForAnswerChecker(resolve, reject) {
// @ts-ignore
const Stimulus = window.Stimulus;
if (
Stimulus &&
Stimulus.controllers.filter((x) => {
return x.answerChecker;
})[0]
) {
var answerChecker = Stimulus.controllers.filter((x) => {
return x.answerChecker;
})[0].answerChecker;
resolve(answerChecker);
} else if (timeout && Date.now() - start >= timeout)
reject(new Error('timeout'));
else setTimeout(waitForAnswerChecker.bind(this, resolve, reject), 30);
}
return new Promise(waitForAnswerChecker);
}
}
// @ts-ignore
window.modAnswerChecker = window.modAnswerChecker || new ModAnswerChecker();
/** @type {ModAnswerChecker} */
// @ts-ignore
const modAnswerChecker = window.modAnswerChecker;
//////////////////////////////////////////////////////////////////////////////
/**
* !Multiple Answer Input section
* @see https://community.wanikani.com/t/userscript-multiple-answer-input-revamped/49075/44
*/
modAnswerChecker.register((e, tryCheck) => {
const splitter = e.questionType === 'reading' ? /・/g : /;/g;
/** @type {Record<string, Evaluation[]>} */
const evalActionMap = {};
for (const subResponse of e.response.split(splitter)) {
let response = subResponse.trim();
if (!response) continue;
const result = tryCheck({ ...e, response });
const sect = evalActionMap[result.action] || [];
sect.push(result);
evalActionMap[result.action] = sect;
}
for (const actionType of ['fail', 'retry', 'pass']) {
if (evalActionMap[actionType]) {
return (
evalActionMap[actionType].find((r) => r.message) ||
evalActionMap[actionType][0]
);
}
}
return null;
});
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址