HoholDetector

Helps you detect Ukrainian text

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);