Jump to content

User:Tpbradbury/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tpbradbury (talk | contribs) at 16:17, 3 June 2024 (change). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 highlightUnreferencedParts() {
    console.log("highlightUnreferencedParts 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');
    console.log("Total elements found:", elements.length);

    elements.forEach(function (element) {
        var sentences = element.innerHTML.split(/(?<=[.!?])\s+/);
        var highlighted = false;

        sentences.forEach(function(sentence, index) {
            if (!/<span class="reference">/.test(sentence) && !/class="highlight-unreferenced"/.test(sentence) && /[a-zA-Z0-9]/.test(sentence)) {
                sentences[index] = `<span class="highlight-unreferenced">${sentence}</span>`;
                highlighted = true;
            }
        });

        if (highlighted) {
            element.innerHTML = sentences.join(' ');
            console.log("Highlighted unreferenced parts in element:", element);
        }
    });
}

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

    $(document).ready(function() {
        console.log("Document ready");
        addStylesheet();
        highlightUnreferencedParts();
        console.log("Highlighting completed");
    });
});