User:Liangent/Gadgets/Toolkit/patrol.uncompressed.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

require(['jquery', 'mediawiki'], function() {
    jQuery(function() {
        var page = new MediaWiki.Page();
        
        // patrol edit
    
        if ((wgAction == 'edit' || wgAction == 'submit') && wgArticleId != 0) { // edit & preview
            // wgArticleId != 0: article exists. if so, patrolled must be true?
            // should this be fixed? and before fixing, wgArticleId != 0 is not needed?
            // following if (!patrolled) can stop the checkbox from being added?
            page.patrolled({}, function(patrolled) {
                if (!patrolled) {
                    jQuery('#wpMinoredit').before(
                        '<input type="checkbox" id="wpPatroledit" accesskey="a" />&nbsp;' + 
                        '<label for="wpPatroledit" title="Mark this page as patrolled before saving [a]">' + 
                        'Patrol before saving</label>\n'
                    );
                }
            });
            jQuery('#wpSave').click(function(event) {
                // if patrolled, #wpPatroledit doesn't exist,
                // so jQuery('#wpPatroledit:checked').length always == 0
                if (jQuery('#wpPatroledit:checked').length != 0) {
                    event.preventDefault();
                    jQuery('#wpSave').attr('disabled', true);
                    var oldval = jQuery('#wpSave').val();
                    jQuery('#wpSave').val('Patrolling...');
                    new MediaWiki.Page().patrol({}, function() {
                        jQuery('#wpSave').val('Saving...');
                        jQuery('#editform')[0].submit();
                    }, function() {
                        if (confirm('Patrolling failed. Save anyway?')) {
                            jQuery('#wpSave').val('Saving...');
                            jQuery('#editform')[0].submit();
                        } else {
                            jQuery('#wpSave').val(oldval);
                            // #wpSave is always enabled when page is loaded,
                            // or does not appear. and $('#wpSave').click cannot be triggered.
                            jQuery('#wpSave').attr('disabled', false);
                        }
                    });
                }
            });
        }
    
        // patrol any page
    
        if ((wgAction == 'view' || wgAction == 'purge') && wgNamespaceNumber >= 0) {
            page.patrolled({}, function(patrolled) {
                if (!patrolled) {
                    jQuery('.printfooter').before(
                        '<div class="patrollink">[<a href="#" ' +
                        'class="ajaxpatrollink">Mark this page as patrolled</a>]</div>'
                    );
                    jQuery('.ajaxpatrollink').click(function() {
                        page.patrol({}, function() {
                            jQuery('.ajaxpatrollink').replaceWith('patrolled');
                        }, function() {
                            alert('Failed to patrol. Maybe this page is too old or already patrolled.');
                        });
                        return false;
                    });
                }
            });
        }
    
        // patrol link on [[Special:NewPages]]
    
        if (wgCanonicalSpecialPageName == 'Newpages') {
            jQuery('li.not-patrolled').each(function() {
                var litag = this;
                var pageaj = jQuery(jQuery('a', litag)[0]);
                pageaj.after(
                    '<span style="vertical-align: super;">(<a href="#" class="newpages_patrollink">patrol</a>)</span>'
                );
                var patrolaj = jQuery('a.newpages_patrollink', litag);
                patrolaj.click(function() {
                    new MediaWiki.Page(pageaj.text()).patrol({}, function() {
                        patrolaj.replaceWith('patrolled'); // or simply remove the span?
                        jQuery(litag).removeClass('not-patrolled');
                    }, function() {
                        alert('Failed to patrol [[' + pageaj.text() +
                            ']]. Maybe this page is too old or already patrolled.');
                    });
                    return false;
                });
            });
        }
    });
});