I am in search of a method to monitor text selections on a webpage. I require code to run every time there is a change in selection. While I have successfully achieved this on the main desktop browsers, I am facing difficulties with Firefox for Android.
I attempted three different methods (all unsuccessful!):
- Attempting to capture the "mouseup" event and checking for a non-empty text selection. However, the issue here is that "mouseup" does not trigger if a selection was made during the mousedown-move-up sequence!
- Trying the same approach with the "touchend" event - with the same outcome.
- Exploring the use of the "selectionchange" event. Upon investigation, I realized that it does not get triggered when the selection changes unless the config key "dom.select_events.enabled" is set. This setting is by default false, which means I cannot expect visitors to modify their browser settings :-)
Moreover, as anticipated, the first two events do not activate if the selection is extended or reduced by dragging the selection start/end markers.
Currently, my only idea is to implement a periodic poller (using setInterval) to verify the presence of a text selection. Unfortunately, this solution is far from ideal and impacts performance negatively.
Any suggestions or guidance would be greatly appreciated.