User:魔琴/gadgets/dykc-edit-type/index.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//<nowiki>
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'ext.gadget.site-lib'/* ,'ext.gadget.HanAssist'*/], function () {
function han(s, t) {
return wgULS(s, t);
//return HanAssist.localize( { hans: s, hant: t } );
}
var cmt = "";
var dl = null;
var cj = {};
var cjs = {};
var API = new mw.Api();
var dykn = 'Wikipedia:新条目推荐/候选';
var html = "";
var regType = /^(.|\n)*type *= *(.*?)\n(.|\n)*?$/g;
var regArticle = /^(.|\n)*article *= *(.*?)\n(.|\n)*?$/g;
var regTypeF = /.*type *= *(.*)/gm;
var regArticleF = /.*article *= *(.*)/gm;
var first = '';
var articlepath = mw.config.get('wgArticlePath');
var script = mw.config.get('wgScript');
if (mw.config.get('wgPageName') === dykn) {
processNom();
} else {
return;
}
function opendl(article) {
cmt = "";
undwarning = "";
if (article === "undefined") undwarning = "<p><span style='color:#f00;font-size:large'>" + han("注意:条目名探测结果为undefined,这可能是程序错误导致,请考虑", "注意:條目名探測結果為undefined,這可能是程式錯誤導致,請考慮") + "<a href='/wiki/User_talk:%E9%AD%94%E7%90%B4'>" + han("回报", "回報") + "</a>。</span></p>"
regSecStr = '^(.|\n)*({' + '{ *DYKEntry(.|\n)*?\\| *article *= *' + article.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/ /g, '[ _]') + '(.|\n)*?(\n))=(.|\n)*$';
regSec = RegExp(regSecStr, '');
getQuestion = API.post({
action: 'parse',
page: dykn,
prop: 'wikitext',
formatversion: '2'
}).done(function (data) {
s0 = data.parse.wikitext
s1 = s0.replace(regSec, '$2')
if (s1 === s0) {
mw.notify('未找到条目', '未找到條目');
return;
}
const iA = s1.search(regArticleF);
const iT = s1.search(regTypeF);
if (iA !== -1 && (iA < iT || iT === -1)) {
first = 'article';
} else if (iT !== -1 && (iT < iA || iA === -1)) {
first = 'type';
} else {
mw.notify('Error: Comparing indices.');
}
type = s1.replace(regType, '$2');
html =
'<div id="dykc-edit-type-dialog" class="dykc-edit-type-dialog">' +
'<style>.dykc-edit-type-dialog,.dykc-edit-type-preview{font-size:initial;}</style>' +
'<p>' + han('修改类别:', '修改類別:') + '<span class="dykc-edit-type-dialog-article">' + atl(article) + '</span></p>' + undwarning +
'<textarea name="dykc-edit-type-cmt" id="dykc-edit-type-cmt" class="dykc-edit-type-cmt" rows=1 ></textarea>' +
'<p>' + han('编辑摘要:', '編輯摘要:') +
'<textarea name="dykc-edit-type-summary" id="dykc-edit-type-summary" class="dykc-edit-type-summary" rows=2 placeholder="可留空"></textarea>' +
'</div>';
if (dl) {
dl.html(html).closest('.ui-dialog-content').dialog('destroy').remove();
dl = null;
}
dl = $(html).dialog({
title: han('修改类别', '修改類別'),
minWidth: 600,
minHeight: 200,
buttons: [
{
text: han('检视', '檢視'),
click: function () {
processCmt(article);
postCmt(article, cmt, summary);
}
},
{
text: han('关闭', '關閉'),
click: function () {
processCmt(article);
$(this).dialog('close');
}
}
]
});
type = type.trim();
if (
type === 'person' ||
type === 'Person' ||
type === 'people' ||
type === 'People' ||
type === 'human' ||
type === 'Human' ||
type === 'man' ||
type === 'Man' ||
type === 'men' ||
type === 'Men'
) {
$('#dykc-edit-type-cmt').after($('<span>').css('font-size','small').css('color','#d00').text(han("自动转换自'","自動轉換自'") + type + "'"))
$('#dykc-edit-type-cmt').val('biography')
} else if (
type === 'woman' ||
type === 'Woman' ||
type === 'women' ||
type === 'Women'
) {
$('#dykc-edit-type-cmt').after($('<span>').css('font-size','small').css('color','#d00').text(han("自动转换自'","自動轉換自'") + type ))
$('#dykc-edit-type-cmt').val('female')
} else {
$('#dykc-edit-type-cmt').val(type)
}
$('#dykc-edit-type-dialog').parent().addClass('dykc-edit-type-dialog-base')
if (cj[article] !== undefined) {
$('#dykc-edit-type-dialog').find('.dykc-edit-type-cmt').val(cj[article]);
}
if (cjs[article] !== undefined) {
$('#dykc-edit-type-dialog').find('.dykc-edit-type-summary').val(cjs[article]);
}
}).fail(function (e) {
mw.notify('Fail: Opendl');
})
}
function processCmt(a) {
cmt = $('#dykc-edit-type-dialog').find('.dykc-edit-type-cmt').val().replace(/\n*$/, "");
summary = $('#dykc-edit-type-dialog').find('.dykc-edit-type-summary').val().replace(/\n*$/, "");
cj[a] = cmt;
cjs[a] = summary
}
function postCmt(a, c, summary) {
mw.notify(han('请求页面代码……','請求頁面代碼……'))
getQuestion = API.post({
action: 'parse',
page: dykn,
prop: 'wikitext',
formatversion: '2'
}).done(function (data) {
c0 = data.parse.wikitext;
var c1 = '';
mw.notify(han('处理类别参数……','處理類別參數……'))
if (first === 'article') {
regCmtStr = '(\\| *article *= *' + a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/ /g, '[ _]') + '(.|\n)*?type *= *)(.*?)\n';
regCmt = RegExp(regCmtStr, '');
c1 = c0.replace(regCmt, '$1' + c + '\n');
} else if (first === 'type') {
regArticle2Str = '.*article *= *' + a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/ /g, '[ _]');
regArticle2 = RegExp(regArticle2Str, 'm');
const idxArticle = c0.search(regArticle2);
// 2. 在 article 之前,用全局匹配把所有 type 都搜出来
const before = c0.slice(0, idxArticle);
const allTypes = [...before.matchAll(regTypeF)];
// 3. 取最后一个 match
const last = allTypes[allTypes.length - 1];
const { index: start, 0: matchedText } = last;
const end = start + matchedText.length;
// 4. 拼接出替换后的新字符串
c1 = c0.slice(0, start)
+ ' | type = ' + c
+ c0.slice(end);
}
if (c1 === c0) {
mw.notify('Failed to edit.');
enableButton();
return;
}
mw.notify(han('查询差异……','查詢差異……'))
var getDiff = API.post({
action: 'compare',
format: 'json',
fromtitle: dykn,
toslots: 'main',
prop: 'diff',
"totext-main": c1,
formatversion: '2'
}).done(function (data) {
var dld = null;
var htmld = data.compare.body
? '<div id="dykc-edit-type-diff-table" style="overflow-y:scroll"><p>' + han('目标:', '目標:') + '<span class="dykc-edit-type-diff-article">' + atl(a) + '</span></p><table class="diff"><colgroup><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content"></colgroup><tbody>' + data.compare.body + '</tbody></table></div>'
: '<div style="margin:20px;font-size:18px;text-align:center;" id="dykc-edit-type-diff-table">无差异</div>';
if (dld) {
dld.html(htmld).dialog("open");
return null;
}
if (data.compare.body) {
dld = $(htmld).dialog({
title: 'diff',
minWidth: 600,
minHeight: 100,
buttons: [{
text: "确定",
click: function () {
postEdit();
$(this).dialog('close');
}
},
{
text: "取消",
click: function () {
$(this).dialog('close');
}
}
]
});
} else {
dld = $(htmld).dialog({
title: 'diff',
minWidth: 600,
minHeight: 100,
buttons: [{
text: "关闭",
click: function () {
$(this).dialog('close');
}
}
]
});
}
})
function postEdit() {
mw.notify(han('正在发布,请稍候。', '正在發佈,請稍候。'))
$('.dykc-edit-type-dialog-base button').each(function () {
$(this).prop("disabled", true);
});
if (summary) {
summary = ':' + summary
}
postPromise = API.postWithToken('csrf', {
action: 'edit',
title: dykn,
text: c1,
summary: '/* ' + a + ' */ Edit type of [[' + a + ']] via [[User:魔琴/gadgets/dykc-edit-type|DET]]' + summary
}).then(function () {
mw.notify('修改成功')
enableButton();
dl.html(html).dialog("close");
window.open(script + '?title=' + dykn + '&diff=cur&oldid=prev&diffonly=1')
}, function (e) {
mw.notify(e);
enableButton();
});
}
}).fail(function (e) {
mw.notify(e);
enableButton();
})
}
function enableButton() {
$('.dykc-edit-type-dialog-base button').each(function () {
$(this).prop("disabled", false);
});
}
// Process nomination page
function processNom() {
$('<style>.mw-heading4 .mw-editsection-divider { display: inline !important; }</style>').appendTo('head');
if ($('.mw-editsection').length > 0) {
var edtyTypeBtn = $('<span>').append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('dykc-edit-type-button').attr('href', 'javascript:void(0)').text(han('修改类别', '修改類別')));
$(".mw-heading4 .mw-editsection").each(function () {
var _edtyTypeBtn = edtyTypeBtn.clone();
$(this).find('.mw-editsection-bracket').last().before(_edtyTypeBtn);
});
}
$('.dykc-edit-type-button').click(function () {
opendl(findArticle($(this)));
})
}
function findArticle(that) {
closesttag = that.closest('.mw-heading4').next().prop("tagName")
switch (closesttag) {
case "SPAN":
return that.closest('.mw-heading4').next().next().next().find('.dykarticle a').html();
break;
case "FIGURE":
return that.closest('.mw-heading4').next().next().find('.dykarticle a').html();
break;
case "UL":
default:
return that.closest('.mw-heading4').next().find('.dykarticle a').html();
break;
}
}
function atu(ar) {
return articlepath.replace('$1', encodeURI(ar)) + "?yikeditor=1&dykarticle=" + encodeURI(ar);
}
function atl(ar) {
return "<a href='" + atu(ar) + "' title='" + ar + "'>" + ar + "</a>";
}
});
//</nowiki>