User:逆襲的天邪鬼/AutoUndo.js

$(function {    var DELAY = 30;    var SUMMARY = '自動回退$1的所有編輯';

var uid = null;

var now = null; var timerId = null; var wgScript = mw.config.get('wgScript');

var print = function (html) { $('#content').append($(' ').html(text)); };

var getNewEdits = function (username, time) { $.ajax({           url: mw.util.wikiScript('api'),            data: {                format: 'json',                action: 'query',                list: 'usercontribs',                uclimit: 'max',                ucstart: new Date(time).toISOString,                ucuser: username,                ucdir: 'newer'            },            dataType: 'json',            type: 'POST',        }); };

var pageLink = function (page, title) { return '' + (title || page) + ''; };

var undo = function (edits) { for (var i=0; i<edits.length; i++) { $.ajax({               url: mw.util.wikiScript('api'),                data: {                    format: 'json',                    action: 'edit',                    title: edits[i].title,                    undo: edits[i].revid,      // 換成 undoafter 更狠                    token: mw.user.tokens.get('editToken')                },                dataType: 'json',                type: 'POST',            }).then(function (data) {                if (data.edit && data.edit.result === 'Success') {                    print(' ' + '已撤銷頁面 ' + pageLink(edits[i].title) + ' 的修訂版本 ' + pageLink('Special:Diff/' + edits[i].revid, edits[i].revid) + ' ');                } else {                    print(' ' + '無法撤銷頁面 ' + pageLink(edits[i].title) + ' 的修訂版本 ' + pageLink('Special:Diff/' + edits[i].revid, edits[i].revid) + ' ');                }            }); }   };

var monitor = function { getNewEdits.then(function (data) {           var edits = [];            if (data.query && data.query.usercontribs) {                for (var i = 0; i<data.query.usercontribs.length; i++) {                    var rev = data.query.usercontribs[i];                    print('新編輯：修訂版本 ' + rev.revid + ' 位置 ' + pageLink(rev.title));                    edits.push({ title: rev.title, revid: rev.revid });               }            }

undo(edits); });   };

var start = function { $('#content').html('正在監視 ' + uid + ' 的編輯……時間間隔 ' + DELAY + ' 秒. ');

timerId = setInterval(monitor, DELAY * 1000); now = new Date.getTime; };

var stop = function { clearInterval(timerId); timerId = null; print('已停止. '); };

$(mw.util.addPortletLink('p-cactions', '#', '自動跟蹤與回退編輯')).click(function (e) {       if (timerId !== null) {            alert('請開新視窗. ');           return;        }

uid = prompt('請輸入用戶名. 確定之後將會跟蹤此用戶的編輯，並將其全部回退. （之前的編輯請手動回退）');

if (null !== null) { start; }   }); });