Jump to content

User:Tpbradbury/monobook.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("Monobook.js is loading");

$(function() {
    var search = document.getElementById("p-search");
    var cactions = document.getElementById("p-cactions");
    if (search && cactions) {
        document.getElementById("column-one").insertBefore(search, cactions);
    }
});

mw.loader.load("https://en.wiktionary.org/w/index.php?title=User:Connel_MacKenzie/spellcheck.js&action=raw&ctype=text/javascript");

importStylesheet('User:Anomie/linkclassifier.css');

function addStylesheet() {
    const stylesheet = document.createElement('style');
    stylesheet.innerHTML = `
    .highlight-unreferenced {
        background-color: LightPink !important;
        border: 2px solid red !important;
    }
    `;
    document.head.appendChild(stylesheet);
    console.log("Stylesheet added");
}

function highlightUnreferencedPassages() {
    console.log("highlightUnreferencedPassages function is executed");

    var contentText = document.getElementById('mw-content-text');
    if (!contentText) {
        console.log("mw-content-text element not found");
        return;
    }
    console.log("mw-content-text element found");

    var elements = contentText.querySelectorAll('p, li, h2, h3');
    console.log("Total elements found:", elements.length);

    var stopHighlighting = false;
    var stopSections = ["references", "see also", "external links", "further reading", "citations", "notes", "bibliography", "explanatory notes"];

    elements.forEach(function (element) {
        if (stopHighlighting) return;

        var tag = element.tagName.toLowerCase();
        if ((tag === 'h2' || tag === 'h3') && stopSections.some(section => element.textContent.trim().toLowerCase().includes(section))) {
            stopHighlighting = true;
            console.log("Stopping at section:", element.textContent.trim().toLowerCase());
            return;
        }

        if (!stopHighlighting && (tag === 'p' || tag === 'li') && element.querySelectorAll('.reference').length === 0) {
            element.classList.add('highlight-unreferenced');
            console.log("Highlighted unreferenced element:", element);
        }
    });
}

function isMainNamespaceContentPage() {
    var namespace = mw.config.get('wgNamespaceNumber');
    var action = mw.config.get('wgAction');
    return namespace === 0 && action === 'view';
}

mw.loader.using(['mediawiki.util'], function () {
    console.log("HighlightUnreferencedPassages.js is loading");

    $(document).ready(function() {
        console.log("Document ready");
        if (isMainNamespaceContentPage()) {
            addStylesheet();
            highlightUnreferencedPassages();
            console.log("Highlighting completed");
        } else {
            console.log("Not a main namespace content page, skipping highlighting");
        }
    });
});