WaniKani Show Hidden Allowed Answers

Adds a section below the "primary" and "alternative" answers to show the hidden "allowed" answers when any exist

目前為 2024-06-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         WaniKani Show Hidden Allowed Answers
// @description  Adds a section below the "primary" and "alternative" answers to show the hidden "allowed" answers when any exist
// @version      1.0.0
// @author       Inserio
// @namespace    wkshowhiddenallowedanswers
// @match        https://www.wanikani.com/*
// @match        https://preview.wanikani.com/*
// @require      https://gf.qytechs.cn/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1380162
// @license      MIT; http://opensource.org/licenses/MIT
// @run-at       document-body
// @grant        none
// ==/UserScript==
/* jshint esversion: 8 */
/* global wkof, wkItemInfo */
(function() {
    'use strict';

    const scriptId = 'wk-show-hidden-allowed-answers', scriptName = 'WaniKani Show Hidden Allowed Answers';
    const state = {
        itemsEl: null,
        sectionEl: null,
    };

    main();

    function main() {
        if (!window.wkof) {
            if (confirm(scriptName+' requires Wanikani Open Framework.\nDo you want to be forwarded to the installation instructions?'))
                window.location.href = 'https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549';
            return;
        }

        wkof.include('Apiv2');
        wkof.ready('Apiv2')
            .then(createSectionElements)
            .then(() => wkItemInfo.under('meaning').notifyWhenVisible(onMeaningVisible));
    }

    async function onMeaningVisible(itemInfo) {
        try {
            await addAllowedAnswers(itemInfo);
        } catch (e) {
            console.error(`Error while adding allowed answers section: ${e.message}`);
        }
    }

    async function addAllowedAnswers(itemInfo) {
        let prevSibling;
        const meaningsSubsections = document.querySelectorAll('#section-meaning .subject-section__meanings');
        for (let i = 0; i < meaningsSubsections.length; i++){
            let subsection = meaningsSubsections[i];
            let sectionText = subsection.querySelector('.subject-section__meanings-title').innerText;
            if (sectionText === 'User Synonyms' || sectionText === 'Word Type')
                break;
            prevSibling = subsection;
        }
        if (prevSibling === undefined)
            return;

        const auxiliaryMeanings = await getHiddenAllowedAnswers(itemInfo);
        if (auxiliaryMeanings.length <= 0)
            return;

        state.itemsEl.textContent = auxiliaryMeanings.map(item => item.meaning).join(', ');
        prevSibling.insertAdjacentElement('afterend', state.sectionEl);
    }

    async function createSectionElements() {
        state.sectionEl = document.createElement("div");
        const titleEl = document.createElement("h2");
        state.itemsEl = document.createElement("p");

        state.sectionEl.setAttribute("id", `${scriptId}-container`);
        state.sectionEl.classList.add('subject-section__meanings', scriptId);
        titleEl.classList.add('subject-section__meanings-title', scriptId);
        titleEl.textContent = 'Allowed';
        state.itemsEl.classList.add('subject-section__meanings-items', scriptId);
        state.itemsEl.textContent = '';

        state.sectionEl.appendChild(titleEl);
        state.sectionEl.appendChild(state.itemsEl);
    }

    async function getHiddenAllowedAnswers(itemInfo) {
        const wkItem = await getItemFromWaniKaniEndpoint(itemInfo);
        const auxiliaryMeanings = [...wkItem.auxiliary_meanings];
        return auxiliaryMeanings.filter((aux_meaning) => aux_meaning.type === 'whitelist');
    }

    async function getItemFromWaniKaniEndpoint(item) {
        return await wkof.Apiv2.get_endpoint(`subjects/${item.id}`, {force_update: false, disable_progress_dialog: true});
    }

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址