Add lang hyphen, if not exists for HTML tags

Arch Linux Firefox doesn't set lang="ja" as Japanese

  1. // ==UserScript==
  2. // @name Add lang hyphen, if not exists for HTML tags
  3. // @namespace https://github.com/patarapolw/wanikani-userscript
  4. // @version 0.1.4
  5. // @description Arch Linux Firefox doesn't set lang="ja" as Japanese
  6. // @author polv
  7. // @license MIT
  8. // @match *://*/*
  9. // @exclude https://www.wanikani.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=eastasiastudent.net
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (() => {
  15. // src/fix-lang-hyphen.ts
  16. var FIX_LANG_HYPHEN = {
  17. ja: "",
  18. ko: "",
  19. zh: "",
  20. vi: ""
  21. };
  22. getLang(document.documentElement);
  23. fixLangObserver();
  24. function fixLangObserver(el = document.body) {
  25. new MutationObserver((muts) => {
  26. for (const mut of muts) {
  27. mut.addedNodes.forEach((n) => {
  28. if (n instanceof HTMLElement) {
  29. findAndFixLang(n);
  30. }
  31. });
  32. }
  33. }).observe(el, {
  34. childList: true,
  35. subtree: true
  36. });
  37. findAndFixLang(el);
  38. }
  39. function findAndFixLang(el) {
  40. el.querySelectorAll("*").forEach((it) => getLang(it));
  41. }
  42. function getLang(it, fix = true) {
  43. let aLang = it.getAttribute("lang");
  44. if (aLang && typeof FIX_LANG_HYPHEN[aLang] === "string") {
  45. aLang += "-" + FIX_LANG_HYPHEN[aLang];
  46. if (fix) {
  47. it.setAttribute("lang", aLang);
  48. }
  49. return aLang;
  50. }
  51. return null;
  52. }
  53. })();