User:Vesekskiy/TitleConv.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function () {
if (mw.config.get('wgAction') !== 'view') return;
const variants = [
'zh',
'zh-hans','zh-cn','zh-my','zh-sg',
'zh-hant','zh-hk','zh-mo','zh-tw'
];
const defaultChecked = ['zh-cn', 'zh-hk', 'zh-tw'];
function getTitles() {
const api = new mw.Api();
const page = mw.config.get('wgPageName');
const result = {};
return Promise.all(
variants.map(v =>
api.get({
action: 'parse',
page: page,
prop: 'displaytitle',
variant: v,
format: 'json'
}).then(data => {
result[v] = $('<div>')
.html(data.parse.displaytitle)
.text();
})
)
).then(() => result);
}
function stripDisambig(title) {
return title.replace(/ \([^()]+\)$/, '');
}
function stripNamespace(title) {
return title.replace(/^[^:]+:/, '');
}
function buildDialog(titles) {
const rawPageTitle = mw.config.get('wgPageName');
const pageTitle = rawPageTitle.replace(/_/g, ' ');
const maxLen = Math.max(...variants.map(v => v.length));
let html = `
<div style="padding:10px;line-height:1.9">
<style>
.variant-code {
font-family: monospace;
display: inline-block;
min-width: ${maxLen + 1}ch;
}
</style>
`;
function row(v) {
const checked = defaultChecked.includes(v) ? 'checked' : '';
return `
<label>
<input type="checkbox" value="${v}" ${checked}>
<span class="variant-code">${v}</span>
</label>
${titles[v]}
<br>
`;
}
html += row('zh') + '<br>';
html += row('zh-hans');
html += row('zh-cn');
html += row('zh-my');
html += row('zh-sg') + '<br>';
html += row('zh-hant');
html += row('zh-hk');
html += row('zh-mo');
html += row('zh-tw') + '<br><br>';
html += `
<label><input type="radio" name="expand" value="rule"> ……</label><br>
<label><input type="radio" name="expand" value="conv" checked> -{……}-</label><br>
<label><input type="radio" name="expand" value="link"> [[……|-{……}-]]</label><br>
<label><input type="radio" name="expand" value="pipe"> ……{{!}}-{……}-</label>
`;
const hasDisambig = variants.some(v => {
const t = titles[v];
return typeof t === 'string' && / \([^()]+\)$/.test(t);
});
const hasNamespace = mw.config.get('wgNamespaceNumber') !== 0;
html += `<br><br>
<label style="${hasDisambig ? '' : 'color: var(--disabled-color, #72777d);'}">
<input type="checkbox" id="removeDisambig" ${hasDisambig ? '' : 'disabled'}>
去除消歧义括号
</label><br>
<label style="${hasNamespace ? '' : 'color: var(--disabled-color, #72777d);'}">
<input type="checkbox" id="removeNamespace" ${hasNamespace ? '' : 'disabled'}>
去除命名空间前缀
</label>
`;
html += '</div>';
$('<div>')
.html(html)
.dialog({
title: '生成标题转换规则',
modal: true,
width: 460,
buttons: {
'生成': function () {
let selected = [];
$(this).find('input[type=checkbox]').each(function () {
if (this.checked && variants.includes(this.value)) {
selected.push(this.value);
}
});
if (!selected.length) return;
const removeDisambig = $(this).find('#removeDisambig').prop('checked');
const removeNamespace = $(this).find('#removeNamespace').prop('checked');
function process(title) {
if (removeNamespace) {
title = stripNamespace(title);
}
if (removeDisambig) {
title = stripDisambig(title);
}
return title;
}
let core = '';
selected.forEach(v => {
core += v + ':' + process(titles[v]) + ';';
});
const mode = $(this).find('input[name=expand]:checked').val();
let output = '';
switch (mode) {
case 'rule':
output = core;
break;
case 'conv':
output = '-{' + core + '}-';
break;
case 'link':
output = '[[' + pageTitle + '|-{' + core + '}-]]';
break;
case 'pipe':
output = pageTitle + '{{!}}-{' + core + '}-';
break;
}
navigator.clipboard.writeText(output);
mw.notify('已复制到剪贴板');
$(this).dialog('close');
},
'取消': function () {
$(this).dialog('close');
}
}
});
}
function init() {
getTitles().then(buildDialog);
}
mw.util.addPortletLink(
'p-cactions',
'#',
'标题转换规则',
'ca-title-conv',
'生成标题转换规则'
).addEventListener('click', function (e) {
e.preventDefault();
init();
});
})();