Fix typo.
A bit of linting.
Add temporary mce settings override to stop the before unload handler from being triggered.

During before unload, TinyMCE saves the content to the textarea but doesn't apply any processing so it saves the rendered version instead of the version with the shortcodes replaced.
Adjust how selection is handled to make it work a bit better across browsers.