MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 记录最近选区并延迟显示 标签:清空 |
Maintenance script(留言 | 贡献) 为未加载 CSS 提供可见性兜底 |
||
| (未显示同一用户的2个中间版本) | |||
| 第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'); |
|||
} |
|||
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 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(); |
|||
}); |
|||
}); |
|||
2025年12月27日 (六) 12:54的最新版本
/* ================================================================
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');
}
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 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();
});
});