Jump to content

User:Jr Mime/WHAM.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.
// @WIKIA
// Scripts of:
//    WHAM
//    Grunny's delete all and rollback all
//    Adaptation to Wikipedia by [http://c.wikia.com/wiki/User:Jr_Mime User:Jr_Mime]
if (mw.config.get("wgCanonicalSpecialPageName") === 'Contributions') {

    
    var username = mw.config.get("wgPageName").split("/")[1],
        token = mw.user.tokens.get("editToken"),
        r1 = (typeof(r1u) === 'undefined') ? '[[w:Help:Vandalism|vandalism]]' : r1u,
        r2 = (typeof(r2u) === 'undefined') ? 'reverting/deleting [[w:Help:Spam|spam]] or [[w:Help:Vandalism|vandalism]]' : r2u;
    
    $('.mw-contributions-form').before('<a id="WHAMdelete" style="cursor:pointer">Delete</a> | ' +
        '<a id="WHAMrollback" style="cursor:pointer">Rollback</a> | ' +
        '<input type="text" id="WHAMblock" value="2 weeks"> <a id="WHAMblockButton" style="cursor:pointer">Block</a> | ' +
        '<a id="WHAMall" style="cursor:pointer">All</a>');
    
    $('#WHAMrollback').on('click', function() {
        $('.mw-rollback-link a').each(function(i) {
            var obj = $(this),
            href = obj.attr('href');
            setTimeout(function() {
    	$.get(href);
    	console.log('Rollback number ' + i + ' successful!');
    	obj.text('gone!').css({'color':'grey','text-decoration':'line-through'}).removeAttr('href').parents().eq(1).css({'color':'grey','text-decoration':'line-through'}).children().removeAttr('href').css({'color':'grey','text-decoration':'line-through'});
        }, i*500);
        });
    });
    
    $('#WHAMblockButton').on('click', function() {
        new mw.Api().post({
        format: 'json',
        action: 'block',
        user: username,
        expiry: $("#WHAMblock").val(),
        nocreate: 0,
        autoblock: 0,
        reason: r1,
        bot: true,
        token: token
        }).done(function(d) { 
          if (!d.error) {
            console.log(username+' has been blocked successfully!');
            alert('User has been blocked!');
          } else {
            alert('Failed to block '+username+': '+ d.error.code);
          }
        }).fail(function() {
          alert('Failed to block '+username+'!');
        });
    });
    
    function apiDelete(page,reason) {
        new mw.Api().post({
        format: 'json',
        action: 'delete',
        title: page,
        reason: r2,
        bot: true,
        token: token
        })
        .done(function(d) { 
        if (!d.error) {
            console.log('Deletion of '+page+' successful!');
        } else {
            console.log('Failed to delete '+page+': '+ d.error.code);
        }
        })
        .fail(function() {
            console.log('Failed to delete '+page+'!');
        });
    }
    
    $('#WHAMdelete').on('click', function WMdelete() {
        $('li .newpage ~ a').each(function() {
            var title = $(this).attr('title');
            apiDelete(title);
            $(this).parent().css({'color':'grey','text-decoration':'line-through'}).children().removeAttr('href').css({'color':'grey','text-decoration':'line-through'});
        });
    });
    
    $('#WHAMall').on('click', function() {
        $("#WHAMrollback, #WHAMdelete, #WHAMblockButton").click();
    });
}