User:Hotaru Natsumi/js/trackandrollback.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
* Auto Rollback
* Forked from WhitePhosphorus's MonitorContrib.js
* Version 0.0.3
* 2022-10-13
* By Hotaru_Natsumi
*/
(function($, mw) {
'use strict';
var delay = 10;
var now = null;
var dft = '<a href="'+mw.config.values.wgScript+'?title={title}&oldid={oldid}" class="mw-changeslist-date" title="{title}">{timestamp}</a>(<a href="'+mw.config.values.wgScript+'?title={title}&diff=prev&oldid={oldid}" class="mw-changeslist-diff" title="{title}">差异</a> | <a href="'+mw.config.values.wgScript+'?title={title}&action=history" class="mw-changeslist-history" title="{title}">历史</a>) <span class="mw-changeslist-separator">. .</span> {new}{minor} <a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'{title}" class="mw-contributions-title" title="{title}">{title}</a> <span class="mw-changeslist-separator">. .</span> <span dir="ltr" class="mw-plusminus-{sizediffsign}" title="更改后有{size}字节">({sizediff})</span><span class="mw-changeslist-separator">. .</span> {userLink}<span class="comment">({comment})</span><span id="hn-rvt-stat" style="background:#ffffaa">[等待回退...]</span>';
var minorEdit = '<abbr class="minoredit" title="该编辑为小编辑">小</abbr>';
var newPage = '<abbr class="newpage" title="该编辑创建了新页面">新</abbr>';
var userLink = '<a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'User:{user}" class="mw-userlink" title="User:{user}"><bdi>{user}</bdi></a><span class="mw-usertoollinks">(<a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'User_talk:{user}" class="mw-usertoollinks-talk" title="User talk:{user}">讨论</a> | <a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE/{user}" class="mw-usertoollinks-contribs" title="Special:用户贡献/{user}">贡献</a> | <a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'Special:%E5%B0%81%E7%A6%81/{user}" class="mw-usertoollinks-block" title="Special:封禁/{user}">封禁</a>)</span>';
var $content = null;
var $usernames = null;
var $results = null;
var $summary = null;
var getNewEdits = function (names, time) {
return $.ajax({
url: mw.util.wikiScript('api'),
data: {
action: 'query',
list: 'usercontribs',
uclimit: 'max',
ucstart: new Date(time).toISOString(),
ucprop: 'ids|title|timestamp|parsedcomment|size|sizediff|flags',
ucuser: names,
ucdir: 'newer',
format: 'json'
},
});
};
var printTop = function (html) {
$results.prepend($('<p>').html(html));
};
var sign = function (n) {
if (n === 0) {
return 'null';
}
return (n > 0 ? 'pos' : 'neg');
};
function Rollback(k, v){
var params = {
action: 'rollback',
title: k,
user: v,
format: 'json',
summary: $summary.val(),
},
api = new mw.Api();
api.postWithToken('rollback', params).fail(function (data) {
if (data == 'onlyauthor') {
$('#hn-rvt-stat').html('[回退失败:仅被回退用户为该页面的作者]').attr('id', 'hn-rvt-fail').attr('style', 'background: #ffaaaa');
} else if (data == 'alreadyrolled') {
$('#hn-rvt-stat').html('[回退失败:已被回退]').attr('id', 'hn-rvt-fail').attr('style', 'background: #ffaaaa');
} else if (data == 'permissiondenied') {
$('#hn-rvt-stat').html('[回退失败:无权回退]').attr('id', 'hn-rvt-fail').attr('style', 'background: #ffaaaa');
} else if (data != null) {
$('#hn-rvt-stat').html('[回退失败:未知错误]').attr('id', 'hn-rvt-fail').attr('style', 'background: #ffaaaa');
}
}).done(function (data) {
if (data != null) {$('#hn-rvt-stat').html('[<a href="'+mw.config.values.wgArticlePath.split("$1")[0]+'Special:Diff/' + data.rollback.revid + '">回退成功</a>]').attr('id', 'hn-rvt-success').attr('style', 'background: #aaffaa');}
});
}
var monitor = function () {
getNewEdits($.trim($usernames.val()).split("\n").join('|'), now).then(function(data) {
now = new Date().getTime();
var edits = [];
if (data.query && data.query.usercontribs) {
for (var i = 0; i < data.query.usercontribs.length; ++i) {
var rev = data.query.usercontribs[i];
printTop(dft.replace(/\{title\}/g, rev.title)
.replace(/\{oldid\}/g, rev.revid)
.replace(/\{timestamp\}/g, rev.timestamp)
.replace(/\{size\}/g, rev.size)
.replace(/\{sizediff\}/g, rev.sizediff)
.replace(/\{sizediffsign\}/g, sign(rev.sizediff))
.replace(/\{userLink\}/g, userLink.replace(/\{user\}/g, rev.user))
.replace(/\{comment\}/g, rev.parsedcomment)
.replace(/\{new\}/g, (rev.new === '' ? newPage : ''))
.replace(/\{minor\}/g, (rev.minor === '' ? minorEdit : '')));
console.log(typeof(rev.title));
console.log(rev.user);
Rollback(rev.title, rev.user);
}
}
});
};
var initWithName = function (name) {
document.getElementsByTagName("title")[0].innerText="自动回退 - " + mw.config.values.wgSiteName
$('#firstHeading').html('自动回退');
$content = $('#mw-content-text');
$content.html('用户名:<br/><textarea id="P4js-monitor-usernames" rows="5"></textarea><div style="width:100%; display:block; height:1px"></div>编辑摘要:<br/><textarea id="hnatsumi-editsummary" rows="5">已还原[[Special:Contributions/$2|$2]]([[User talk:$2|讨论]])的编辑至最后由[[User:$1|$1]]修订的版本:[[WP:VOA|纯粹破坏]])([[User:Hotaru Natsumi/TrackAndRollback|自动脚本]]协助,请复查</textarea>');
$usernames = $('#P4js-monitor-usernames');
if (name !== mw.config.values.wgUserName.replaceAll(" ", "_") && name !== mw.config.values.wgUserName) {
$usernames.val(name);
} else {
alert('您应该不希望自动回退自己的编辑吧?');
history.go(-1);
}
$summary = $('#hnatsumi-editsummary');
$summary.after('<div id="P4js-monitor-results"></div>');
$results = $('#P4js-monitor-results');
$summary.after($('<a></a>').html('清屏').attr('href', '#').click(function (e) {
e.preventDefault();
$results.html('');
}));
$summary.after('<div style="width:100%; display:block; height:1px"></div>');
now = new Date().getTime();
setInterval(monitor, delay * 1000);
};
var init = function () {
document.getElementsByTagName("title")[0].innerText="自动回退 - " + mw.config.values.wgSiteName
$('#firstHeading').html('自动回退');
$content = $('#mw-content-text');
$content.html('用户名:<br/><textarea id="P4js-monitor-usernames" rows="5"></textarea><div style="width:100%; display:block; height:1px"></div>编辑摘要:<br/><textarea id="hnatsumi-editsummary" rows="5">已还原[[Special:Contributions/$2|$2]]([[User talk:$2|讨论]])的编辑至最后由[[User:$1|$1]]修订的版本:[[WP:VOA|纯粹破坏]])([[User:Hotaru Natsumi/TrackAndRollback|自动脚本]]协助,请复查</textarea>');
$usernames = $('#P4js-monitor-usernames');
$summary = $('#hnatsumi-editsummary');
$summary.after('<div id="P4js-monitor-results"></div>');
$results = $('#P4js-monitor-results');
$summary.after($('<a></a>').html('清屏').attr('href', '#').click(function (e) {
e.preventDefault();
$results.html('');
}));
$summary.after('<div style="width:100%; display:block; height:1px"></div>');
now = new Date().getTime();
setInterval(monitor, delay * 1000);
};
mw.loader.using(['mediawiki.util'], function () {
$(mw.util.addPortletLink('p-tb', mw.util.getUrl('Special:BlankPage/TrackAndRollback'), '监视用户编辑并自动回退'));
if (mw.config.values.wgNamespaceNumber == -1 && mw.config.values.wgPageName.split('/')[0].split(':')[1] == '用户贡献' && mw.config.values.wgPageName.split('/')[1]) {
$(mw.util.addPortletLink('p-cactions', mw.util.getUrl('Special:BlankPage/TrackAndRollback/' + mw.config.values.wgPageName.split('/')[1]), '自动回退该用户新的编辑'));
}
if (mw.config.values.wgNamespaceNumber == 2 || mw.config.values.wgNamespaceNumber == 3) {
$(mw.util.addPortletLink('p-cactions', mw.util.getUrl('Special:BlankPage/TrackAndRollback/' + mw.config.values.wgTitle.split('/')[0]), '自动回退该用户新的编辑'));
}
});
if (mw.config.get('wgPageName').split(':')[1] == '空白页面/TrackAndRollback' && mw.config.get('wgNamespaceNumber') == -1) {
init();
}
if (mw.config.get('wgNamespaceNumber') == -1 && mw.config.get('wgPageName').split('/')[0].split(':')[1] == '空白页面' && mw.config.get('wgPageName').split('/')[1] == 'TrackAndRollback' && mw.config.get('wgPageName').split('/')[2] != undefined && mw.config.get('wgPageName').split('/')[2] != '' && mw.config.get('wgPageName').split('/')[3] == undefined) {
initWithName(mw.config.get('wgPageName').split('/')[2]);
}
if (mw.config.get('wgNamespaceNumber') == -1 && mw.config.get('wgPageName').split('/')[0].split(':')[1] == '空白页面' && mw.config.get('wgPageName').split('/')[1] == 'TrackAndRollback' && mw.config.get('wgPageName').split('/')[2] != undefined && mw.config.get('wgPageName').split('/')[2] != '' && mw.config.get('wgPageName').split('/')[3] != undefined && mw.config.get('wgPageName').split('/')[3] != '') {
alert('初始化失败:用户名中含有非法字符');
history.go(-1);
}
})(jQuery, mw);