MediaWiki:Gadget-Wordcount.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function($) {
function bytecount(text) {
text = text.replace(/[\u0000-\u007F]/g, '.');
text = text.replace(/[\u0080-\u07FF\uD800-\uDFFF]/g, '..');
text = text.replace(/[\u0800-\uD7FF\uE000-\uFFFF]/g, '...');
return text.length;
};
function cjkcount(text) {
text = text.replace(/\./g, '');
text = text.replace(/[\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u3005\u3007\u3021-\u3029\u3038-\u303B\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE50]|\uD87E[\uDC00-\uDE1F]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF|\uD888[\uDC00-\uDFAF]/g, '.');
text = text.replace(/[^\.]/g, '');
return text.length;
};
function getwcbytext(text) {
return text.length + ' character(s) (' + cjkcount(text) + ' CJK)<br />' +
bytecount(text) + ' byte(s) in <a href="' + mw.config.get('wgScript') + '?title=UTF-8">UTF-8</a> encoding';
};
function getsel() {
if (!window.getSelection) return '';
return getSelection().toString();
};
function dowc(event) {
$('.wordcount').remove(); // or remove after text.length == 0 checking?
var text = getsel();
if (text.length == 0) return;
var divj = $('<div />').html(getwcbytext(text))
.css({
'position': 'fixed',
'right': '0',
'bottom': '0',
'margin': '4px',
'padding': '6px'
})
.addClass('wordcount ui-state-highlight ui-corner-all')
.appendTo('body');
// we hook keyup, so this may make it flickering
// eg when shift, ctrl.etc key up
//.hide().fadeIn('slow');
setTimeout(function() {
divj.fadeOut('slow');
}, 5000);
};
$(document).mouseup(dowc).keyup(dowc);
})(jQuery);