MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 确保 DOM 就绪后初始化 |
Maintenance script(留言 | 贡献) 增加轮询兜底以兼容未触发 selectionchange 的情况 |
||
| 第22行: | 第22行: | ||
var rafId = null; |
var rafId = null; |
||
var pollTimer = null; |
|||
var lastText = null; |
|||
function getSelectionText() { |
function getSelectionText() { |
||
| 第35行: | 第37行: | ||
var active = document.activeElement; |
var active = document.activeElement; |
||
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) { |
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) { |
||
if (lastText !== null) { |
|||
lastText = null; |
|||
$counter.hide(); |
|||
} |
|||
return; |
return; |
||
} |
} |
||
| 第41行: | 第46行: | ||
var text = getSelectionText(); |
var text = getSelectionText(); |
||
if (!text) { |
if (!text) { |
||
if (lastText !== null) { |
|||
lastText = null; |
|||
$counter.hide(); |
|||
} |
|||
return; |
return; |
||
} |
} |
||
if (text === lastText) { |
|||
return; |
|||
} |
|||
lastText = text; |
|||
var count = Array.from(text).length; |
var count = Array.from(text).length; |
||
$counter.text('已选 ' + count + ' 字').show(); |
$counter.text('已选 ' + count + ' 字').show(); |
||
| 第54行: | 第67行: | ||
} |
} |
||
rafId = window.requestAnimationFrame(updateCounter); |
rafId = window.requestAnimationFrame(updateCounter); |
||
} |
|||
function startPolling() { |
|||
if (pollTimer) { |
|||
return; |
|||
} |
|||
pollTimer = window.setInterval(updateCounter, 500); |
|||
} |
|||
function stopPolling() { |
|||
if (!pollTimer) { |
|||
return; |
|||
} |
|||
window.clearInterval(pollTimer); |
|||
pollTimer = null; |
|||
} |
} |
||
| 第60行: | 第88行: | ||
document.addEventListener('keyup', scheduleUpdate); |
document.addEventListener('keyup', scheduleUpdate); |
||
document.addEventListener('touchend', scheduleUpdate, { passive: true }); |
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(); |
|||
}); |
}); |
||
}); |
}); |
||
2025年12月27日 (六) 12:42的版本
/* ================================================================
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();
});
});