I also run into this problem since I did not find a good solution, this is my workaround.
It fires the selectionEnd event when the user confirms his choice by pressing the confirm / return button in the clipboard of the mobile browser.
var longpress = false; var longpressTimer = null; var loop = null; var latestSelection = null; window.ontouchstart = function(){ longpressTimer = setTimeout(function(){ longpress = true; loop = setInterval(getSelection, 200); }, 500) }; window.ontouchend = function(){ if(longpressTimer){ clearTimeout(longpressTimer); } longpress = false; } var getSelection = function (){ var s = window.getSelection(); if(s.rangeCount > 0){ latestSelection = s.getRangeAt(0); }else{ clearInterval(loop); var selEndEvent = new CustomEvent("selectionEnd", {"detail": latestSelection}); window.dispatchEvent(selEndEvent); } }
When a long press is performed, it starts the interval to control the selection. Then the user confirms his choice, and the clipboard automatically deletes it; which interrupts the monitor loop and dispatches a selectionEnd event.
You can access the last selected text in the detailed property.
I hope to get some news about this issue and get a better solution.
nicokant
source share