Jump to content

User:Polygnotus/Scripts/SpaceBeforeRef.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>
// Add tool to remove whitespace before ref tags
$(function() {
    // Only run on edit pages
    if (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
        mw.loader.using(['mediawiki.util'], function() {
            mw.util.addPortletLink(
                'p-tb',
                '#',
                'Remove whitespace before refs',
                'ca-fix-ref-spacing',
                'Remove whitespace before <ref> tags'
            );
            
            $('#ca-fix-ref-spacing').on('click', function(e) {
                e.preventDefault();
                
                var $textarea = $('#wpTextbox1');
                var text = $textarea.val();
                var originalText = text;
                
                // Remove whitespace before ref tags
                // Matches one or more whitespace characters before <ref
                text = text.replace(/\s+(<ref[^>]*>)/g, '$1');
                
                if (text !== originalText) {
                    $textarea.val(text);
                    mw.notify('Whitespace before <ref> tags removed.', {type: 'success'});
                } else {
                    mw.notify('No whitespace before <ref> tags found.', {type: 'info'});
                }
            });
        });
    }
});
// </nowiki>