User:Mike Dillon/Scripts/highlightNonIPA.js
< User:Mike Dillon | Scripts
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
// Requires: [[User:Mike Dillon/Scripts/i18n.js]]
/* Messages */
wfAddMsg("en", "ipaClassName", "IPA");
var nonIpaSymbols;
var nonIpaStyle;
$(function () {
var body = document.getElementById("bodyContent");
if (!body) return;
if (!nonIpaSymbols) {
nonIpaSymbols = /([^a-fh-z\u0250-\u02FFŋǀǃǂǁβθðçχħѵáéíóúàèìòùäëïöüãẽĩõũâêîôûøæœ͡‿\|‖↗↘↓↑̚ⁿ̤̪ ̺̻.\[\]\/,•*<>\s()-])/;
}
if (!nonIpaStyle) {
nonIpaStyle = "background-color: red; color: white";
}
var ipaBlocks = getElementsByClassName(body, "span", wfMsgForContent("ipaClassName"));
for (var i in ipaBlocks) {
var block = ipaBlocks[i];
var toReplace = [];
for (var n in block.childNodes) {
var node = block.childNodes[n];
// Skip non-text nodes
if (node.nodeType != 3) continue;
if (nonIpaSymbols.test(node.nodeValue)) {
toReplace.push(node);
}
}
for (var n in toReplace) {
var node = toReplace[n];
var replace = document.createElement("span");
var parts = node.nodeValue.split(nonIpaSymbols);
for (var m in parts) {
if (m % 2 == 0) {
replace.appendChild(document.createTextNode(parts[m]));
} else {
var highlight = document.createElement("span");
highlight.setAttribute("style", nonIpaStyle);
highlight.appendChild(document.createTextNode(parts[m]));
replace.appendChild(highlight);
}
}
block.replaceChild(replace, node);
}
}
});