Jump to content

User:Anomie/revdel-checkboxes.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anomie (talk | contribs) at 21:10, 18 February 2012 (Don't float it, that looks dumb). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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" />');
            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 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.prependTo(li);
        });
        loglists.forEach(function(ul,i){
            var button=$('<input type="button" value="Del/undel selected" />')
            button.click(function(ev){
                var q={
                    title:'Special:RevisionDelete',
                    type:'logging',
                    target:mw.config.get('wgPageName'),
                    ids:{}
                };
                var ct=0;
                $('input[name="revdel-ids-'+i+'"]').each(function(i,n){
                    if(n.checked){
                        ct++;
                        q.ids[n.value]=1;
                    }
                });
                if(ct == 0) $('input[name="revdel-ids-'+i+'"]').each(function(i,n){
                    ct++;
                    q.ids[n.value]=1;
                });
                if(ct > 0) location.href=mw.config.get('wgScript')+'?'+jQuery.param(q);
            });
            var buttondiv=$('<div></div>');
            buttondiv.append(button); 
            $(ul).before(buttondiv.clone(true));
            $(ul).after(buttondiv);
        });
    }
});