MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 补齐脚本 |
Maintenance script(留言 | 贡献) 增加触发事件并增强选区检测 |
||
| 第16行: | 第16行: | ||
var rafId = null; |
var rafId = null; |
||
function getSelectionText() { |
|||
var selection = window.getSelection(); |
|||
if (!selection || selection.isCollapsed) { |
|||
return ''; |
|||
} |
|||
return String(selection).trim(); |
|||
} |
|||
function updateCounter() { |
function updateCounter() { |
||
| 第25行: | 第33行: | ||
} |
} |
||
var text = |
var text = getSelectionText(); |
||
if (!text) { |
if (!text) { |
||
$counter.hide(); |
$counter.hide(); |
||
| 第35行: | 第43行: | ||
} |
} |
||
function scheduleUpdate() { |
|||
if (rafId) { |
if (rafId) { |
||
return; |
return; |
||
} |
} |
||
rafId = window.requestAnimationFrame(updateCounter); |
rafId = window.requestAnimationFrame(updateCounter); |
||
} |
} |
||
document.addEventListener('selectionchange', scheduleUpdate); |
|||
document.addEventListener('mouseup', scheduleUpdate); |
|||
document.addEventListener('keyup', scheduleUpdate); |
|||
document.addEventListener('touchend', scheduleUpdate, { passive: true }); |
|||
}); |
}); |
||