Jump to content

User:Anomie/revdel-checkboxes.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.
$(document).ready(function($){
    // File description pages
    var filehistory=$('.filehistory');
    if(filehistory && filehistory.length){
        var ct=0;
        filehistory.find('.mw-revdelundel-link a').each(function(i,a){
            var m=a.href.match(/&ids=([0-9]+)/);
            if(!m) return;
            var chk=$('<input type="checkbox" name="revdel-ids" value="'+m[1]+'" />');
            var td=a;
            while(td.nodeName!='TD') td=td.parentNode;
            chk.appendTo(td);
            ct++;
        });
        if(ct>0){
            var button=$('<input type="button" value="Del/undel selected" class="file-revdel-button" />');
            button.click(function(ev){
                var q={
                    title:'Special:RevisionDelete',
                    type:'oldimage',
                    target:mw.config.get('wgPageName'),
                    ids:{}
                };
                var ct=0;
                $('input[name="revdel-ids"]').each(function(i,n){
                    if(n.checked){
                        ct++;
                        q.ids[n.value]=1;
                    }
                });
                if(ct == 0) $('input[name="revdel-ids"]').each(function(i,n){
                    ct++;
                    q.ids[n.value]=1;
                });
                if(ct > 0) location.href=mw.config.get('wgScript')+'?'+jQuery.param(q);
            });
            filehistory.after(button);
        }
    }

    // Special:Log and other log lists
    var loglines=$('li[class^="mw-logline-"] > .mw-revdelundel-link a');
    if(loglines && loglines.length){
        var loglists=[];
        loglines.each(function(i,a){
            var m=a.href.match(/&ids=([0-9]+)/);
            if(!m) return;
            var t=a.href.match(/&target=([^&]+)/);
            t=t?decodeURIComponent(t[1]):mw.config.get('wgPageName');
            var li=a.parentNode.parentNode;
            var ul=li.parentNode;
            var idx=loglists.indexOf(ul);
            if(idx<0){
                idx=loglists.length;
                loglists[idx]=ul;
            }
            var chk=$('<input type="checkbox" name="revdel-ids-'+idx+'" value="'+m[1]+'" />');
            chk.attr('data-target', t);
            chk.prependTo(li);
        });
        loglists.forEach(function(ul,i){
            var button=$('<input type="button" value="Del/undel selected" class="log-revdel-button" />')
            button.click(function(ev){
                var q={
                    title:'Special:RevisionDelete',
                    type:'logging',
                    target:null,
                    ids:{}
                };
                var ct=0;
                $('input[name="revdel-ids-'+i+'"]').each(function(i,n){
                    if(n.checked){
                        ct++;
                        q.ids[n.value]=1;
                        var t=$(n).attr('data-target');
                        if(q.target===null){
                            q.target=t;
                        } else if(q.target!=t){
                            q.target=mw.config.get('wgPageName');
                        }
                    }
                });
                if(ct == 0) $('input[name="revdel-ids-'+i+'"]').each(function(i,n){
                    ct++;
                    q.ids[n.value]=1;
                    var t=$(n).attr('data-target');
                    if(q.target===null){
                        q.target=t;
                    } else if(q.target!=t){
                        q.target=mw.config.get('wgPageName');
                    }
                });
                if(ct > 0) location.href=mw.config.get('wgScript')+'?'+jQuery.param(q);
            });
            var buttondiv=$('<div class="log-revdel-button-wrapper"></div>');
            buttondiv.append(button); 
            $(ul).before(buttondiv.clone(true));
            $(ul).after(buttondiv);
        });
    }
});