首页
随机
附近
登录
设置
关于陋室
免责声明
陋室
搜索
查看“︁MediaWiki:Gadget-SelectionCounter.js”︁的源代码
←
MediaWiki:Gadget-SelectionCounter.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于这些用户组的用户执行:
用户
、
自动确认用户
本页面提供本维基软件的界面文本,并受到保护以防止滥用。 要添加或更改所有维基的翻译,请使用MediaWiki的本地化项目
translatewiki.net
。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有网站访客。
您可以查看和复制此页面的源代码。
/* ================================================================ 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.isCollapsed) { return ''; } return String(selection).trim(); } function updateCounter() { rafId = null; var active = document.activeElement; if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) { if (lastText !== null) { lastText = null; $counter.hide(); } return; } var text = getSelectionText(); if (!text) { if (lastText !== null) { lastText = null; $counter.hide(); } return; } if (text === lastText) { return; } lastText = text; var count = Array.from(text).length; $counter.text('已选 ' + count + ' 字').show(); } function scheduleUpdate() { if (rafId) { return; } rafId = window.requestAnimationFrame(updateCounter); } 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', scheduleUpdate); document.addEventListener('keyup', scheduleUpdate); document.addEventListener('touchend', scheduleUpdate, { passive: true }); window.addEventListener('focus', startPolling); window.addEventListener('blur', stopPolling); document.addEventListener('visibilitychange', function () { if (document.hidden) { stopPolling(); return; } startPolling(); }); startPolling(); }); });
返回
MediaWiki:Gadget-SelectionCounter.js
。