Jump to content

User:Polygnotus/Scripts/Bullets.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.
// <nowiki>
// Real-time accessibility checker for list markup in comments
// https://en.wikipedia.org/wiki/User:Isaacl/On_wikitext_list_markup
// https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Accessibility
// https://en.wikipedia.org/wiki/User_talk:Polygnotus#talk_page_comment_conventions



(function() {
    'use strict';
    
    // Only run on edit pages
    if (mw.config.get('wgAction') !== 'edit' && mw.config.get('wgAction') !== 'submit') {
        return;
    }
    
    var lastContent = '';
    var warningBox = null;
    
    function createWarningBox() {
        warningBox = $('<div>')
            .css({
                'background-color': '#fef6e7',
                'border': '2px solid #fc3',
                'padding': '10px',
                'margin': '10px 0',
                'border-radius': '3px',
                'display': 'none'
            })
            .html('<strong>List markup accessibility issue detected:</strong> You are switching between different list types (changing from * to : or : to * at the same nesting level). This causes screen readers to announce extra list end/start pairs. Consider using {{pb}} template for paragraph breaks instead, or maintain consistent list markup.');
        
        $('#wpTextbox1').before(warningBox);
    }
    
    function getListPrefix(line) {
        // Extract the prefix characters (*, :, #) from start of line
        var match = line.match(/^([*:#]+)/);
        return match ? match[1] : '';
    }
    
    function checkForListIssues(content) {
        var lines = content.split('\n');
        var hasIssue = false;
        
        for (var i = 0; i < lines.length - 1; i++) {
            var currentLine = lines[i].trim();
            var nextLine = lines[i + 1].trim();
            
            var currentPrefix = getListPrefix(currentLine);
            var nextPrefix = getListPrefix(nextLine);
            
            // Skip if either line isn't a list item
            if (!currentPrefix || !nextPrefix) continue;
            
            // Check if we're at the same nesting level
            if (currentPrefix.length === nextPrefix.length) {
                // Get the last character of each prefix (the actual list type at this level)
                var currentType = currentPrefix.charAt(currentPrefix.length - 1);
                var nextType = nextPrefix.charAt(nextPrefix.length - 1);
                
                // Problem: switching types at same level
                if (currentType !== nextType) {
                    hasIssue = true;
                    break;
                }
            }
        }
        
        if (hasIssue) {
            warningBox.slideDown(200);
        } else {
            warningBox.slideUp(200);
        }
    }
    
    function monitorEditBox() {
        var textbox = $('#wpTextbox1');
        if (textbox.length === 0) return;
        
        createWarningBox();
        
        // Check initially
        lastContent = textbox.val();
        checkForListIssues(lastContent);
        
        // Check every second
        setInterval(function() {
            var currentContent = textbox.val();
            if (currentContent !== lastContent) {
                lastContent = currentContent;
                checkForListIssues(currentContent);
            }
        }, 1000);
    }
    
    // Wait for the edit box to be ready
    $(document).ready(function() {
        monitorEditBox();
    });
})();
// </nowiki>