Lang attribute setter (prioritize Kanji font on Japanese Websites)

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

  1. // ==UserScript==
  2. // @name Lang attribute setter (prioritize Kanji font on Japanese Websites)
  3. // @namespace https://github.com/patarapolw/wanikani-userscript
  4. // @version 0.2.0
  5. // @description Fixes Han unification, for example, for Japanese (can be edited to prioritize other langs)
  6. // @author polv
  7. // @license MIT
  8. // @match *://community.wanikani.com/*
  9. // @match *://*.kanjipedia.jp/*
  10. // @match *://moji.tekkai.com/*
  11. // @match *://*.immersionkit.com/*
  12. // @match *://youglish.com/*
  13. // @match *://docs.google.com/*
  14. // @icon https://www.google.com/s2/favicons?sz=64&domain=eastasiastudent.net
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20. const LANG = 'ja-JP';
  21. const LOCALSTORAGE_KEY = '.user.js--lang-attr';
  22. const originalLang = document.documentElement.lang;
  23.  
  24. checkInject();
  25.  
  26. Object.assign(window, {
  27. langAttrSetter: {
  28. inject: doInject,
  29. setPath: setInject,
  30. unsetPath: unsetInject,
  31. },
  32. });
  33.  
  34. function doInject(lang = LANG) {
  35. return (document.documentElement.lang = lang);
  36. }
  37.  
  38. function checkInject() {
  39. if (typeof location === 'undefined') {
  40. return doInject();
  41. }
  42. const domain = '.' + location.origin.split('://')[1];
  43. if (domain.endsWith('.youglish.com')) {
  44. // Example URL: https://youglish.com/pronounce/%E5%AE%B6%E6%97%8F/japanese
  45. const lang = location.pathname.split('/').slice(2);
  46. if (!lang.length) return;
  47. switch (lang[0]) {
  48. case 'japanese': {
  49. return doInject('ja-JP');
  50. }
  51. case 'korean': {
  52. return doInject('ko-KR');
  53. }
  54. case 'chinese': {
  55. return doInject(`zh-${(lang[1] || 'CN').toLocaleUpperCase()}`);
  56. }
  57. }
  58. return;
  59. }
  60.  
  61. const paths = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || '{}');
  62. if (paths[location.pathname]) {
  63. return doInject(paths[location.pathname]);
  64. }
  65.  
  66. if (domain.endsWith('.docs.google.com')) return;
  67. return doInject();
  68. }
  69.  
  70. function setInject(lang = LANG) {
  71. const paths = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || '{}');
  72. if (paths[location.pathname]) {
  73. console.log('original value:', paths[location.pathname]);
  74. }
  75. paths[location.pathname] = lang;
  76. localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(paths));
  77. return doInject(lang);
  78. }
  79.  
  80. function unsetInject() {
  81. const paths = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || '{}');
  82. if (paths[location.pathname]) {
  83. console.log('original value:', paths[location.pathname]);
  84. }
  85. delete paths[location.pathname];
  86. localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(paths));
  87. return doInject(originalLang);
  88. }
  89. })();