Wanikani Context Move-Up

Move up Context Sentences and Patterns of Use in Review

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Wanikani Context Move-Up
// @namespace     http://www.wanikani.com
// @description   Move up Context Sentences and Patterns of Use in Review
// @icon          https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
// @match         https://www.wanikani.com/extra_study/session*
// @match         https://www.wanikani.com/review/session*
// @match         https://www.wanikani.com/subjects/review*
// @match         https://www.wanikani.com/subjects/extra_study*
// @match         https://preview.wanikani.com/extra_study/session*
// @match         https://preview.wanikani.com/review/session*
// @match         https://preview.wanikani.com/subjects/review*
// @match         https://preview.wanikani.com/subjects/extra_study*
// @version       0.1.0
// @author        polv
// @license       MIT; http://opensource.org/licenses/MIT
// @grant         none
// ==/UserScript==

(function () {
  const elInfo = document.querySelector('turbo-frame#subject-info');
  if (elInfo) {
    const obs = new MutationObserver(() => {
      const container = Array.from(elInfo.childNodes).find(
        (el) => el instanceof HTMLElement && el.classList.contains('container'),
      );
      if (!(container instanceof HTMLElement)) return;

      const contextHref = container.querySelector('section > a#context');
      if (!contextHref) return;

      const contextSection = contextHref.parentElement;
      if (!contextSection) return;

      const meaningSection = container.querySelector(
        'section > a#meaning',
      )?.parentElement;
      if (!meaningSection) return;

      meaningSection.insertAdjacentElement(
        'beforebegin',
        contextSection.cloneNode(true),
      );

      contextSection.remove();
    });
    obs.observe(elInfo, {
      attributes: true,
      attributeFilter: ['src', 'complete'],
    });
  }
})();