Check what text has been highlighted by user

Thank you very much! Code works perfectly (tested in FF and Edge). But can I bug you again? I tried turning your code into a widget for processing logic in the same passage, like checking if highlighted selection is correct, etc. As I do not know how to do this in JavaScript, I tried to use your code within ChapelR’s event macro.

> <<widget "checkHighlighted">>
> 	<<set _sentence to $args [0]>>
> 	<<set _correctHighlight to $args [1]>>
> 	<<set $selection to ''>>\
> 	<<print '<div id="highlightHere">' + _sentence + '</div>'>>
> 
> 	<<event 'mouseup' '#highlightHere'>> 
> 		<<script>>
> 				var selectedText = '';
> 
> 				if (window.getSelection) { 
> 					selectedText = window.getSelection().toString();
> 				} 
> 				else if (document.getSelection) { 
> 					selectedText = document.getSelection().toString();
> 				} 
> 				else if (document.selection) { 
> 					selectedText = document.selection.createRange().text;
> 				}
> 				else {
> 					console.log('selection not supported');
> 					return;
> 				}
> 
> 				console.log('selectedText: ' + selectedText);
> 	
> 				if (selectedText) {
> 					/* Update the $selected Story Variable. */
> 					State.variables.selection = selectedText;
> 				}
> 			});
            <</script>>
> 	    <<run UI.alert($selection)>>
> 	<</event>>
> <</widget>>

Upon highlighting, I get an alert window, but it is empty. Also, if I highlight a word by double-clicking on it, alert is opened and closed immediately. Is there a way to prevent this?

Thank you!