打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Gadget-SelectionCounter.js

MediaWiki界面页面
可打印版本不再受到支持且可能有渲染错误。请更新您的浏览器书签并改用浏览器默认打印功能。

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/* ================================================================
   MediaWiki Gadget: SelectionCounter
   功能:
   - 选中文本时显示字数统计
   - 与 RightToolbar 风格一致
   ================================================================= */

mw.loader.using(['mediawiki.util']).then(function () {
    if (window.SelectionCounterLoaded) {
        return;
    }
    window.SelectionCounterLoaded = true;

    $(function () {
        var $counter = $('#mw-selection-counter');

        if (!$counter.length) {
            $counter = $('<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>')
                .hide()
                .appendTo('body');
        }

        function ensureVisibleStyles() {
            var el = $counter[0];
            if (!el) {
                return;
            }
            var computed = window.getComputedStyle(el);
            if (computed.position !== 'static') {
                return;
            }
            $counter.css({
                position: 'fixed',
                bottom: '24px',
                right: '24px',
                padding: '6px 10px',
                fontSize: '12px',
                borderRadius: '4px',
                background: 'var(--color-surface-0, rgba(255, 255, 255, 0.85))',
                border: '1px solid var(--border-color-base, #aaa)',
                color: 'var(--color-base, #333)',
                boxShadow: '0 1px 3px rgba(0, 0, 0, 0.2)',
                backdropFilter: 'blur(4px)',
                zIndex: 2147483647,
                pointerEvents: 'none'
            });
        }

        ensureVisibleStyles();

        var rafId = null;
        var pollTimer = null;
        var lastText = '';
        var lastTime = 0;
        var isSelecting = false;

        function getSelectionText() {
            var selection = window.getSelection();
            if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
                return '';
            }
            return selection.toString();
        }

        function compactText(text) {
            return text.replace(/\s+/g, '');
        }

        function showCount(text) {
            var compact = compactText(text);
            if (!compact) {
                return false;
            }
            var count = Array.from(compact).length;
            $counter.text('已选 ' + count + ' 字').show();
            return true;
        }

        function updateCounter() {
            rafId = null;
            var text = getSelectionText();
            if (text) {
                lastText = text;
                lastTime = Date.now();
                showCount(text);
                return;
            }

            if (lastText && Date.now() - lastTime < 1200) {
                showCount(lastText);
                return;
            }

            $counter.hide();
        }

        function scheduleUpdate() {
            if (rafId) {
                return;
            }
            rafId = window.requestAnimationFrame(updateCounter);
        }

        function scheduleUpdateDelayed() {
            window.setTimeout(scheduleUpdate, 0);
        }

        function startPolling() {
            if (pollTimer) {
                return;
            }
            pollTimer = window.setInterval(updateCounter, 500);
        }

        function stopPolling() {
            if (!pollTimer) {
                return;
            }
            window.clearInterval(pollTimer);
            pollTimer = null;
        }

        function onSelectMove() {
            if (!isSelecting) {
                return;
            }
            scheduleUpdate();
        }

        document.addEventListener('selectionchange', function () {
            var text = getSelectionText();
            if (text) {
                lastText = text;
                lastTime = Date.now();
            }
            scheduleUpdate();
        });
        document.addEventListener('mousedown', function () {
            isSelecting = true;
        }, true);
        document.addEventListener('mouseup', function () {
            isSelecting = false;
            scheduleUpdateDelayed();
        }, true);
        document.addEventListener('mousemove', onSelectMove, true);
        document.addEventListener('touchstart', function () {
            isSelecting = true;
        }, { passive: true, capture: true });
        document.addEventListener('touchmove', onSelectMove, { passive: true, capture: true });
        document.addEventListener('touchend', function () {
            isSelecting = false;
            scheduleUpdateDelayed();
        }, { passive: true, capture: true });
        document.addEventListener('keyup', scheduleUpdate);
        window.addEventListener('focus', startPolling);
        window.addEventListener('blur', stopPolling);
        document.addEventListener('visibilitychange', function () {
            if (document.hidden) {
                stopPolling();
                return;
            }
            startPolling();
        });

        startPolling();
    });
});