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

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

MediaWiki界面页面
删除的内容 添加的内容
Maintenance script留言 | 贡献
增加轮询兜底以兼容未触发 selectionchange 的情况
Maintenance script留言 | 贡献
为未加载 CSS 提供可见性兜底
 
(未显示同一用户的4个中间版本)
第20行: 第20行:
.appendTo('body');
.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 rafId = null;
var pollTimer = null;
var pollTimer = null;
var lastText = null;
var lastText = '';
var lastTime = 0;
var isSelecting = false;


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 compactText(text) {
rafId = null;
return text.replace(/\s+/g, '');
}
var active = document.activeElement;

if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
if (lastText !== null) {
function showCount(text) {
lastText = null;
var compact = compactText(text);
$counter.hide();
if (!compact) {
}
return false;
return;
}
}
var count = Array.from(compact).length;
$counter.text('已选 ' + count + ' 字').show();
return true;
}


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


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


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


第67行: 第100行:
}
}
rafId = window.requestAnimationFrame(updateCounter);
rafId = window.requestAnimationFrame(updateCounter);
}

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


第84行: 第121行:
}
}


function onSelectMove() {
document.addEventListener('selectionchange', scheduleUpdate);
if (!isSelecting) {
document.addEventListener('mouseup', scheduleUpdate);
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);
document.addEventListener('keyup', scheduleUpdate);
document.addEventListener('touchend', scheduleUpdate, { passive: true });
window.addEventListener('focus', startPolling);
window.addEventListener('focus', startPolling);
window.addEventListener('blur', stopPolling);
window.addEventListener('blur', stopPolling);