MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 更健壮的选区获取与事件触发 |
Maintenance script(留言 | 贡献) 为未加载 CSS 提供可见性兜底 |
||
| (未显示同一用户的3个中间版本) | |||
| 第20行: | 第20行: | ||
.appendTo('body'); |
.appendTo('body'); |
||
} |
} |
||
function ensureVisibleStyles() { |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
var computed = window.getComputedStyle(el); |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
$counter.css({ |
|||
position: 'fixed', |
|||
bottom: '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() { |
||
| 第31行: | 第61行: | ||
} |
} |
||
return selection.toString(); |
return selection.toString(); |
||
} |
|||
function compactText(text) { |
|||
return text.replace(/\s+/g, ''); |
|||
} |
|||
function showCount(text) { |
|||
| ⚫ | |||
if (!compact) { |
|||
return false; |
|||
} |
|||
var count = Array.from(compact).length; |
|||
| ⚫ | |||
return true; |
|||
} |
} |
||
function updateCounter() { |
function updateCounter() { |
||
rafId = null; |
rafId = null; |
||
var text = getSelectionText(); |
var text = getSelectionText(); |
||
if ( |
if (text) { |
||
lastText = text; |
|||
lastTime = Date.now(); |
|||
showCount(text); |
|||
| ⚫ | |||
return; |
return; |
||
} |
} |
||
if (lastText && Date.now() - lastTime < 1200) { |
|||
showCount(lastText); |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
return; |
return; |
||
} |
} |
||
$counter.hide(); |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
} |
} |
||
| 第89行: | 第121行: | ||
} |
} |
||
function onSelectMove() { |
|||
| ⚫ | |||
if (!isSelecting) { |
|||
| ⚫ | |||
return; |
|||
| ⚫ | |||
} |
|||
scheduleUpdate(); |
|||
} |
|||
| ⚫ | |||
var text = getSelectionText(); |
|||
if (text) { |
|||
lastText = text; |
|||
lastTime = Date.now(); |
|||
} |
|||
scheduleUpdate(); |
|||
}); |
|||
document.addEventListener('mousedown', function () { |
|||
isSelecting = true; |
|||
}, true); |
|||
| ⚫ | |||
isSelecting = false; |
|||
scheduleUpdateDelayed(); |
|||
}, true); |
|||
| ⚫ | |||
| ⚫ | |||
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); |
||