User:Eejit43/scripts/highlight-homographs.js: Difference between revisions

Content deleted Content added
Syncing script from GitHub (via script)
Syncing script from GitHub (via script)
 
(13 intermediate revisions by the same user not shown)
Line 1:
// <nowikipre>
// Note: This script was compiled and minified from TypeScript. For a more readable version, see https://github.com/Eejit43/wikipedia-scripts/blob/main/scripts/highlight-homographs.ts
 
"use strict";(()=>{(()=>{let n=document.querySelector("#firstHeading");if(!n)return mw.notify("highlight-homographs: Could not find title element!",{type:"error"});for(let t of n.children)t.textContent&&(t.nodeType===Node.TEXT_NODE?n.replaceChild(document.createRange().createContextualFragment(o(t.textContent)),t):(t.classList.contains("mw-page-title-main")||t.tagName==="I")&&(t.innerHTML=o(t.innerHTML)));function o(t){return[...t].map(e=>/[\u0400-\u052F\u1D2B\u1D78\u2DE0-\u2DFF\uA640-\uA69F]/.test(e)||/[ɑΑΒΕΖΗΙΚΜΝΟΡΤΥΧνο]/.test(e)||/[ԼՏոսօ]/.test(e)||/[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯ]/i.test(e)?`<abbr title="This character is a homograph!" style="text-decoration: none; background-color: #ff5555">${e}</abbr>`:e).join("")}})();})();
"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);
});
/**
* Marks homographs in a string with a red background color
* @param {string} string The string to mark homographs in
* @returns {string} The string with homographs marked
*/
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('');
}
})();
 
// </nowikipre>
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc2NyaXB0cy9oaWdobGlnaHQtaG9tb2dyYXBocy50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiKCgpID0+IHtcbiAgICBjb25zdCB0aXRsZSA9IGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNmaXJzdEhlYWRpbmcnKTtcblxuICAgIGlmICghdGl0bGUpIHJldHVybiBtdy5ub3RpZnkoJ2hpZ2hsaWdodC1ob21vZ3JhcGhzOiBDb3VsZCBub3QgZmluZCB0aXRsZSBlbGVtZW50IScsIHsgdHlwZTogJ2Vycm9yJyB9KTtcblxuICAgIGZvciAoY29uc3QgZWxlbWVudCBvZiB0aXRsZS5jaGlsZHJlbikge1xuICAgICAgICBpZiAoIWVsZW1lbnQudGV4dENvbnRlbnQpIGNvbnRpbnVlO1xuICAgICAgICBpZiAoZWxlbWVudC5ub2RlVHlwZSA9PT0gTm9kZS5URVhUX05PREUpXG4gICAgICAgICAgICB0aXRsZS5yZXBsYWNlQ2hpbGQoZG9jdW1lbnQuY3JlYXRlUmFuZ2UoKS5jcmVhdGVDb250ZXh0dWFsRnJhZ21lbnQobWFya0hvbW9ncmFwaHMoZWxlbWVudC50ZXh0Q29udGVudCkpLCBlbGVtZW50KTtcbiAgICAgICAgZWxzZSBpZiAoZWxlbWVudC5jbGFzc0xpc3QuY29udGFpbnMoJ213LXBhZ2UtdGl0bGUtbWFpbicpIHx8IGVsZW1lbnQudGFnTmFtZSA9PT0gJ0knKVxuICAgICAgICAgICAgZWx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