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

MediaWiki:Gadget-SelectionCounter.js:修订间差异

MediaWiki界面页面
删除的内容 添加的内容
Maintenance script留言 | 贡献
增加轮询兜底以兼容未触发 selectionchange 的情况
Maintenance script留言 | 贡献
更健壮的选区获取与事件触发
第27行: 第27行:
function getSelectionText() {
function getSelectionText() {
var selection = window.getSelection();
var selection = window.getSelection();
if (!selection || selection.isCollapsed) {
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
return '';
return '';
}
}
return String(selection).trim();
return selection.toString();
}
}


function updateCounter() {
function updateCounter() {
rafId = null;
rafId = null;

var active = document.activeElement;
var text = getSelectionText();
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
if (!text) {
if (lastText !== null) {
if (lastText !== null) {
lastText = null;
lastText = null;
第44行: 第45行:
}
}


var text = getSelectionText();
var compact = text.replace(/\s+/g, '');
if (!text) {
if (!compact) {
if (lastText !== null) {
if (lastText !== null) {
lastText = null;
lastText = null;
第58行: 第59行:


lastText = text;
lastText = text;
var count = Array.from(text).length;
var count = Array.from(compact).length;
$counter.text('已选 ' + count + ' 字').show();
$counter.text('已选 ' + count + ' 字').show();
}
}
第67行: 第68行:
}
}
rafId = window.requestAnimationFrame(updateCounter);
rafId = window.requestAnimationFrame(updateCounter);
}

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


第85行: 第90行:


document.addEventListener('selectionchange', scheduleUpdate);
document.addEventListener('selectionchange', scheduleUpdate);
document.addEventListener('mouseup', scheduleUpdate);
document.addEventListener('mouseup', scheduleUpdateDelayed, true);
document.addEventListener('mouseup', scheduleUpdateDelayed);
document.addEventListener('keyup', scheduleUpdate);
document.addEventListener('keyup', scheduleUpdate);
document.addEventListener('touchend', scheduleUpdate, { passive: true });
document.addEventListener('touchend', scheduleUpdateDelayed, { passive: true, capture: true });
document.addEventListener('pointerup', scheduleUpdateDelayed, true);
window.addEventListener('focus', startPolling);
window.addEventListener('focus', startPolling);
window.addEventListener('blur', stopPolling);
window.addEventListener('blur', stopPolling);

2025年12月27日 (六) 12:45的版本

/* ================================================================
   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');
        }

        var rafId = null;
        var pollTimer = null;
        var lastText = null;

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

        function updateCounter() {
            rafId = null;

            var text = getSelectionText();
            if (!text) {
                if (lastText !== null) {
                    lastText = null;
                    $counter.hide();
                }
                return;
            }

            var compact = text.replace(/\s+/g, '');
            if (!compact) {
                if (lastText !== null) {
                    lastText = null;
                    $counter.hide();
                }
                return;
            }

            if (text === lastText) {
                return;
            }

            lastText = text;
            var count = Array.from(compact).length;
            $counter.text('已选 ' + count + ' 字').show();
        }

        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;
        }

        document.addEventListener('selectionchange', scheduleUpdate);
        document.addEventListener('mouseup', scheduleUpdateDelayed, true);
        document.addEventListener('mouseup', scheduleUpdateDelayed);
        document.addEventListener('keyup', scheduleUpdate);
        document.addEventListener('touchend', scheduleUpdateDelayed, { passive: true, capture: true });
        document.addEventListener('pointerup', scheduleUpdateDelayed, true);
        window.addEventListener('focus', startPolling);
        window.addEventListener('blur', stopPolling);
        document.addEventListener('visibilitychange', function () {
            if (document.hidden) {
                stopPolling();
                return;
            }
            startPolling();
        });

        startPolling();
    });
});