JPDB Add WaniKani Info To Review

6/12/2024, 12:17:38 PM

目前为 2024-06-15 提交的版本。查看 最新版本

// ==UserScript==
// @name        JPDB Add WaniKani Info To Review
// @namespace   JPDB_Add_WaniKani_Info_To_Review
// @match       https://jpdb.io/review*
// @grant       none
// @version     0.01
// @author      Flipp Fuzz
// @description 6/12/2024, 12:17:38 PM
// @run-at      document-idle
// @license MIT
// ==/UserScript==

(function() {
  'use strict';
  // Handling of answer is copied from https://gf.qytechs.cn/en/scripts/459772-jpdb-auto-reveal-answer-sentence/code
  // -こう- implemented the switch from question to answer without really navigating
  // by simply replacing the content and location.href on clicking the "show answer" button
  window.onload = observeUrlChange(addWkLinkInReview);

  // this handles refreshing the page while on the answer screen
  addWkLinkInReview();
})();

function observeUrlChange(onChange) {
  let oldHref = document.location.href;
  const body = document.querySelector("body");
  const observer = new MutationObserver(mutations => {
    mutations.forEach(() => {
      if (oldHref !== document.location.href) {
        oldHref = document.location.href;
        onChange();
      }
    });
  });
  observer.observe(body, { childList: true, subtree: true });
}

function addWkLinkInReview() {
  if(new URL(location.href).searchParams.get('c')) {
    // Figure out where to insert our new div
    const insertAfterElement = document.querySelector('body > div.container.bugfix > div > div.review-reveal > div.result.vocabulary > div > div.subsection-meanings');

    // Find out what is our vocab word
    const wordA = document.querySelector('body > div.container.bugfix > div > div.review-reveal > div.answer-box > div.plain > a');
    let word = wordA.getAttribute('href').split(/[?#]/)[0].split('/').pop();

    // Find out all the Kanjis
    const KanjisA = document.querySelectorAll('body > div.container.bugfix > div > div.review-reveal > div.result.vocabulary > div > div.subsection-composed-of-kanji > div > div > div.spelling > a');
    let kanjis = [];
    KanjisA.forEach((kanjiA) => {
      let localKanji = kanjiA.getAttribute('href').split(/[?#]/)[0].split('/').pop();
      kanjis.push(localKanji);
    });

    // Terminate if we can't find the word or location to insert
    if(!insertAfterElement || !wordA || !word) {
      console.log(`insertAfterElement: ${insertAfterElement}`);
      console.log(`wordA: ${wordA}`);
      console.log(`word: ${word}`);
      console.log(`KanjiA: ${KanjiA}`);
      return;
    }

    let subsectionWkLinkDiv = document.createElement('div');
    subsectionWkLinkDiv.setAttribute('class', 'subsection-wk-link');

    let subsectionWkLinkLabel = document.createElement('h6');
    subsectionWkLinkLabel.setAttribute('class', 'subsection-label');
    subsectionWkLinkLabel.textContent = 'WaniKani';
    subsectionWkLinkDiv.appendChild(subsectionWkLinkLabel);

    let subsectionWkLinkSubsectionDiv = document.createElement('div');
    subsectionWkLinkSubsectionDiv.setAttribute('class', 'subsection');
    subsectionWkLinkDiv.appendChild(subsectionWkLinkSubsectionDiv);

    let wkVocablinka = document.createElement('a');
    wkVocablinka.textContent = word;
    wkVocablinka.setAttribute('href', `https://www.wanikani.com/vocabulary/${word}`);
    wkVocablinka.setAttribute('target', '_blank');
    let tmpP = document.createElement('p');
    tmpP.appendChild(wkVocablinka);
    subsectionWkLinkSubsectionDiv.appendChild(tmpP);

    kanjis.forEach((kanji) => {
      let wkLocalKanjilinka = document.createElement('a');
      wkLocalKanjilinka.textContent = kanji;
      wkLocalKanjilinka.setAttribute('href', `https://www.wanikani.com/kanji/${kanji}`);
      wkLocalKanjilinka.setAttribute('target', '_blank');
      let tmpP = document.createElement('p');
      tmpP.appendChild(wkLocalKanjilinka);
      subsectionWkLinkSubsectionDiv.appendChild(tmpP);
    });

    insertAfterElement.insertAdjacentElement('afterend', subsectionWkLinkDiv);
  }
}

QingJ © 2025

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