What you are looking for is window.getSelection() method. This method returns a specific Selection object with the range of the selected text on the web page.
Here is how you can use onselectionchange()
together with pdf.js
<script >
// addEventListener version
document.addEventListener('selectionchange', () => {
console.log(document.getSelection());
});
// onselectionchange version
document.onselectionchange = () => {
var text = getSelectedText();
if (text) {
alert(text);
}
};
function getSelectedText() {
if (window.getSelection) {
return window.getSelection().toString();
} else if (document.selection) {
return document.selection.createRange().text;
}
return '';
}
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…