jpdb.io with KanjiVG

Use KanjiVG images instead of jpdb.io ones

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         jpdb.io with KanjiVG
// @description  Use KanjiVG images instead of jpdb.io ones
// @match        https://jpdb.io/*
// @license      MIT
// @grant        GM_xmlhttpRequest
// @version      0.0.6
// @namespace https://greasyfork.org/users/1309172
// ==/UserScript==

const log = console.log

function loadSvg() {
    if (!KanjiChar) {
        return;
    }
    if (NewSvg) {
        return;
    }

    const kanjiUnicodeHex = KanjiChar.charCodeAt(0).toString(16).toLowerCase().padStart(5, '0');
    const url = `https://raw.githubusercontent.com/KanjiVG/kanjivg/master/kanji/${kanjiUnicodeHex}.svg`;

    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function (response) {
            if (response.status != 200) {
                console.log(`KanjiVG: ${xhr.status}: ${xhr.statusText}`);
                return;
            }

            const svg = response.responseXML.getElementsByTagName("svg")[0];
            svg.style.width = "300px";
            svg.style.height = "300px";

            const strokeNumbers = svg.getElementById(`kvg:StrokeNumbers_${kanjiUnicodeHex}`);
            strokeNumbers.style.fontSize = "6px";

            // if dark theme
            if (document.firstElementChild.classList.contains("dark-mode")) {
                svg.getElementById(`kvg:StrokePaths_${kanjiUnicodeHex}`).style.stroke = "#aaaaaa";
                strokeNumbers.style.fill = "#666666";
            }

            log("Loaded")
            NewSvg = svg;
            Replaced = replaceSvg();
        }
    });
}

function replaceSvg() {
    if (Replaced) {
        return true;
    }

    const kanjiElement = document.querySelector('a.kanji.plain');
    if (!kanjiElement) {
        return false;
    }

    if (!NewSvg) {
        return false;
    }

    kanjiElement.firstChild.replaceWith(NewSvg);
    log("Replaced")
    return true;
}

function getKanjiFromPage() {
    const uri = location.href.split("/")[3]

    if (uri === "review#a") {
        const cardType = document.querySelector("body > div.container.bugfix > div > div.review-hidden > div > div.kind");
        if (cardType && (cardType.textContent !== "Kanji" && cardType.textContent !== "Component")) {
            return null;
        }

        const linkWithKanji = document.querySelector("head > link[rel='prefetch']");
        if (!linkWithKanji) {
            return null;
        }
        return linkWithKanji.getAttribute("href")[15];
    }

    const kanjiElement = document.querySelector('a.kanji.plain');
    if (!kanjiElement) {
        return null;
    }
    return kanjiElement.getAttribute("href").split("/")[2][0];
}

const KanjiChar = getKanjiFromPage();
let NewSvg = null;
let Replaced = false;
loadSvg();

let observer = new MutationObserver(() => {
    if (Replaced) {
      observer.disconnect();
      return;
    }
    loadSvg();
    Replaced = replaceSvg();
});

observer.observe(document.body, {
     childList: true,
     subtree: true,
});