User:PeriodicEditor/Words2Watch.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:PeriodicEditor/Words2Watch.
console.log("loading");
mw.loader.using('mediawiki.util').then(function () {
console.log("Subpage JS loaded!");
const rules = [
{regex: /\b(legendary|best|great(est)?|acclaimed|iconic|visionary|outstanding|leading|celebrated|popular|cutting[-\s]edge|innovative|revolutionary|extraordinary|brilliant|amazing|renowned|remarkable|prestigious|world[-\s]class|respected|notable|virtuoso|awesome|pioneer(ing)?|phenomenal|prominent|famous(ly)?|best|excellent)\b/gi, color: "yellow"},//Peacock words
{regex: /\b(cult|racist|pervert(ed)?|sexist|homophobic|transphobic|misogynistic|sect|fundamentalist|heretic|extremist|denialist|terrorist|freedom[-\s]fighter|bigot(ted)?|neo[-\s]nazi|pseudo|controversial)\b/gi, color:"OrangeRed"},//Contientous lables
{regex: /\b((some|many|most|people|scholars|scientists|experts) (say|remember|state|claim|declare)|it is (often )?(believed|considered|regarded|said|claimed)|is (widely )?regarded as)\b/gi, color:"ForestGreen"},//Weasel words
{regex: /\b(supposed|apparent|purported|alleged|accused|so[-\s]called)\b/gi, color:"orange"},//Expressions of doubt
{regex: /\b(notably|noted|arguably|interesting(ly)?|essentially|utterly|actually|only|clearly|absolutely|of course|without a doubt|indeed|happily|sadly|tragically|aptly|ironically|(un)?fortunately|untimely)\b/gi, color:"pink"},//Editorialising
{regex:/\b(passed away|gave (her|his|their) life|eternal (rest|sleep)|ma[kd](e|ing) love|an issue with|collateral damage|differently able(d)?)\b/gi, color:"cyan"},//euphamism
{regex:/\b(lion's share|tip of the iceberg|white elephant|gild(ing)? the lily|tak(ing|e) the plunge|ace up the sleeve|bird in the hand|twist of fate)\b/gi, color:"HotPink"},//Cliche
{regex:/\b(recent(ly)?|lately|current(ly)?|today|presently|to date|\d+ years ago|(this|last|next) (year|month|spring|summer|autumn|fall|winter)|yesterday|tomorrow|in the future|now|at the moment)\b/gi, color:"lightGreen"},//time relative
{regex:/\b(this (country|city|town)|here|somewhere|sometimes|often|occasionally|somehow)\b/gi, color:"DeepSkyBlue"},//unspecified
{regex:/\b(survived by|([A-z]+'s|his|her|their) survivors include)\b/gi},//survived by
];
let enabled = false;
// Add toggle link to the Tools sidebar
const link = mw.util.addPortletLink(
'p-cactions',
'#',
'Toggle highlights',
'ca-toggle-highlights'
);
link.addEventListener('click', function (e) {
e.preventDefault();
enabled = !enabled;
clearHighlights();
if (enabled) applyHighlights();
});
function clearHighlights() {
document.querySelectorAll(".my-highlight").forEach(el => {
el.replaceWith(document.createTextNode(el.textContent));
});
}
function applyHighlights() {
const content = document.querySelector("#mw-content-text");
if (!content) return;
walk(content);
}
// Walk the DOM and process only text nodes
function walk(node) {
let child = node.firstChild;
while (child) {
const next = child.nextSibling;
if (child.nodeType === Node.TEXT_NODE) {
highlightTextNode(child);
} else if (child.nodeType === Node.ELEMENT_NODE) {
// Skip elements that should NOT be touched
if (!["SCRIPT", "STYLE", "NOSCRIPT"].includes(child.tagName)) {
walk(child);
}
}
child = next;
}
}
function highlightTextNode(textNode) {
let text = textNode.nodeValue;
let replaced = false;
rules.forEach(rule => {
text = text.replace(rule.regex, match => {
replaced = true;
return `<span class="my-highlight" style="background:${rule.color};">${match}</span>`;
});
});
if (replaced) {
const wrapper = document.createElement("span");
wrapper.innerHTML = text;
textNode.replaceWith(wrapper);
}
}
});