// ==UserScript== // @name Wanikani Progressive Japanese UI // @namespace https://github.com/EmerenSolutions/user-scripts // @version 1.0.0 // @description Replaces UI words with Japanese vocabulary learned in WaniKani // @author Johan Emerén // @copyright 2026, Johan Emerén // @license MIT // @match https://www.wanikani.com/* // @grant GM_addElement // @grant GM_getValue // @grant GM_setValue // @inject-into content // @noframes // @run-at document-idle // @downloadURL https://raw.githubusercontent.com/EmerenSolutions/user-scripts/main/japanese-ui/src/wanikani-progressive-japanese-ui.user.js // @updateURL https://raw.githubusercontent.com/EmerenSolutions/user-scripts/main/japanese-ui/src/wanikani-progressive-japanese-ui.user.js // ==/UserScript== (() => { 'use strict'; const SCRIPT_NAME = 'Wanikani Progressive Japanese UI'; const SCRIPT_VERSION = '1.0.0'; const CACHE_KEY = 'learned-vocabulary-cache-v1'; const CACHE_SCHEMA_VERSION = 1; const MINIMUM_SRS_STAGE = 1; const WKOF_BRIDGE_EVENT_PREFIX = 'wanikani-progressive-japanese-ui:wkof'; const WKOF_BRIDGE_TIMEOUT_MS = 15000; const UI_ANCESTOR_SELECTOR = [ 'a', 'button', 'label', 'summary', 'nav', 'header', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'th', '[role="button"]', '[role="heading"]', '[role="menuitem"]', '[role="tab"]' ].join(','); const UI_DIRECT_SELECTOR = [ '[class*="forecast"]', '[class*="header"]', '[class*="heading"]', '[class*="label"]', '[class*="lesson"]', '[class*="menu"]', '[class*="navigation"]', '[class*="progress"]', '[class*="quiz"]', '[class*="subtitle"]', '[class*="summary"]', '[class*="title"]' ].join(','); const EXCLUDED_SELECTOR = [ 'script', 'style', 'textarea', 'input', 'select', 'option', 'code', 'pre', '[contenteditable]:not([contenteditable="false"])', '[data-japanese-ui-ignore]', '.subject-character', '.subject-page-header__subject', '.subject-section__content', '.subject-section__text', '.subject-subsection__content', '.last-item__meanings', '.last-item__readings', '[class*="context-sentence"]', '[class*="explanation"]', '[class*="mnemonic"]' ].join(','); const FORECAST_SELECTOR = [ 'turbo-frame[src*="/widgets/review-forecast"]', 'turbo-frame[data-dashboard-widget-url-value*="/widgets/review-forecast"]', '[class*="review-forecast"]' ].join(','); const WEEKDAY_LABELS = new Map([ ['mon', 'monday'], ['tue', 'tuesday'], ['tues', 'tuesday'], ['wed', 'wednesday'], ['thu', 'thursday'], ['thur', 'thursday'], ['thurs', 'thursday'], ['fri', 'friday'], ['sat', 'saturday'], ['sun', 'sunday'] ]); const TOKEN_STOP_WORDS = new Set([ 'a', 'an', 'and', 'are', 'as', 'at', 'be', 'been', 'being', 'but', 'by', 'did', 'do', 'does', 'for', 'from', 'he', 'her', 'his', 'in', 'is', 'it', 'its', 'of', 'on', 'or', 'our', 'she', 'that', 'the', 'their', 'these', 'they', 'this', 'those', 'to', 'was', 'we', 'were', 'with', 'you', 'your' ]); const AMBIGUOUS_MEANINGS = new Set([ 'account', 'about', 'back', 'down', 'left', 'master', 'right', 'safe', 'up' ]); // Each rule names the exact WaniKani vocabulary required to reveal it. // Composite labels use the most complete variant the learner has unlocked. const TERM_RULES = Object.freeze([ { source: 'Today', target: '今日', requires: ['今日'] }, { source: "Today's", target: '今日の', requires: ['今日'] }, { source: 'Tomorrow', target: '明日', requires: ['明日'] }, { source: 'Yesterday', target: '昨日', requires: ['昨日'] }, { source: 'Monday', target: '月曜日', requires: ['月曜日'] }, { source: 'Tuesday', target: '火曜日', requires: ['火曜日'] }, { source: 'Wednesday', target: '水曜日', requires: ['水曜日'] }, { source: 'Thursday', target: '木曜日', requires: ['木曜日'] }, { source: 'Friday', target: '金曜日', requires: ['金曜日'] }, { source: 'Saturday', target: '土曜日', requires: ['土曜日'] }, { source: 'Sunday', target: '日曜日', requires: ['日曜日'] }, { source: 'Hour', target: '時間', requires: ['時間'] }, { source: 'Hours', target: '時間', requires: ['時間'] }, { source: 'Study', target: '勉強', requires: ['勉強'] }, { source: 'Lesson', target: '授業', requires: ['授業'] }, { source: 'Lessons', target: '授業', requires: ['授業'] }, { source: "Today's Lessons", variants: [ { target: '今日の授業', requires: ['今日', '授業'] }, { target: '今日の Lessons', requires: ['今日'] }, { target: "Today's 授業", requires: ['授業'] } ] }, { source: 'Lesson Picker', variants: [ { target: '授業選択', requires: ['授業', '選択'] }, { target: '授業 Picker', requires: ['授業'] }, { target: 'Lesson 選択', requires: ['選択'] } ] }, { source: 'Start Lessons', variants: [ { target: '授業開始', requires: ['授業', '開始'] }, { target: 'Start 授業', requires: ['授業'] }, { target: 'Lessons 開始', requires: ['開始'] } ] }, { source: 'Interleave Lessons', variants: [ { target: '授業を混ぜる', requires: ['授業', '混ぜる'] }, { target: 'Interleave 授業', requires: ['授業'] }, { target: 'Lessonsを混ぜる', requires: ['混ぜる'] } ] }, { source: 'Lesson Summary', variants: [ { target: '授業概要', requires: ['授業', '概要'] }, { target: '授業 Summary', requires: ['授業'] }, { target: 'Lesson 概要', requires: ['概要'] } ] }, { source: 'Review', target: '復習', requires: ['復習'] }, { source: 'Reviews', target: '復習', requires: ['復習'] }, { source: 'Review Forecast', variants: [ { target: '復習予報', requires: ['復習', '予報'] }, { target: '復習 Forecast', requires: ['復習'] }, { target: 'Review 予報', requires: ['予報'] } ] }, { source: 'Review Summary', variants: [ { target: '復習概要', requires: ['復習', '概要'] }, { target: '復習 Summary', requires: ['復習'] }, { target: 'Review 概要', requires: ['概要'] } ] }, { source: 'Next Review', variants: [ { target: '次の復習', requires: ['次', '復習'] }, { target: '次の Review', requires: ['次'] }, { target: 'Next 復習', requires: ['復習'] } ] }, { source: 'Next Reviews', variants: [ { target: '次の復習', requires: ['次', '復習'] }, { target: '次の Reviews', requires: ['次'] }, { target: 'Next 復習', requires: ['復習'] } ] }, { source: 'Upcoming Reviews', variants: [ { target: '今後の復習', requires: ['今後', '復習'] }, { target: '今後の Reviews', requires: ['今後'] }, { target: 'Upcoming 復習', requires: ['復習'] } ] }, { source: 'Radical', target: '部首', requires: ['部首'] }, { source: 'Radicals', target: '部首', requires: ['部首'] }, { source: 'Kanji', target: '漢字', requires: ['漢字'] }, { source: 'Vocabulary', target: '単語', requires: ['単語'] }, { source: 'Kanji Composition', variants: [ { target: '漢字の構成', requires: ['漢字', '構成'] }, { target: '漢字 Composition', requires: ['漢字'] }, { target: 'Kanjiの構成', requires: ['構成'] } ] }, { source: 'Meaning', target: '意味', requires: ['意味'] }, { source: 'Meanings', target: '意味', requires: ['意味'] }, { source: 'Reading', target: '読み方', requires: ['読み方'] }, { source: 'Readings', target: '読み方', requires: ['読み方'] }, { source: 'Pronunciation', target: '発音', requires: ['発音'] }, { source: 'Context', target: '文脈', requires: ['文脈'] }, { source: 'Name', target: '名前', requires: ['名前'] }, { source: 'Names', target: '名前', requires: ['名前'] }, { source: 'Example', target: '例', requires: ['例'] }, { source: 'Examples', target: '例', requires: ['例'] }, { source: 'Answer', target: '答え', requires: ['答え'] }, { source: 'Question', target: '質問', requires: ['質問'] }, { source: 'Correct', target: '正解', requires: ['正解'] }, { source: 'Incorrect', target: '不正解', requires: ['不正解'] }, { source: 'Answered Correctly', target: '正解', requires: ['正解'] }, { source: 'Answered Incorrectly', target: '不正解', requires: ['不正解'] }, { source: 'Progress', target: '進行', requires: ['進行'] }, { source: 'Summary', target: '概要', requires: ['概要'] }, { source: 'Details', target: '詳細', requires: ['詳細'] }, { source: 'Settings', target: '設定', requires: ['設定'] }, { source: 'Item', target: '項目', requires: ['項目'] }, { source: 'Items', target: '項目', requires: ['項目'] }, { source: 'All', target: '全て', requires: ['全て'] }, { source: 'Select All', variants: [ { target: '全て選択', requires: ['全て', '選択'] }, { target: 'Select 全て', requires: ['全て'] }, { target: 'All 選択', requires: ['選択'] } ] }, { source: 'Recent', target: '最近', requires: ['最近'] }, { source: 'Recent Mistakes', variants: [ { target: '最近の間違い', requires: ['最近', '間違い'] }, { target: '最近の Mistakes', requires: ['最近'] }, { target: 'Recent 間違い', requires: ['間違い'] } ] }, { source: 'Recent Unlocks', variants: [ { target: '最近の解放', requires: ['最近', '解放'] }, { target: '最近の Unlocks', requires: ['最近'] }, { target: 'Recent 解放', requires: ['解放'] } ] }, { source: 'Recently Learned', variants: [ { target: '最近習った', requires: ['最近', '習う'] }, { target: '最近 Learned', requires: ['最近'] }, { target: 'Recently 習った', requires: ['習う'] } ] }, { source: 'Extra Study', variants: [ { target: '追加の勉強', requires: ['追加', '勉強'] }, { target: '追加の Study', requires: ['追加'] }, { target: 'Extra 勉強', requires: ['勉強'] } ] }, { source: 'Critical Items', variants: [ { target: '危険な項目', requires: ['危険', '項目'] }, { target: '危険な Items', requires: ['危険'] }, { target: 'Critical 項目', requires: ['項目'] } ] }, { source: 'Radical Name', variants: [ { target: '部首の名前', requires: ['部首', '名前'] }, { target: '部首 Name', requires: ['部首'] }, { target: 'Radicalの名前', requires: ['名前'] } ] }, { source: 'Radical Meaning', variants: [ { target: '部首の意味', requires: ['部首', '意味'] }, { target: '部首 Meaning', requires: ['部首'] }, { target: 'Radicalの意味', requires: ['意味'] } ] }, { source: 'Kanji Meaning', variants: [ { target: '漢字の意味', requires: ['漢字', '意味'] }, { target: '漢字 Meaning', requires: ['漢字'] }, { target: 'Kanjiの意味', requires: ['意味'] } ] }, { source: 'Kanji Reading', variants: [ { target: '漢字の読み方', requires: ['漢字', '読み方'] }, { target: '漢字 Reading', requires: ['漢字'] }, { target: 'Kanjiの読み方', requires: ['読み方'] } ] }, { source: 'Vocabulary Meaning', variants: [ { target: '単語の意味', requires: ['単語', '意味'] }, { target: '単語 Meaning', requires: ['単語'] }, { target: 'Vocabularyの意味', requires: ['意味'] } ] }, { source: 'Vocabulary Reading', variants: [ { target: '単語の読み方', requires: ['単語', '読み方'] }, { target: '単語 Reading', requires: ['単語'] }, { target: 'Vocabularyの読み方', requires: ['読み方'] } ] } ]); let translations = new Map(); let observer = null; const translatedNodes = new Map(); const runtimeStatus = { version: SCRIPT_VERSION, source: null, lastSyncedAt: null, learnedItems: 0, learnedMeanings: 0, translatedNodes: 0 }; const normalizeSource = value => String(value).toLocaleLowerCase('en-US'); const isLearnedVocabularyItem = item => ( (item?.object === 'vocabulary' || item?.object === 'kana_vocabulary') && item?.data?.hidden_at == null && item?.assignments?.started_at != null && Number(item.assignments.srs_stage) >= MINIMUM_SRS_STAGE ); const collectLearnedItems = items => items.filter(isLearnedVocabularyItem); const collectLearnedVocabulary = items => new Set( collectLearnedItems(items) .map(item => item.data.characters || item.data.slug) .filter(Boolean) ); // A learned meaning can map to more than one vocabulary item. Prefer primary // accepted meanings, then make equal-score selection deterministic. Common // and ambiguous English words are excluded rather than translated poorly. const buildLearnedMeaningTranslations = learnedItems => { const candidates = new Map(); for (const item of learnedItems) { const target = item.data.characters || item.data.slug; if (!target) continue; for (const meaning of item.data.meanings || []) { const source = normalizeSource(meaning.meaning).trim(); if ( !/^[a-z][a-z '\-]*$/u.test(source) || TOKEN_STOP_WORDS.has(source) || AMBIGUOUS_MEANINGS.has(source) ) continue; const candidate = { target, score: (meaning.primary ? 100 : 0) + (meaning.accepted_answer === false ? 0 : 10) - [...target].length }; const current = candidates.get(source); if ( !current || candidate.score > current.score || ( candidate.score === current.score && candidate.target.localeCompare(current.target, 'ja') < 0 ) ) candidates.set(source, candidate); } } return new Map( [...candidates].map(([source, candidate]) => [source, candidate.target]) ); }; const applyTermRules = (activeTranslations, learnedVocabulary) => { // Apply safety exclusions to older cached meaning maps as well. for (const source of AMBIGUOUS_MEANINGS) activeTranslations.delete(source); for (const rule of TERM_RULES) { const variants = rule.variants || [rule]; const activeVariant = variants.find(variant => ( variant.requires.every(term => learnedVocabulary.has(term)) )); // Curated UI senses must not fall back to an unrelated learned meaning // (for example Study -> research or Reading -> reading a book). const source = normalizeSource(rule.source); activeTranslations.delete(source); if (activeVariant) activeTranslations.set(source, activeVariant.target); } return activeTranslations; }; // Retained as a focused composition boundary and instrumented by the unit // tests, even though production cache activation uses the function below. // deno-lint-ignore no-unused-vars const buildTranslations = (learnedVocabulary, learnedItems = []) => ( applyTermRules( buildLearnedMeaningTranslations(learnedItems), learnedVocabulary ) ); const createLearnedCache = (learnedItems, savedAt = new Date().toISOString()) => { const learnedVocabulary = collectLearnedVocabulary(learnedItems); const learnedMeanings = buildLearnedMeaningTranslations(learnedItems); return { schemaVersion: CACHE_SCHEMA_VERSION, savedAt, learnedItems: learnedItems.length, learnedVocabulary: [...learnedVocabulary], learnedMeanings: [...learnedMeanings] }; }; const isValidLearnedCache = cache => ( cache?.schemaVersion === CACHE_SCHEMA_VERSION && typeof cache.savedAt === 'string' && Number.isInteger(cache.learnedItems) && Array.isArray(cache.learnedVocabulary) && Array.isArray(cache.learnedMeanings) ); const buildTranslationsFromCache = cache => applyTermRules( new Map(cache.learnedMeanings), new Set(cache.learnedVocabulary) ); // Match the whole site so new browsing pages and Turbo visits work without // adding URLs. Active study sessions remain excluded to protect quiz content. const isAllowedPage = location => ( location.origin === 'https://www.wanikani.com' && !/^\/(?:review|reviews|lesson|lessons)(?:\/|$)/u.test(location.pathname) && !/^\/subjects\/(?:review|extra_study|extra-study)(?:\/|$)/u.test(location.pathname) && !( /^\/subject-lessons(?:\/|$)/u.test(location.pathname) && !/^\/subject-lessons\/picker\/?$/u.test(location.pathname) ) ); // A URL fallback also protects subject links supplied by other scripts or // future card markup, while category navigation (/kanji, etc.) stays eligible. const isSubjectLink = href => { if (typeof href !== 'string' || !href) return false; try { const url = new URL(href, 'https://www.wanikani.com/'); return url.origin === 'https://www.wanikani.com' && /^\/(?:kanji|radicals|vocabulary|kana-vocabulary)\/[^/]+\/?$/u.test(url.pathname); } catch { return false; } }; const isWaniKaniHost = hostname => ( hostname === 'www.wanikani.com' || hostname === 'preview.wanikani.com' ); const serializeWkofItems = items => (Array.isArray(items) ? items : []).map(item => ({ object: item?.object, data: { characters: item?.data?.characters, slug: item?.data?.slug, hidden_at: item?.data?.hidden_at, meanings: (item?.data?.meanings || []).map(meaning => ({ meaning: meaning?.meaning, primary: Boolean(meaning?.primary), accepted_answer: meaning?.accepted_answer })) }, assignments: item?.assignments ? { srs_stage: item.assignments.srs_stage, started_at: item.assignments.started_at } : null })); const runWkofBridge = async (responseEventName, serializeItems) => { const respond = payload => document.documentElement.dispatchEvent( new CustomEvent(responseEventName, { detail: JSON.stringify(payload) }) ); try { if (!window.wkof?.include || !window.wkof?.ready) { throw new Error('Wanikani Open Framework is not available.'); } window.wkof.include('ItemData'); await window.wkof.ready('ItemData'); const items = await window.wkof.ItemData.get_items({ wk_items: { options: { assignments: true }, filters: { item_type: 'vocabulary,kana_vocabulary' } } }); respond({ ok: true, items: serializeItems(items) }); } catch (error) { respond({ ok: false, error: error && typeof error.message === 'string' ? error.message : String(error) }); } }; const createWkofBridgeSource = responseEventName => ( `void (${runWkofBridge.toString()})(` + `${JSON.stringify(responseEventName)}, (${serializeWkofItems.toString()}));` ); const parseWkofBridgeDetail = detail => { if (typeof detail !== 'string') { throw new Error('WKOF bridge returned an invalid response.'); } const payload = JSON.parse(detail); if (payload?.ok !== true) { throw new Error(payload?.error || 'WKOF bridge failed.'); } if (!Array.isArray(payload.items)) { throw new Error('WKOF bridge returned invalid item data.'); } return payload.items; }; const createBridgeEventName = () => { const identifier = globalThis.crypto?.randomUUID?.() || `${Date.now()}-${Math.random().toString(36).slice(2)}`; return `${WKOF_BRIDGE_EVENT_PREFIX}:${identifier}`; }; const singularize = word => { if (word.endsWith('ies') && word.length > 4) return `${word.slice(0, -3)}y`; if (/(?:ches|shes|sses|xes|zes)$/u.test(word)) return word.slice(0, -2); if (word.endsWith('s') && !word.endsWith('ss')) return word.slice(0, -1); return word; }; const translateEnglishWords = (value, activeTranslations) => value.replace( /[A-Za-z]+(?:['’][A-Za-z]+)?/gu, word => { const normalized = normalizeSource(word); if (TOKEN_STOP_WORDS.has(normalized)) return word; return activeTranslations.get(normalized) || activeTranslations.get(singularize(normalized)) || word; } ); const translateCoreText = (core, activeTranslations = translations, context = {}) => { // Forecast dates use English weekday abbreviations, not ordinary meanings. // Protect the label even when its weekday vocabulary has not been learned. const weekdayMatch = context.forecast ? core.match(/^(Mon|Tues?|Wed|Thurs?|Thu|Fri|Sat|Sun)\.?(?=$|[\s,])/iu) : null; if (weekdayMatch) { const weekday = WEEKDAY_LABELS.get(normalizeSource(weekdayMatch[1])); const target = activeTranslations.get(weekday); const suffix = core.slice(weekdayMatch[0].length); return (target || weekdayMatch[0]) + translateEnglishWords(suffix, activeTranslations); } const direct = activeTranslations.get(normalizeSource(core)); if (direct) return direct; const countMatch = core.match(/^([\d,]+)\s+(.+)$/u); if (countMatch) { const countedTerm = activeTranslations.get(normalizeSource(countMatch[2])); if (countedTerm) return `${countMatch[1]} ${countedTerm}`; } return translateEnglishWords(core, activeTranslations); }; const translateText = (value, activeTranslations = translations, context = {}) => { const raw = String(value ?? ''); const match = raw.match(/^(\s*)(.*?)(\s*)$/su); if (!match) return raw; const translated = translateCoreText(match[2], activeTranslations, context); return translated === match[2] ? raw : `${match[1]}${translated}${match[3]}`; }; const isUiTextNode = node => { if (!node || node.nodeType !== 3 || !node.parentElement) return false; if (node.parentElement.closest(EXCLUDED_SELECTOR)) return false; const link = node.parentElement.closest('a[href]'); if (isSubjectLink(link?.getAttribute?.('href'))) return false; return Boolean( node.parentElement.closest(UI_ANCESTOR_SELECTOR) || node.parentElement.matches(UI_DIRECT_SELECTOR) ); }; const updateStatus = () => { runtimeStatus.translatedNodes = translatedNodes.size; }; // Keep the original text for Turbo page caching and for host pages that // rewrite an already translated node after a dynamic update. const processTextNode = node => { const previous = translatedNodes.get(node); if (previous) { if (node.nodeValue === previous.translated) { if (isUiTextNode(node)) return; // A host can move translated UI text into a protected subject card. node.nodeValue = previous.original; } translatedNodes.delete(node); } if (!isUiTextNode(node)) return; const original = node.nodeValue; const translated = translateText(original, translations, { forecast: Boolean(node.parentElement.closest(FORECAST_SELECTOR)) }); if (translated === original) return; translatedNodes.set(node, { original, translated }); node.nodeValue = translated; }; const processSubtree = root => { if (!root) return; if (root.nodeType === 3) { processTextNode(root); return; } if (root.nodeType !== 1 && root.nodeType !== 9 && root.nodeType !== 11) return; const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); let node = walker.nextNode(); while (node) { processTextNode(node); node = walker.nextNode(); } updateStatus(); }; // Dynamic sites frequently replace whole UI subtrees. Forget translated // nodes only after they are disconnected so same-document moves retain the // original text needed by restoreTranslatedNodes. const forgetDetachedTextNodes = (root, records = translatedNodes) => { if (!root) return; if (root.nodeType === 3) { if (!root.isConnected) records.delete(root); return; } if (root.nodeType !== 1 && root.nodeType !== 9 && root.nodeType !== 11) return; const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); let node = walker.nextNode(); while (node) { if (!node.isConnected) records.delete(node); node = walker.nextNode(); } }; const stopObserving = () => { observer?.disconnect(); observer = null; }; const restoreTranslatedNodes = () => { stopObserving(); for (const [node, record] of translatedNodes) { if (node.nodeValue === record.translated) node.nodeValue = record.original; } translatedNodes.clear(); updateStatus(); }; const startObserving = () => { if (!isAllowedPage(window.location)) { restoreTranslatedNodes(); return; } stopObserving(); processSubtree(document.body || document.documentElement); observer = new MutationObserver(mutations => { if (!isAllowedPage(window.location)) { restoreTranslatedNodes(); return; } const removedRoots = []; for (const mutation of mutations) { if (mutation.type === 'attributes') { processSubtree(mutation.target); continue; } if (mutation.type === 'characterData') { processTextNode(mutation.target); continue; } removedRoots.push(...mutation.removedNodes); for (const node of mutation.addedNodes) processSubtree(node); } for (const root of removedRoots) forgetDetachedTextNodes(root); updateStatus(); }); observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'href', 'contenteditable', 'data-japanese-ui-ignore'], characterData: true, childList: true, subtree: true }); }; const loadLearnedItems = () => new Promise((resolve, reject) => { const responseEventName = createBridgeEventName(); const timeout = setTimeout(() => { document.documentElement.removeEventListener(responseEventName, handleResponse); reject(new Error('Timed out while waiting for Wanikani Open Framework.')); }, WKOF_BRIDGE_TIMEOUT_MS); const cleanup = () => { clearTimeout(timeout); document.documentElement.removeEventListener(responseEventName, handleResponse); }; const handleResponse = event => { if (event.target !== document.documentElement) return; try { const items = parseWkofBridgeDetail(event.detail); cleanup(); resolve(collectLearnedItems(items)); } catch (error) { cleanup(); reject(error); } }; document.documentElement.addEventListener(responseEventName, handleResponse); try { const bridgeElement = GM_addElement('script', { textContent: createWkofBridgeSource(responseEventName) }); bridgeElement.remove(); } catch (error) { cleanup(); reject(error); } }); const activateTranslations = (cache, source = 'wkof') => { restoreTranslatedNodes(); translations = buildTranslationsFromCache(cache); runtimeStatus.source = source; runtimeStatus.lastSyncedAt = cache.savedAt; runtimeStatus.learnedItems = cache.learnedItems; runtimeStatus.learnedMeanings = cache.learnedMeanings.length; startObserving(); console.info( `[${SCRIPT_NAME}] Translated ${translatedNodes.size} UI text nodes ` + `using ${runtimeStatus.learnedMeanings} learned meanings from ` + `${runtimeStatus.learnedItems} vocabulary items (${runtimeStatus.source}).` ); }; // Refresh the learned vocabulary through the narrow WaniKani-only bridge. const initializeFromWaniKani = async () => { const learnedItems = await loadLearnedItems(); const cache = createLearnedCache(learnedItems); GM_setValue(CACHE_KEY, cache); activateTranslations(cache); }; const initialize = async () => { if (window.location.origin !== 'https://www.wanikani.com') return; // Register before fetching: navigation can finish while WKOF is pending. // Even a document opened in a quiz must resume on a later browsing visit. document.addEventListener('turbo:before-cache', restoreTranslatedNodes); document.addEventListener('turbo:load', startObserving); try { const cache = GM_getValue(CACHE_KEY, null); if (isValidLearnedCache(cache)) activateTranslations(cache, 'cache'); await initializeFromWaniKani(); } catch (error) { console.error(`[${SCRIPT_NAME}] Failed to refresh learned vocabulary.`, error); } }; void initialize(); })();