MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 增加轮询兜底以兼容未触发 selectionchange 的情况 |
Maintenance script(留言 | 贡献) 更健壮的选区获取与事件触发 |
||
| 第27行: | 第27行: | ||
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 updateCounter() { |
function updateCounter() { |
||
rafId = null; |
rafId = null; |
||
var |
var text = getSelectionText(); |
||
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) { |
|||
if (!text) { |
|||
if (lastText !== null) { |
if (lastText !== null) { |
||
lastText = null; |
lastText = null; |
||
| 第44行: | 第45行: | ||
} |
} |
||
var |
var compact = text.replace(/\s+/g, ''); |
||
if (! |
if (!compact) { |
||
if (lastText !== null) { |
if (lastText !== null) { |
||
lastText = null; |
lastText = null; |
||
| 第58行: | 第59行: | ||
lastText = text; |
lastText = text; |
||
var count = Array.from( |
var count = Array.from(compact).length; |
||
$counter.text('已选 ' + count + ' 字').show(); |
$counter.text('已选 ' + count + ' 字').show(); |
||
} |
} |
||
| 第67行: | 第68行: | ||
} |
} |
||
rafId = window.requestAnimationFrame(updateCounter); |
rafId = window.requestAnimationFrame(updateCounter); |
||
} |
|||
function scheduleUpdateDelayed() { |
|||
window.setTimeout(scheduleUpdate, 0); |
|||
} |
} |
||
| 第85行: | 第90行: | ||
document.addEventListener('selectionchange', scheduleUpdate); |
document.addEventListener('selectionchange', scheduleUpdate); |
||
document.addEventListener('mouseup', |
document.addEventListener('mouseup', scheduleUpdateDelayed, true); |
||
document.addEventListener('mouseup', scheduleUpdateDelayed); |
|||
document.addEventListener('keyup', scheduleUpdate); |
document.addEventListener('keyup', scheduleUpdate); |
||
document.addEventListener('touchend', |
document.addEventListener('touchend', scheduleUpdateDelayed, { passive: true, capture: true }); |
||
document.addEventListener('pointerup', scheduleUpdateDelayed, true); |
|||
window.addEventListener('focus', startPolling); |
window.addEventListener('focus', startPolling); |
||
window.addEventListener('blur', stopPolling); |
window.addEventListener('blur', stopPolling); |
||