What is problem here?
if ( window.getSelection() )
EditField = window.getSelection().getRangeAt(0);
throws error:
<
The exact way to address this problem (like 90% of everything in JavaScript) is object detection; selection related isn't as straight forward of course:
if (window.getSelection().rangeCount >= 1) {var r = window.getSelection().getRangeAt(0);}
To test throw window.getSelection().rangeCount in to a console when there is no selected text and it'll return 0; with a selection it'll return 1. I am not sure however if and how you could manage to get it to return 2 or greater.