跳转到内容

MediaWiki:Gadget-Wordcount.js

计算器百科,非营利的计算器专业知识百科。
春上冰月留言 | 贡献2016年4月16日 (六) 12:21的版本 (debug)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
    (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(/[㐀-\u4dbe一-\u9ffe豈-\ufafe]|[\ud840-\ud868\ud86a-\ud86c][\udc00-\udfff]|\ud869[\udc00-\udede\udf00-\udfff]|\ud86d[\udc00-\udf3e\udf40-\udfff]|\ud86e[\udc00-\udc1e]/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="' + 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',
                    'border': '1px solid #f9dd34',
                    'background': '#ffef8f',
                    'border-radius': '3px'
                })
                .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);