MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 记录最近选区并延迟显示 |
Maintenance script(留言 | 贡献) 拖选时实时更新并增强触发 |
||
| 第25行: | 第25行: | ||
var lastText = ''; |
var lastText = ''; |
||
var lastTime = 0; |
var lastTime = 0; |
||
var isSelecting = false; |
|||
function getSelectionText() { |
function getSelectionText() { |
||
| 第90行: | 第91行: | ||
window.clearInterval(pollTimer); |
window.clearInterval(pollTimer); |
||
pollTimer = null; |
pollTimer = null; |
||
} |
|||
function onSelectMove() { |
|||
if (!isSelecting) { |
|||
return; |
|||
} |
|||
scheduleUpdate(); |
|||
} |
} |
||
| 第101行: | 第109行: | ||
}); |
}); |
||
document.addEventListener('mousedown', function () { |
document.addEventListener('mousedown', function () { |
||
isSelecting = true; |
|||
}, true); |
|||
| ⚫ | |||
isSelecting = false; |
|||
scheduleUpdateDelayed(); |
|||
}, true); |
}, true); |
||
document.addEventListener(' |
document.addEventListener('mousemove', onSelectMove, true); |
||
document.addEventListener(' |
document.addEventListener('touchstart', function () { |
||
isSelecting = 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); |
||
| ⚫ | |||
| ⚫ | |||
window.addEventListener('focus', startPolling); |
window.addEventListener('focus', startPolling); |
||
window.addEventListener('blur', stopPolling); |
window.addEventListener('blur', stopPolling); |
||
2025年12月27日 (六) 12:51的版本
/* ================================================================
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 = '';
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();
});
});