User:Liangent/Scripts/DispConv.js
< User:Liangent | Scripts
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
require(['jquery', 'jqueryui', 'mediawiki'], function() { (function($) {
if (wgNamespaceNumber < 0 || wgArticleId == 0) return;
$(function() {
$('#p-tb ul').append('<li id="t-dispconv">' +
'<a title="显示在这个页面上定义的字词转换" href="#">显示字词转换</a></li>'
).find('#t-dispconv a').click(function(event) {
event.preventDefault();
var dialog = $('<div title="本页上的字词转换" />').dialog({
resizable: false
});
dialog.text('正在读取源代码');
MediaWiki._request({
action: 'query',
prop: 'revisions',
titles: wgPageName,
rvprop: 'content',
rvexpandtemplates: ''
}, function(xml) {
var text = $('api>query>pages>page>revisions>rev', xml).text();
// from pywikibot: textlib.py
$.each([/<!--.*?-->/g, /<includeonly>.*?<\/includeonly>/g, /<nowiki>.*?<\/nowiki>/g,
/<pre>.*?<\/pre>/g, /<source .*?<\/source>/g], function() {
text = text.replace(this, '');
});
var convlist = text.match(/-\{.*?\}-/g);
var conv = {};
var pushconv = function(k, v) {
if (conv[k] === undefined) {
conv[k] = [v];
} else {
conv[k].push(v);
}
};
// from http://www.martienus.com/code/javascript-remove-duplicates-from-array.html
var unique = function(a) {
var r = new Array();
o: for(var i = 0, n = a.length; i < n; i++) {
for(var x = i + 1; x < n; x++) {
if(a[x]==a[i]) {
continue o;
}
}
r[r.length] = a[i];
}
return r;
};
if (convlist == null) {
dialog.text('没有字词转换');
} else {
$.each(convlist, function() {
var code = this.slice(2, -2);
if (code.length >= 2 && code[1] == '|') {
pushconv(code[0], code.slice(2));
} else {
pushconv('', code);
}
});
var toparse = '';
for (var k in conv) {
// display A, H and - only
if (k != 'A' && k != 'H' && k != '-') {
continue;
}
toparse += '以下是使用代码[' + k + ']的字词转换:\n';
$.each(unique(conv[k]), function() {
toparse += '*-{D|' + this + '}- 显示为:-{' + this + '}-\n';
});
}
if (toparse.length == 0) {
dialog.text('没有影响全文的字词转换');
} else {
dialog.text('正在解析');
MediaWiki._request({
action: 'parse',
title: wgPageName.replace(/_/g, ' '),
text: toparse,
prop: 'text',
variant: wgUserVariant
}, function(xml) {
dialog.html($('api>parse>text', xml).text())
.css({ 'max-height': 480, 'overflow': 'auto', 'font-size': '10px' })
.dialog('option', 'height', 'auto').dialog('option', 'width', 'auto')
.dialog('option', 'position', ['center', 'center']);
}, function() {
dialog.text('解析错误');
});
}
}
}, function() {
dialog.text('读取错误');
});
});
});
})(jQuery); });