User:魔琴/gadgets/edit-conflict-detector.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// Global version: [[:meta:User:魔琴/edit-conflict-detector.js]]
mw.loader.using(['mediawiki.util', 'mediawiki.api']).done( function() {
if(!/[&?](ve)?action=edit/.test(window.location.search)) // Not edit mode
return;
var newrev ;
var rev = mw.config.get('wgCurRevisionId');
if( rev === 0) // Page does not exist
return;
var warned = 0;
var changed = 0;
var warn = {};
var pagename = mw.config.get('wgPageName');
var API = new mw.Api();
var newrevs = {};
$.when(mw.loader.using('mediawiki.jqueryMsg'), $.ready)
.then(function () {
warn = {
edit: mw.libs.HanAssist.localize( { hans: '检测到编辑冲突!', hant: '檢測到編輯衝突!' } ),
gone: mw.message('Deletedwhileediting').text()
};
setInterval(checkEC, 5000);
});
function checkEC () {
API.get({
'action': 'query',
'format': 'json',
'prop': 'revisions',
'titles': pagename,
'rvprop': 'ids'
}).done(function(data){
var pages = data.query.pages;
var id = Object.keys(pages)[0];
var revision = pages[id].revisions[0];
var parameters = {
diff: revision.revid,
oldId: revision.parentid
};
newrev = parameters.diff;
if ( newrev > rev ) {
alertEC(warn.edit);
} else if (newrev===0){
if ( warned!==3 ) { warned = 1; }
alertEC(warn.gone);
}
});
}
function alertEC(warntype) {
if (newrevs[newrev] !== 1) {
mw.notify('Sensed edit conflict...');
}
promise = API.post({
action: 'parse',
contentmodel: 'wikitext',
text: ' ' + warntype + ' ',
title: pagename,
prop: 'text',
formatversion: '2'
}).done(function(data){
if (newrevs[newrev] !== 1) {
newrevs[newrev] = 1;
mw.notify($(`<div>${data.parse.text}</div>`));
}
if (changed===0) { // 变色
$("#wpSave").css("background-color", "#b33");
$("#wpSaveWidget").css("background-color", "#b33");
$(".ve-ui-toolbar-saveButton").css("background-color", "#b33");
}
if (warned<2){ // 只有第一次是编辑冲突,或首次遇到页面删除时才提醒
$("#wpSaveWidget").append('<div style="color:#b33;background-color:#ffd;font-weight:bold">' + data.parse.text + '</div>');
$(".ve-ui-toolbar-saveButton").append('<div style="color:#b33;background-color:#ffd">' + data.parse.text + '</div>');
}
}).fail(function(error){
mw.notify(warntype);
console.log('[Edit-conflict-detector] Failed: '+error);
}).always(function(){
change = 1;
warned = changed === 0 ? 2 : warned;
warned = warned === 1 ? 3 : warned;
});
}
}(jQuery, window, document));