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

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

MediaWiki界面页面
删除的内容 添加的内容
Maintenance script留言 | 贡献
增加触发事件并增强选区检测
Maintenance script留言 | 贡献
确保 DOM 就绪后初始化
第7行: 第7行:


mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using(['mediawiki.util']).then(function () {
if (window.SelectionCounterLoaded) {
var $counter = $('#mw-selection-counter');
return;

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


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


if (!$counter.length) {
function getSelectionText() {
$counter = $('<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>')
var selection = window.getSelection();
if (!selection || selection.isCollapsed) {
.hide()
return '';
.appendTo('body');
}
}
return String(selection).trim();
}


var rafId = null;
function updateCounter() {
rafId = null;
var active = document.activeElement;
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
$counter.hide();
return;
}


var text = getSelectionText();
function getSelectionText() {
var selection = window.getSelection();
if (!text) {
$counter.hide();
if (!selection || selection.isCollapsed) {
return;
return '';
}
return String(selection).trim();
}
}


var count = Array.from(text).length;
function updateCounter() {
$counter.text('已选 ' + count + ' 字').show();
rafId = null;
var active = document.activeElement;
}
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
$counter.hide();
return;
}


var text = getSelectionText();
function scheduleUpdate() {
if (rafId) {
if (!text) {
return;
$counter.hide();
return;
}

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

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


document.addEventListener('selectionchange', scheduleUpdate);
document.addEventListener('selectionchange', scheduleUpdate);
document.addEventListener('mouseup', scheduleUpdate);
document.addEventListener('mouseup', scheduleUpdate);
document.addEventListener('keyup', scheduleUpdate);
document.addEventListener('keyup', scheduleUpdate);
document.addEventListener('touchend', scheduleUpdate, { passive: true });
document.addEventListener('touchend', scheduleUpdate, { passive: true });
});
});
});

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

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

        function getSelectionText() {
            var selection = window.getSelection();
            if (!selection || selection.isCollapsed) {
                return '';
            }
            return String(selection).trim();
        }

        function updateCounter() {
            rafId = null;
            var active = document.activeElement;
            if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
                $counter.hide();
                return;
            }

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

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

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

        document.addEventListener('selectionchange', scheduleUpdate);
        document.addEventListener('mouseup', scheduleUpdate);
        document.addEventListener('keyup', scheduleUpdate);
        document.addEventListener('touchend', scheduleUpdate, { passive: true });
    });
});