Difference between revisions of "User:DollarStoreBa'al/common.js"

Explain xkcd: It's 'cause you're dumb.
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
console.log("your JS loaded it didn't do anything");
+
// This script implements a very crude one-click undo. If you go onto a diff page with an undo link, this will change it to work in one single click rather than two.
/**
+
// It also adds a "revert" link to edits on contribution pages, that works the same way (one click).
* Rollback
+
// Known glitch: Does not work as intended if a user has edited the same page multiple times (to avoid this, I recommend ticking the "Only show edits that are latest revisions" box.
* @description Perform rollbacks without needing to be in the usergroup
+
 
* @author Ozank
+
function rvt(undop, undoafterp, c, tl) { // Implements one-click undo
*/
+
     $.ajax({
(function() {
+
         url: 'https://www.explainxkcd.com/wiki/api.php',
    'use strict';
+
         data: {
    // Exit if normal rollback links are present or the wiki disabled the script
+
             format: 'json',
     if ($('.mw-rollback-link').length || window.RollbackWikiDisable) {
+
             action: 'edit',
         return;
+
             title: atob(tl),
    }
+
            undo: undop,
    var Rollback = $.extend(window.Rollback, {
+
            undoafter: undoafterp,
         config: mw.config.get([
+
            summary: "Reverted vandalism with [[User:DollarStoreBa'al/common.js]]",
             'wgAction',
+
             token: mw.user.tokens.get('csrfToken')
             'wgCanonicalSpecialPageName',
 
             'wgPageName'
 
        ]),
 
        user: 'Ex Kay Cee Dee',
 
        _preload: 1,
 
        preload: function() {
 
             if (--this._preload === 0) {
 
                this.init();
 
            }
 
 
         },
 
         },
         init: function() {
+
         dataType: 'json',
            this.api = new mw.Api();
+
        type: 'POST',
            this.performRollbackCallback = this.performRollbackCallback
+
        success: function () { if (c) { $('.mw-diff-undo > a').text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } else { $('#link' + undop).text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } }
                .bind(this);
+
    });
            this.getTop().then(function(data) {
+
}
                const arr = data.query.usercontribs;
+
try {
                console.log(arr);
+
    var href = $('.mw-diff-undo > a').attr('href').split("=");
                const interval = setInterval(function() {
+
    var undop = href[4];
                    const rev = arr.shift();
+
    var undoafterp = href[3].substr(0, href[3].length - 5);
                    if (!rev) {
+
    $('.mw-diff-undo > a').removeAttr('href');
                        clearInterval(interval);
+
    $('.mw-diff-undo > a').attr('onclick', 'rvt(' + undop + ',' + undoafterp + ',1,"'+btoa($("#firstHeading").text().substr(33, $("#firstHeading").text().length - 34))+'")');
                        window.location.reload();
+
}
                        return;
+
catch (e) { console.log(e); console.log("assumed not undoable"); }
                    }
+
 
                    this.getRevisionIdAndContent(rev.title, rev.user);
+
 
                    console.log(arr.length);
+
function makeLinks(str, titles) { // Implements undo links on contribs page
                }.bind(this), 50);
+
    $.ajax({
            }.bind(this));
+
        url: 'https://www.explainxkcd.com/wiki/api.php',
        },
+
        type: 'GET',
        getTop: function() {
+
        dataType: 'json',
            return this.api.get({
+
        data: { format: 'json', action: 'query', prop: 'revisions', titles: str, rvprop: 'ids', formatversion: 2 },
                action: 'query',
+
         success: function (data) {
                list: 'usercontribs',
+
             for (var i = 0; i < data["query"]["pages"].length; i++) {
                ucuser: this.user,
+
                 ob = data["query"]["pages"][i]
                uctoponly: 1,
+
                pg = ob["title"];
                uclimit: 'max'
+
                if (titles[pg] == "done") continue;
            });
+
                el = titles[pg];
        },
+
                 $('<span id="span' + ob["revisions"][0]["revid"] + '"></span>').insertAfter(el);
        getRevisionIdAndContent: function(title, target) {
+
                 $("#span" + ob["revisions"][0]["revid"]).html(' <strong>[<a id="link' + ob["revisions"][0]["revid"] + '" onclick=\'rvt(' + ob["revisions"][0]["revid"] + ',' + ob["revisions"][0]["parentid"] + ',0,"'+btoa(pg)+'")\'>revert</a>]</strong>');
            this.api.get({
+
                titles[pg] = "done";
                action: 'query',
 
                cb: Date.now(),
 
                indexpageids: 1,
 
                prop: 'revisions',
 
                rvlimit: 'max',
 
                rvprop: 'user|ids',
 
                titles: title
 
            }).done(this.getRevisionIdCallback.bind(this, target)).fail(
 
                this.outputError.bind(this, 'revisionFail')
 
            );
 
        },
 
         getRevisionIdCallback: function(target, data) {
 
             if (data.error) {
 
                 this.outputError('revisionFail', data.error.code);
 
                return;
 
            }
 
            var revisions = data.query.pages[data.query.pageids[0]].revisions;
 
            // Don't rollback if the page has been edited by somebody else
 
            if (target !== revisions[0].user) {
 
                 console.log('edit conflict');
 
                // this.outputError('editConflict');
 
                 return;
 
            }
 
            var lastUser, revId;
 
            for (var i in revisions) {
 
                if (revisions[i].user !== target) {
 
                    // Remember last author
 
                    lastUser = revisions[i].user;
 
                    // Get revision to revert to
 
                    revId = revisions[i].revid;
 
                    break;
 
                }
 
 
             }
 
             }
            if (!lastUser) {
 
                this.outputError('singleEditor');
 
                return;
 
            }
 
            this.api.get({
 
                action: 'query',
 
                cb: Date.now(),
 
                indexpageids: 1,
 
                prop: 'revisions',
 
                revids: revId,
 
                rvprop: 'content'
 
            }).done(
 
                this.getRevisionContentCallback.bind(this, target, lastUser)
 
            ).fail(
 
                this.outputError.bind(this, 'contentFail', undefined)
 
            );
 
        },
 
        getRevisionContentCallback: function(target, lastUser, data) {
 
            if (data.error) {
 
                this.outputError('contentFail', data.error.code);
 
                return;
 
            }
 
            // Can be no content on page
 
            var page = data.query.pages[data.query.pageids[0]],
 
                content = page.revisions ? page.revisions[0]['*'] : '';
 
            this.performRollback(page.title, content, target, lastUser);
 
        },
 
        performRollback: function(page, text, user, user2) {
 
            this.api.post({
 
                action: 'edit',
 
                bot: true,
 
                minor: true,
 
                summary: 'rv',
 
                text: text,
 
                title: page,
 
                token: mw.user.tokens.get('editToken')
 
            }).done(this.performRollbackCallback).fail(
 
                this.outputError.bind(this, 'editFail')
 
            );
 
        },
 
        performRollbackCallback: function(data) {
 
            if (data.error) {
 
                this.outputError('editFail', data.error.code);
 
            } else {
 
                // mw.notify('Reverted successfully');
 
            }
 
        },
 
        outputError: function(message, code) {
 
            mw.notify('error: ' + message + ' ' + code, {
 
                type: 'error'
 
            });
 
 
         }
 
         }
     });
+
     })
    mw.loader.using([
+
}
        'mediawiki.api',
+
 
        'mediawiki.user',
+
cs = $('.mw-contributions-title');
        'mediawiki.util',
+
titles = {};
        'mediawiki.notification'
+
str = "";
     ], Rollback.preload.bind(Rollback));
+
for (var i = 0; i < cs.length; i++) {
})();
+
     titles[cs[i].getAttribute("title")] = cs[i];
console.log("no of course it did something");
+
    str += cs[i].getAttribute("title") + '|'
 +
    makeLinks(str.slice(0, -1), titles);
 +
}

Latest revision as of 17:00, 12 November 2025

// This script implements a very crude one-click undo. If you go onto a diff page with an undo link, this will change it to work in one single click rather than two.
// It also adds a "revert" link to edits on contribution pages, that works the same way (one click).
// Known glitch: Does not work as intended if a user has edited the same page multiple times (to avoid this, I recommend ticking the "Only show edits that are latest revisions" box.

function rvt(undop, undoafterp, c, tl) { // Implements one-click undo
    $.ajax({
        url: 'https://www.explainxkcd.com/wiki/api.php',
        data: {
            format: 'json',
            action: 'edit',
            title: atob(tl),
            undo: undop,
            undoafter: undoafterp,
            summary: "Reverted vandalism with [[User:DollarStoreBa'al/common.js]]",
            token: mw.user.tokens.get('csrfToken')
        },
        dataType: 'json',
        type: 'POST',
        success: function () { if (c) { $('.mw-diff-undo > a').text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } else { $('#link' + undop).text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } }
    });
}
try {
    var href = $('.mw-diff-undo > a').attr('href').split("=");
    var undop = href[4];
    var undoafterp = href[3].substr(0, href[3].length - 5);
    $('.mw-diff-undo > a').removeAttr('href');
    $('.mw-diff-undo > a').attr('onclick', 'rvt(' + undop + ',' + undoafterp + ',1,"'+btoa($("#firstHeading").text().substr(33, $("#firstHeading").text().length - 34))+'")');
}
catch (e) { console.log(e); console.log("assumed not undoable"); }


function makeLinks(str, titles) { // Implements undo links on contribs page
    $.ajax({
        url: 'https://www.explainxkcd.com/wiki/api.php',
        type: 'GET',
        dataType: 'json',
        data: { format: 'json', action: 'query', prop: 'revisions', titles: str, rvprop: 'ids', formatversion: 2 },
        success: function (data) {
            for (var i = 0; i < data["query"]["pages"].length; i++) {
                ob = data["query"]["pages"][i]
                pg = ob["title"];
                if (titles[pg] == "done") continue;
                el = titles[pg];
                $('<span id="span' + ob["revisions"][0]["revid"] + '"></span>').insertAfter(el);
                $("#span" + ob["revisions"][0]["revid"]).html(' <strong>[<a id="link' + ob["revisions"][0]["revid"] + '" onclick=\'rvt(' + ob["revisions"][0]["revid"] + ',' + ob["revisions"][0]["parentid"] + ',0,"'+btoa(pg)+'")\'>revert</a>]</strong>');
                titles[pg] = "done";
            }
        }
    })
}

cs = $('.mw-contributions-title');
titles = {};
str = "";
for (var i = 0; i < cs.length; i++) {
    titles[cs[i].getAttribute("title")] = cs[i];
    str += cs[i].getAttribute("title") + '|'
    makeLinks(str.slice(0, -1), titles);
}