Jump to content

MediaWiki:Gadget-LingoTools.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// 1) Disables Lingo tooltips of an acronym in the article that defines it; 2) Disables Lingo in arbitrary Namespaces from the frontend; 3) Disables Lingo in containers using arbitrary CSS classes (visual removal via Common.css)
$(function() {
// Namespaces to exclude
var blockedNamespaces = ['Template', 'Category', 'Module'], currentNS = mw.config.get('wgCanonicalNamespace');
// Classes to exclude
var blockedClasses = ['template-table', 'country-hub-infobox', 'campaign-instructions'];

if (blockedNamespaces.indexOf(currentNS) !== -1) {
    document.querySelectorAll('.mw-lingo-term').forEach(function(term) {
        term.parentNode.replaceChild(document.createTextNode(term.textContent), term);
    });
    console.log("Lingo processing is blocked in the '" + currentNS + "' namespace.");
    return;
}

blockedClasses.forEach(function(className) {
    var terms = document.querySelectorAll('.' + className + ' .mw-lingo-term');
    if (terms.length > 0) {
        terms.forEach(function(term) {
            term.parentNode.replaceChild(document.createTextNode(term.textContent), term);
        });
        console.log("Removed " + terms.length + " Lingo tooltip(s) from ." + className + " container(s).");
    }
});

var rawTitle = mw.config.get('wgTitle').replace(/_/g, ' ').trim(), acronym = null;
var titleMatch = rawTitle.match(/^(.+?)\s+\(([A-Z]{2,})\)$/);
if (titleMatch) {
    acronym = titleMatch[2];
    console.log("Using acronym from title parenthetical: " + acronym);
} else if (/^[A-Z]+$/.test(rawTitle)) {
    acronym = rawTitle;
    console.log("Using pure acronym from title: " + acronym);
} else {
    var longForm = rawTitle.replace(/\s*\(.*\)\s*$/, '');
    var fallback = longForm.split(/\s+/).map(function(word) { return word.charAt(0).toUpperCase(); }).join('');
    var firstParagraph = $('#mw-content-text p').first().text();
    var parenMatch = firstParagraph.match(/\(([A-Z]{2,})\)/);
    if (parenMatch) {
        var extracted = parenMatch[1];
        console.log("Extracted acronym from first paragraph: " + extracted);
        if (extracted === fallback) {
            acronym = extracted;
            console.log("Acronym matches fallback computed from title: " + acronym);
        } else {
            console.log("Extracted acronym (" + extracted + ") does not match computed fallback (" + fallback + "); not proceeding with tooltip removal");
            return;
        }
    } else {
        console.log("No acronym found in first paragraph; not proceeding with tooltip removal");
        return;
    }
}

console.log("Assuming definition for acronym: " + acronym);
var all = document.querySelectorAll('.mw-lingo-term');
var matching = Array.prototype.filter.call(all, function(term) { return term.textContent.trim() === acronym; });
console.log("Found " + matching.length + " element(s) with visible text '" + acronym + "'");
matching.forEach(function(term) {
    term.parentNode.replaceChild(document.createTextNode(term.textContent), term);
});
console.log("Finished processing Lingo tooltips for acronym: " + acronym);
});