Lang attribute setter (prioritize Kanji font on Japanese Websites)

Fixes Han unification, for example, for Japanese (can be edited to prioritize other langs)

目前為 2022-10-05 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Lang attribute setter (prioritize Kanji font on Japanese Websites)
// @namespace    https://github.com/patarapolw/wanikani-userscript
// @version      0.2.0
// @description  Fixes Han unification, for example, for Japanese (can be edited to prioritize other langs)
// @author       polv
// @license      MIT
// @match        *://community.wanikani.com/*
// @match        *://*.kanjipedia.jp/*
// @match        *://moji.tekkai.com/*
// @match        *://*.immersionkit.com/*
// @match        *://youglish.com/*
// @match        *://docs.google.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=eastasiastudent.net
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  const LANG = 'ja-JP';
  const LOCALSTORAGE_KEY = '.user.js--lang-attr';

  checkInject();

  Object.assign(window, {
    langAttrSetter: {
      inject: doInject,
      setPath: setInject,
    },
  });

  function doInject(lang = LANG) {
    document.documentElement.lang = lang;
  }

  function checkInject() {
    if (typeof location === 'undefined') {
      return doInject();
    }
    const domain = '.' + location.origin.split('://')[1];
    if (domain.endsWith('.youglish.com')) {
      // Example URL: https://youglish.com/pronounce/%E5%AE%B6%E6%97%8F/japanese
      const lang = location.pathname.split('/').slice(2);
      if (!lang.length) return;
      switch (lang[0]) {
        case 'japanese': {
          return doInject('ja-JP');
        }
        case 'korean': {
          return doInject('ko-KR');
        }
        case 'chinese': {
          return doInject(`zh-${(lang[1] || 'CN').toLocaleUpperCase()}`);
        }
      }
      return;
    }

    const paths = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || '{}');
    if (paths[location.pathname]) {
      return doInject(paths[location.pathname]);
    }

    if (domain.endsWith('.docs.google.com')) return;
    return doInject();
  }

  function setInject(lang = LANG) {
    if (location.origin.endsWith('docs.google.com')) {
      const paths = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || '{}');
      if (paths[location.pathname]) {
        console.log('original value:', paths[location.pathname]);
      }
      paths[location.pathname] = lang;
      localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(paths));
      return doInject(lang);
    }
    return false;
  }
})();