User:Eejit43/scripts/highlight-homographs.js

This is an old revision of this page, as edited by Eejit43 (talk | contribs) at 01:34, 30 June 2023 (Syncing script from GitHub (via script)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
// Note: This script was compiled from TypeScript. For a more readable version, see https://github.com/Eejit43/wikipedia-scripts/blob/main/scripts/highlight-homographs.ts

"use strict";
(() => {
  const title = document.getElementById("firstHeading");
  if (!title)
    return mw.notify("Could not find title element!", { type: "error" });
  [...title.children].forEach((element) => {
    if (!element.textContent)
      return;
    if (element.nodeType === Node.TEXT_NODE)
      title.replaceChild(document.createRange().createContextualFragment(markHomographs(element.textContent)), element);
    else if (element.classList.contains("mw-page-title-main") || element.tagName === "I")
      element.innerHTML = markHomographs(element.innerHTML);
  });
  function markHomographs(string) {
    return string.split("").map((char) => {
      if (
        /* Cyrillics */
        /[\u0400-\u04FF\u0500-\u052F\u2DE0-\u2DFF\uA640-\uA69F\u1D2B\u1D78]/.test(char) || // eslint-disable-line no-misleading-character-class
        /* Greek */
        /[ονɑΑΒΕΗΙΚΜΝΟΡΤΧΥΖ]/.test(char) || /* Armenian */
        /[օոսՏԼ]/.test(char) || /* Roman Numerals */
        /[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯ]/i.test(char)
      )
        return `<abbr title="This character is a homograph!" style="text-decoration: none; background-color: #ff5555">${char}</abbr>`;
      else
        return char;
    }).join("");
  }
})();

// </nowiki>