MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 记录最近选区并延迟显示 标签:清空 |
Maintenance script(留言 | 贡献) 记录最近选区并延迟显示 |
||
| 第1行: | 第1行: | ||
/* ================================================================ |
|||
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; |
|||
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; |
|||
} |
|||
document.addEventListener('selectionchange', function () { |
|||
var text = getSelectionText(); |
|||
if (text) { |
|||
lastText = text; |
|||
lastTime = Date.now(); |
|||
} |
|||
scheduleUpdate(); |
|||
}); |
|||
document.addEventListener('mousedown', function () { |
|||
$counter.hide(); |
|||
}, true); |
|||
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(); |
|||
}); |
|||
}); |
|||
2025年12月27日 (六) 12:48的版本
/* ================================================================
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;
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;
}
document.addEventListener('selectionchange', function () {
var text = getSelectionText();
if (text) {
lastText = text;
lastTime = Date.now();
}
scheduleUpdate();
});
document.addEventListener('mousedown', function () {
$counter.hide();
}, true);
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();
});
});