User:Jackninja5DipperGravityFalls/GoodFaithRevert.javascript

/* Good Faith Revert Tool based from Wikipedia. Used Ditto Duck Penguin's code as a template. */

var main = { init: function { if (mw.config.get('wgCanonicalSpecialPageName') == "Contributions") { $('#mw-content-text ul').find('li').each(function {                   if ($(this).find('.mw-uctop').length)                        $(this).append(' [undo good faith edit] ');                }); } else if (($.getUrlVar('diff') || $.getUrlVar('oldid')) && $('#differences-nextlink').length === 0) $('.mw-usertoollinks:last').after('   [undo good faith edit] '); $('.mw-custom-good-faith-link a').click(function {               main.getRevisionIdAndContent($(this).attr('data-id'));                $(this).parent.remove;            }); },       getRevisionIdAndContent: function(title) { var API = new mw.Api; API.get({               action: 'query',                prop: 'revisions',                titles: title,                rvprop: 'user|ids',                rvlimit: 1,                cb: new Date.getTime            }).done(function(d) {                if (!d.error) {                    var revisions;                    for (var i in d.query.pages) {                        revisions = d.query.pages[i].revisions;                    }                    var currentUser = revisions[0].user, //current user undoing a good faith                    edit, revId;                    for (var i in revisions) {                        if (revisions[i].user != currentUser) {                            lastUser = revisions[i].user;                            //remember last author                            revId = revisions[i].revid;                            //get revision to undo but assume good faith break; }                   }                    if (lastUser) { API.get({                           action: 'query',                            prop: 'revisions',                            rvprop: 'content',                            revids: revId,                            cb: new Date.getTime                        }).done(function(d) {                            if (!d.error) {                                var content = "";                                //can be no content on page so initialise empty as failsafe                                for (var i in d.query.pages) {                                    if (d.query.pages[i].revisions)                                        content = d.query.pages[i].revisions[0]["*"];                                }                                main.performUndo(title, content, currentUser, lastUser);                            } else                                new BannerNotification('Unable to undo edit (failed to get page content): ' + d.error.code,'error').show; }).fail(function { new BannerNotification('Unable to undo edit: failed to get page content!','error').show; });                   } else                    new BannerNotification('Unable to undo edit (failed to get revisions): ' + d.error.code,'error').show;            }).fail(function {                new BannerNotification('Unable to undo edit: failed to get revisions!','error').show;            }); },                   new BannerNotification('Unable to undo edit (failed to publish edit): ' + d.error.code,'error').show; }).fail(function { new BannerNotification('Unable to undo edit: failed to publish edit!','error').show; });       }    };    main.init;    mw.loader.using('mediawiki.api', function { var main = { init: function { if (mw.config.get('wgCanonicalSpecialPageName') == "Contributions") { $('#mw-content-text ul').find('li').each(function {                       if ($(this).find('.mw-uctop').length)                            $(this).append(' [undo good faith edit] ');                    }); } else if (($.getUrlVar('diff') || $.getUrlVar('oldid')) && $('#differences-nextlink').length === 0) $('.mw-usertoollinks:last').after('   [undo good faith edit] '); $('.mw-custom-good-faith-link a').click(function {                   main.getRevisionIdAndContent($(this).attr('data-id'));                    $(this).parent.remove;                }); },           getRevisionIdAndContent: function(title) { var API = new mw.Api; API.get({                   action: 'query',                    prop: 'revisions',                    titles: title,                    rvprop: 'user|ids',                    rvlimit: 1,                    cb: new Date.getTime                }).done(function(d) {                    if (!d.error) {                        var revisions;                        for (var i in d.query.pages) {                            revisions = d.query.pages[i].revisions;                        }                        var currentUser = revisions[0].user, //current user undoing good faith edit, revId;                        for (var i in revisions) {                            if (revisions[i].user != currentUser) {                                lastUser = revisions[i].user;                                //remember last author                                revId = revisions[i].revid; //get revision to undo but assume good faith break; }                       }                        if (lastUser) { API.get({                               action: 'query',                                prop: 'revisions',                                rvprop: 'content',                                revids: revId,                                cb: new Date.getTime                            }).done(function(d) {                                if (!d.error) {                                    var content = "";                                    //can be no content on page so initialise empty as failsafe                                    for (var i in d.query.pages) {                                        if (d.query.pages[i].revisions)                                            content = d.query.pages[i].revisions[0]["*"];                                    }                                    main.performUndo(title, content, currentUser, lastUser);                                } else new BannerNotification('Unable to undo (failed to get page content): ' + d.error.code,'error').show; }).fail(function { new BannerNotification('Unable to undo: failed to get page content!','error').show; });                       } else                        new BannerNotification('Unable to undo (failed to get revisions): ' + d.error.code,'error').show;                }).fail(function {                    new BannerNotification('Unable to undo: failed to get revisions!','error').show;                }); },                       new BannerNotification('Unable to undo (failed to publish edit): ' + d.error.code,'error').show; }).fail(function { new BannerNotification('Unable to undo: failed to publish edit!','error').show; });           }        };        main.init;    });