MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 增加轮询兜底以兼容未触发 selectionchange 的情况 |
Maintenance script(留言 | 贡献) 为未加载 CSS 提供可见性兜底 |
||
| (未显示同一用户的4个中间版本) | |||
| 第20行: | 第20行: | ||
.appendTo('body'); |
.appendTo('body'); |
||
} |
} |
||
function ensureVisibleStyles() { |
|||
| ⚫ | |||
| ⚫ | |||
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 = |
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 |
return selection.toString(); |
||
} |
} |
||
function |
function compactText(text) { |
||
return text.replace(/\s+/g, ''); |
|||
} |
|||
var active = document.activeElement; |
|||
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) { |
|||
function showCount(text) { |
|||
var compact = compactText(text); |
|||
if (!compact) { |
|||
return false; |
|||
| ⚫ | |||
} |
} |
||
| ⚫ | |||
| ⚫ | |||
return true; |
|||
} |
|||
function updateCounter() { |
|||
rafId = null; |
|||
var text = getSelectionText(); |
var text = getSelectionText(); |
||
if ( |
if (text) { |
||
lastText = text; |
|||
lastTime = Date.now(); |
|||
showCount(text); |
|||
| ⚫ | |||
return; |
return; |
||
} |
} |
||
if ( |
if (lastText && Date.now() - lastTime < 1200) { |
||
showCount(lastText); |
|||
return; |
return; |
||
} |
} |
||
$counter.hide(); |
|||
| ⚫ | |||
| ⚫ | |||
} |
} |
||
| 第67行: | 第100行: | ||
} |
} |
||
rafId = window.requestAnimationFrame(updateCounter); |
rafId = window.requestAnimationFrame(updateCounter); |
||
} |
|||
function scheduleUpdateDelayed() { |
|||
window.setTimeout(scheduleUpdate, 0); |
|||
} |
} |
||
| 第84行: | 第121行: | ||
} |
} |
||
function onSelectMove() { |
|||
| ⚫ | |||
if (!isSelecting) { |
|||
| ⚫ | |||
return; |
|||
} |
|||
scheduleUpdate(); |
|||
} |
|||
| ⚫ | |||
| ⚫ | |||
if (text) { |
|||
lastText = text; |
|||
lastTime = Date.now(); |
|||
} |
|||
scheduleUpdate(); |
|||
}); |
|||
document.addEventListener('mousedown', function () { |
|||
isSelecting = true; |
|||
}, true); |
|||
| ⚫ | |||
isSelecting = false; |
|||
scheduleUpdateDelayed(); |
|||
}, 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); |
||
| ⚫ | |||
window.addEventListener('focus', startPolling); |
window.addEventListener('focus', startPolling); |
||
window.addEventListener('blur', stopPolling); |
window.addEventListener('blur', stopPolling); |
||