diff --git a/public/pdfjs/web/viewer.html b/public/pdfjs/web/viewer.html
index ad4f85d..55d1537 100644
--- a/public/pdfjs/web/viewer.html
+++ b/public/pdfjs/web/viewer.html
@@ -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);