Check for window.getSelection before using it