[update] pdf预览页面、标准解读查看页面 增加复制字符长度限制1000,超出后复制自动截取前1000个字符

This commit is contained in:
fengchenchen
2024-10-12 11:38:42 +08:00
parent b58f1e89c1
commit d35dbdcbc3
5 changed files with 98 additions and 6 deletions
+43 -4
View File
@@ -623,11 +623,50 @@ See https://github.com/adobe-type-tools/cmap-resources
const handleSelectionChange = (event) => {
const windowSelection = window.getSelection()
const selectionText = windowSelection ? window.getSelection().toString() : document.selection.createRange().text
if (selectionText.length > 500) {
alert('超出可复制长度')
windowSelection ? window.getSelection().removeAllRanges() : document.selection.empty()
// if (selectionText.length > 50) {
// alert('超出可复制长度')
// windowSelection ? window.getSelection().removeAllRanges() : document.selection.empty()
// }
}
document.addEventListener('selectionchange', handleSelectionChange);
const handleCopy = (e) => {
const COPY_MAX_LENGTH = 1000
const windowSelection = window.getSelection()
let selectionText = windowSelection ? window.getSelection().toString() : document.selection.createRange().text
if (selectionText.length > COPY_MAX_LENGTH) {
// 单次复制超过x个字,提示并截取前x字
alert('超出可复制长度' + COPY_MAX_LENGTH)
selectionText = selectionText.substring(0, COPY_MAX_LENGTH)
// 清楚页面选中的效果
const wSelections = window.getSelection()
if (wSelections) {
wSelections.removeAllRanges()
} else {
document.selection.empty()
}
const tempTextarea = document.createElement('textarea')
tempTextarea.value = selectionText
tempTextarea.style.height = '0px'
tempTextarea.style.width = '0px'
document.body.appendChild(tempTextarea)
tempTextarea.select()// Safari兼容
tempTextarea.setSelectionRange(0, 99999999)
let timer = setTimeout(() => {
document.execCommand('copy')
document.body.removeChild(tempTextarea)
// 清除 textarea 选中的效果
if (window.getSelection()) {
window.getSelection()?.removeAllRanges && window.getSelection()?.removeAllRanges()
} else {
document.selection.empty()
}
clearTimeout(timer)
timer = null
}, 100)
}
}
document.addEventListener('selectionchange', handleSelectionChange)
// 监听复制
document.addEventListener('copy', handleCopy);
</script>
</html>