Jump to content

User:Macaw*/noRefListAlert.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
mw.hook('wikipage.content').add(function($content) {
    if (mw.config.get('wgAction') !== 'view') {
        return;
    }
    if (mw.config.get('wgIsRedirect')) {
        return;
    }
    if (mw.config.get('wgArticleId') === 0) {
        return;
    }

    var ns = mw.config.get('wgNamespaceNumber');
    var title = mw.config.get('wgTitle').replace(/_/g, ' ');
    if (title === 'Main Page') {
        return;
    }

    var isValidNamespace = (ns === 0 || ns === 118);
    var rawHTML = $content.html().toLowerCase();

    // Check if there is a <references tag anywhere or a <ref tag.
    var hasReferencesTag = rawHTML.indexOf('<references') !== -1 || rawHTML.indexOf('<ref') !== -1 || rawHTML.indexOf('{{reflist}}';

    // For {{reflist ...}}: look for the starting substring and then check for the closing "}}".
    var startPos = rawHTML.indexOf('{{reflist');
    var hasReflistTemplate = false;
    if (startPos !== -1) {
        var endPos = rawHTML.indexOf('}}', startPos);
        if (endPos !== -1) {
            hasReflistTemplate = true;
        }
    }

    if (isValidNamespace && !(hasReferencesTag || hasReflistTemplate)) {
        var warningMessage = document.createElement('div');
        warningMessage.style.backgroundColor = '#ffcc00';
        warningMessage.style.color = '#000';
        warningMessage.style.padding = '10px';
        warningMessage.style.fontSize = '16px';
        warningMessage.style.fontWeight = 'bold';
        warningMessage.style.position = 'relative';
        warningMessage.style.zIndex = '1000';
        warningMessage.style.marginBottom = '20px';
        warningMessage.innerHTML = 'Warning: This page does not contain a references section.';

        var $firstHeading = $('#firstHeading');
        if ($firstHeading.length) {
            $firstHeading.after(warningMessage);
        }
    }
});