I'm looking for a way to track the text selection on a web page. I need some code to be executed whenever there is a change in selection. I have successfully done this on all major desktop browsers, but the same does not seem to be working on Firefox for Android.
I tried three approaches (none of them worked!):
- Tried catching the
"mouseup"
event and checking if there is a non-empty text selection. Problem is"mouseup"
isn't getting triggered if a selection was made during the mousedown-move-up sequence! - Tried doing the same with the
"touchend"
event - Same result. - Tried catching the
"selectionchange"
event. I saw that it isn’t triggered when the selection changes as it needs the config key"dom.select_events.enabled"
to be set. This isfalse
by default and I obviously can't ask my visitors to tweak browser settings :-)
Also, as expected, the first two events don't get triggered if the selection is extended or reduced by dragging the selection start/end markers.
The only solution I can think of now is a periodic poller (using setInterval) that checks if there is a text selection. This is definitely unclean and anti-performance.
Any alternatives and/or advice will be very helpful.