HoholDetector

Helps you detect Ukrainian text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            HoholDetector
// @name:ru         Обнаружитель мовы
// @namespace       Violentmonkey Scripts
// @match           *://*/*
// @grant           none
// @version         1.0
// @author          -
// @description     Helps you detect Ukrainian text
// @description:ru  Помогает в обнаружении украинского языка в тексте
// @license MIT
// ==/UserScript==

const ukrainianSymbols = /[ґєії]/i;

const replaceMovaString = (s) => {
  if (s.includes("🐷") || !ukrainianSymbols.test(s)) {
    return s;
  }

  return `🐷${s}🐷`;
};

const replaceMovaHTML = (element) => {
  element.childNodes.forEach((node) => replaceMovaHTML(node));
  if (element.nodeType == Node.TEXT_NODE) {
    const newValue = replaceMovaString(element.nodeValue);
    if (element.nodeValue !== newValue) {
      element.nodeValue = newValue;
    }
  }
};

const observer = new MutationObserver(
  (mutationList, observer) => {
    for (const mutation of mutationList) {
      switch (mutation.type) {
        case 'childList':
          mutation.addedNodes.forEach(replaceMovaHTML);
          break;
        case 'attributes':
          break;
        case 'characterData':
          replaceMovaHTML(mutation.target);
          break;
        case 'subtree':
          break;
      }
    }
  }
);

observer.observe(document, {
  childList: true,
  // attributes: true,
  characterData: true,
  subtree: true
});

const onLoad = () => {
  replaceMovaHTML(document.getElementsByTagName('html')[0]);
};

window.addEventListener('load', onLoad, false);